Python 3, an Illustrated Tour Transcripts
Chapter: Type Annotations
Lecture: Annotations best practices
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Few best practices for typing, if you have a function that you're passing around and you want to add typing information to it
0:08
using first class functions so you're passing a function into another function, again use that typing callable to annotate that
0:14
if you want to disregard the type use the typing any why would you want to disregard the type and use this any guy?
0:21
Well, tools like mypy allows for what's called gradual typing, and it's going to ignore any code that doesn't have types on it.
0:29
But when you start adding types, it's going to do type checking on those. This is a feature that's built in into mypy, it's intentional
0:36
and the idea there is that if you want to start adding types to your code you can add them bit by bit and as you add them,
0:44
the code that you add the types to will start getting type checked and it will ignore code that doesn't have types.
0:49
Another hint when you start adding these types is instead of returning some complicated, nested structure
0:55
list of strings isn't super complicated, but one thing to consider is instead of doing that, make a variable called append result
1:02
that is equal to this list of strings and just use the append result instead and then any place where you have this list of string guy,
1:10
you can use this append results variable instead, it just makes your code a little bit easier to read
1:16
especially when you get some of these more nested structures, we'll show an example of that later.
1:20
If you're using a named tuple, note that the traditional syntax for a named tuple here does not allow for type annotations.
1:27
So here I've got a named tuple person with a name, age and country, I can't annotate that by using that syntax,
1:33
but I can import from the typing Library the named tuple class and if I subclass from that, here I'm making a person class
1:42
and I'm subtyping named tuple, I can put these class variables here with type annotations and add the annotation there.
1:50
So if you use named tuples you probably want to migrate to this newer way of defining them that allows you to put types on them.
1:58
Another thing to be aware of is that none can be used all over the place in Python and you don't want to dereference a none per se
2:06
and try and pull an attribute off of a none object and so a lot of places in Python you'll need to have checks
2:12
for if something is actually none or not before you do something to it. And sometimes you might return something and you might not,
2:18
and in that case you're going to want to use this optional from the typing module so we can import optional here and we can say
2:25
this function find returns optionally a person object and basically it's going to loop over a list and if it finds some person that matches
2:34
it will return it, otherwise, it will return a none. So if you are optionally returning something, use that optional type.
2:41
Here's another hint when you're using the mypy tool, mypy tool has a nice little function called reveal_type.
2:47
We don't even need to import it, we just put reveal_type in our code and when we run our mypy tool it will print out what it infers the type to be.
2:56
Here I have a function called add that takes two parameters, x and y and it says the x is an integer and y is a float,
3:02
if I sum those up maybe I don't know what type that returns, I don't know what Python returns there.
3:07
If I want to find out, I can say reveal types there with the new variable and when I run mypy on this, mypy is going to say
3:14
I think that this type is this and that allows me to add typing to the res variable if I wanted to.