Type hints have completely changed how developers write Python
code instead of having to rely completely on doc strings
or developer documentation on the website for even the tiniest
bits of information, we can now look at the code
itself or use the tools that we have to get
the information right at our fingertips. No context switching. What's
even better is when our code editors do it for
us. The Python extension for Visual Studio code gives us
information.
About the methods and modules that we're using, and in
this latest update the July 2022 update, the team has
taken it up a level. Now you can see type
hints directly next to your variables and functions. Let's dive
in a little bit more, but before we do that,
I want to make sure everybody's on the same page
when it comes to type events.
Type pins were released in Python 3/5 and they were
added as a way to standardize how we statically type
our code. Now, if you know anything about Python, you
don't have to add type hints to your code, but
it does give you a little bit of information about
what you're working with and it can help the tools
that you're using.
To create a type hint, basically you just create that
object and then you add a colon or in the
case of a function you add a dash and> and
then you add what the type is of that object
followed by the rest of what that object is. Now
the proposal authors noted that in doing this it was
going to open up Python code for easier analysis.
Refactoring and even code generation. And the Ides would be
able to use it for a lot of great things
in Visual Studio code type hints as well as other
bits of information can be seen when we're creating an
object or while we're hovering over an existing object. The
Python extension added two new optional ways, so now you
can actually inlay the type hints next to the code.
That references, let's check out this example I made. Here's
a simple address book app. The module address book uses
Python data classes, which uses typing to declare how the
class should be built.
Now if we go over to Main dot Pi, this
is using those data classes. We'll define some person objects
shout out to my team.
And we'll add them to a team object. Next, we'll
create a function that gets a link to each team
member's Twitter account. That way, if we ever add someone
new, they can quickly make sure that they're following everybody
on the team.
Then we'll head over to the Python extension. You can
find that in the extensions tab. In order to use
this feature, you're going to need Python Extension version 2022
dot 10.0 or higher.
We'll make sure that everything is up to date, and
then we're going to open the command palette and select
preferences. Open settings Jason.
Down in the settings, we're going to add 2 values.
Python analysis, inlay hints, function return types. Make sure you
set that value to true and then the other one
is Python analysis, inlay hint, variable types also set to
true.
Now that first one function return types, enables the type
hints that appear at the end of a function or
a method, and the second one are those variable type
hints that we see right after we've created a new
variable object. Just a reminder, this doesn't change your code,
it just adds information to the interface and eliminates the
need for having to hover just to see what type
of object you're supposed to be providing.
That said, the hints that are there are valid Python
code, so if you do want to type your code
out, you can just type exactly what you see and
there. This wasn't the only feature added in July. We
also added support for setting up Visual Studio code for
Jupyter notebooks. My teammate Sarah has more on that, so
be sure to check out her video and you can
find more about both of these updates and even more
changes on the Microsoft Dev blog.
There will be a link in the description below to
that article.
But I want to know are you going to use
this feature? Let us know in the comments below or
let us know what Python extension feature you'd like to
see pop up next and make sure you're subscribed to
the Visual Studio code channel. That way when it comes
out, you'll be the first to know.