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