Rock Solid Python with Python Typing Transcripts
Chapter: Typing in Python
Lecture: Gradual Typing
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's close out this chapter by talking about gradual typing. Now, instead of me showing you a really complex example of this,
0:08
I'd like to refer you to a talk done by Lukas Lange when he was at Instagram.
0:13
He's now the developer in residence at the PSF for Python, which is awesome.
0:17
But previously, he was at Instagram and talked about how Instagram used gradual typing and mypy
0:24
to over time reduce the number of bugs that they had in their system.
0:29
And it sounds like everyone over there, maybe not everyone at first was on board with it,
0:33
but by the time they started seeing real results, it was really paying off and everyone was into it. So what is gradual typing?
0:40
Gradual typing is mostly about the tool mypy, which we're going to talk about later.
0:44
But it's the idea that if I define the types for one part of my code, it starts to trickle over into other parts.
0:52
We saw that with the type inference where we returned a person object, and I didn't say what its type was,
0:58
then it started having auto-complete features for the type that was returned because that thing, that function that was defined
1:06
had its type information, so that would trickle over to things using that function. Then if we take that value and pass it to another,
1:12
and if it has types, well, that can be checked. Instead, well, that's a person, you expected a string. Did you mean person.email?
1:18
All of those things start to build up over time. So this is the design pattern where you start at small blocks of your code,
1:25
you add some types, run type checkers as much as possible to give you information about
1:30
well, so far what you know is that working because Python typing information is both
1:37
ignored by the runtime and optional, you can use this process of building up the type information
1:43
as you want to understand a part of your app better or get more type safety, you can just add that typing gradually.
1:50
So this is I think a 45 minute or 30 minute talk. You can listen to Lukas talk about that. That'd be great.
1:57
One of the important pictures that came about it out of it is how much type information,
2:02
how much typed Python code did they have versus untyped Python code. Here's the human written as opposed to generated percentages over time.
2:12
So it started out really, really low. You can see mid April of 2017, it was like half a percent or 0% depending on the lines
2:22
or the functions that you're talking about as a ratio. For a couple of months, it was only a few percent.
2:28
And then people are like, okay, this is really awesome. Let's add a bunch more where we might get some value and it jumped up to 15%.
2:34
And it's just continuing to climb. Remember, they had something like a million lines of Python code that had zero type information whatsoever.
2:41
and they started bringing this type information into their code base. So the people at Instagram got a huge benefit from Python typing.
2:50
You probably picked up a lot of reasons why already in this course.
2:54
But if you want to have a concrete example that you can bring to your team and talk about, go check out Lukasz's talk here.
3:02
See a lot of what he talks about some of the best practices and ways of working retroactively
3:06
with a large codebase and applying types to it after the fact.