Rock Solid Python with Python Typing Transcripts
Chapter: Typing Guidance, Patterns, and Advice
Lecture: Refactoring with Types
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Here's a simplified simplified version and also slightly partitioned into multiple pieces of our motorcycle app that we started this course with.
0:10
So here you can say create some bikes and it's gonna go create some adventure bikes of different types, Himalayan, Tenere, KTM, et cetera.
0:19
And it's awesome because it expresses the type information in 3.9 or above style with a lowercase L. So list of bike, yeah, that's super cool.
0:30
And it creates a list of them and sure enough returns them. And if we run it, just let's see what it does. See some bikes and there they go, right?
0:38
That should be familiar from before, right? All we, right, I hear all we care about is that it's iterable, but in this case it's kind of the reverse.
0:46
So it's fine that it's overly specified in a sense. So what if I wanna make some changes? Imagine I'm not even in here now, I'm over in this section
0:56
and I've got a way more complicated app than two files with simple things in both of them. But maybe we're over here looking like, you know what?
1:05
We called it bike, but there's another part of our app that works with bicycles or mountain bikes or something like that.
1:12
We wanna be really, really clear that this is let's say a motorbike or motorcycle. I'll call it a motorbike. And by charm, we can right click,
1:22
refactor and there's all these awesome things we can do, but let's just keep it simple and say rename. I'll say this is a motorbike.
1:32
And notice down here as I type, it's like this whole file is live updating itself, which is awesome. So this, we're gonna make some changes here.
1:44
Do we wanna do this? Yes, yes we do. And because we're using typing.self, right? That's one of those weird edge cases on how Python defines classes,
1:54
but it also, because it's our motorbike where even the return type has changed. But look over here, this is term blue, which means it's changed.
2:03
And it's changed in what way? Well, motorbike. And I'll rename the file too. Let's guess that's what it was probably asking me.
2:13
Motorbike, that's obvious, right? This part of code, but even the type information here, right? So if we wrote this in like,
2:20
say some kind of docstring style type information or whatever, or in a strings, we might not get that right. And here we do.
2:29
So the complete type system sticks together under this awesome refactoring, which is pretty sweet.