Python 3, an Illustrated Tour Transcripts
Chapter: Type Annotations
Lecture: The Typing library
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Python 3.5 introduced a typing library. This is in the standard library and it adds support for various types.
0:08
We can get support for any, union, tuple, callable, type variable and generic.
0:13
There's also other types that allows us to specify dictionary and list types and we'll see some examples of those.
0:20
Another thing to be aware of is if you've got a class that you're saying this variable is going to be a type this class
0:26
and the class is in the same file as you're referencing it, but the class has not been defined yet, it's going to be defind later,
0:32
you can just put the class name in a quoted string for a forward declaration of that class. Here's some examples of annotations
0:39
note that I am using the typing library here and I'm importing capital Dict here, and here in my annotation,
0:45
I've got a variable called ages and it's mapping a name to an age. So here's my traditional dictionary over on this side here
0:53
Fred has the age of 10, and here's my annotation here. I'm saying dict and then I put square braces here and then I map the key and the value here.
1:06
So string here is the key and it is the value. What this allows me to do is specify what the type,
1:14
the specific type of the key is and what the specific type of the value is. Again, I'm going to harp on this but when you run your code
1:22
Python is going to ignore this, you need to use a tool like mypy to actually get some information out of this, we'll show an example of that later.
1:29
If you've got a list of items here in square brackets in our annotation list the list type that we import from typing, note that it's capital List,
1:37
we see that we are putting strings into our list. So this allows us to type our list and use a specific type in there.
1:46
Same thing for tuples we can specify a tuple, we're going to import capital tuple from the typing.
1:52
And in this case we're saying a person has a string, an int and a string. And so those correspond to the individual items in the tuple.
2:01
So Fred is a string, 10 is an integer, and USA is a string. We can also specify types for callables,
2:09
here, I have a little function called repeat that takes a function as input, again, Python has first class functions
2:15
and I can invoke my function down here, I can pass my function around and I can return functions and pass in functions to other functions.
2:23
And we can say that this function that's getting passed into repeat is going to be a callable. We're going to import callable from typing
2:31
and here in the square brackets inside of the square brackets, these are the parameters that are passed into our callable
2:37
and then this final guy here at the end is the return value here. So this callable that's passed in, if we're passing in add
2:43
it takes two integers and it returns an integer as output. One thing to notice, PEP 526 provided some syntax for typing variables,
2:52
but it didn't provide syntax for all the variables that can get created in Python, when you use a with statement and use an as at the end
3:01
it creates a variable called foo and when you use a for loop and you put variables in there, it's going to create x and y.
3:08
Pep 526 does not have a way to inline those type annotations there but PEP 484 did by providing these comments here.
3:17
So if I've got a with statement I can put a little type comment at the end that says foo is going to be type int.
3:24
Similarly, for a for loop, if I want to put a type on those I can put a type there that says x is going to be a float
3:31
and y is going to be a float as well. This is straight out of the PEP 484. The typing module also has support for typing generators.
3:39
If I want to type a generator, I can just say this echo_round returns a generator and this first guy here, this is going to be whatever it yields.
3:48
So we can see that we're yielding round res, which is an integer. The second value that's getting past here, float is the send_type
3:56
so we can see that when we get input into it, it is going to be a float or it should be a float and we're going to round it,
4:03
so what comes in should be a float and then this final one here is the return type. So the string that we're returning is going to be a string.
4:14
So that's how you use the generator type annotation.