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