Rock Solid Python with Python Typing Transcripts
Chapter: Typing in Python
Lecture: Externally Defining Types
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
One of the real powers of Python is its insane external set of packages.
0:07
Here on PyPI, we can see there's almost 480,000 separate libraries that do who knows what, you know,
0:15
you name it, there's probably a library that will do it here, right? How maintained are these? How turned on to the type system are they?
0:25
There's a bunch of packages that for a long time tried to straddle the Python 2 and 3 world. And I already talked about, well, in 3.9,
0:34
you can use this functionality for the typing definitions. But before that, you've got to use something else. Well, before 3.5, there was none.
0:41
So a lot of this wasn't able to be applied to those ones that were straddling the fence, so to speak.
0:47
And if they haven't been updated, well, what do you do? They're not your projects. It's not your code.
0:54
you can't go and make changes to it or specify the types, right? So what are we going to do in that world?
1:00
In the Python type system, there's a way to have a separate file that says, It looks like that package, but it actually has only type definition.
1:12
So if you come across a library, a package, code, whatever, itself does not have type information,
1:18
and for whatever reason, you don't want to change it, you could do this to your own code, you know, at your company,
1:23
if they say this part of code we're not touching, but you still want type information, there's
1:28
a way to add that to it. Now let's start by looking at this place called type shed. Type
1:35
shed has a bunch of things for the Python standard library. So for example, if we talk
1:41
about asyncio, you can come down here and here's the futures not dot p y file, but futures Let's look and see what's in this.
1:52
So it's got all the standard imports you would expect, but here's a function given an object. You can ask, ""Is it a future?
2:01
Well, it can be anything, so object kind of is another option there. And it tells you this is going to return a type guard of future of any.
2:10
Similarly, the class future is a weightable of whatever the type here is, the type that that we've defined right there as a type variable.
2:20
So it's a waitable of that type and iterable of that type. Here's another function. It takes a self and returns a base exception or none
2:29
or so an optional exception. And these dot, dot, dots, this is not the editor being collapsed.
2:35
This literally is the implementation of that code, right? So these PYI files allow us to externally add type information to existing Python code
2:46
without changing that existing Python code. So why bother showing you typeshed at all? Why not just do that for our own code?
2:52
Well, we will, but what's interesting is it's not just for the standard lib, it's also for other things. So you may be familiar with Pillow,
3:04
a library for working with images in Python. Well, here's Pillow's definition for its types. So the image plugin,
3:13
Its format is a class var of literal of that. Its format description is a class var of string and so on. Interested in CalDAV, there's its information.
3:24
Docutils, DocOpt, Flask, Flask things over here, Flake 8 stuff. So you can see there's a bunch of third-party things
3:37
that you can find the type definitions for. You can just pip install typeset element.
3:43
So for example, up here it says things like mypy, pycharm, pyrite, these all come with
3:50
a copy of typeshed bundled with the standard libraries or bundled with themselves. So you don't need to do anything.
3:57
But if you want things for third party packages, you can install them.
4:01
For example, if you're using six and requests, you can install types-six and types-request to get those stubs installed into your project.
4:11
And then things like PyCharm or mypy will automatically pick them up.
4:15
Okay, so typeshed is a whole bunch of work that many people put together to already define
4:20
types for not just Python, the standard library, but also for these popular external things like requests.