Rock Solid Python with Python Typing Transcripts
Chapter: Typing in Python
Lecture: Adding Our Own Types

Login or purchase this course to watch this video and the rest of the course contents.
0:00 All right, so we're going to come up with a function here, and it's going to do who knows what, and it's going to have--
0:07 we'll give it its boilerplate stuff here, tell it to run. And let's imagine we could have a calculator class, and maybe just a calculator module.
0:19 And it can do things like add one and three. And we can print out the sum of that, right? Well, obviously, since this doesn't exist,
0:28 PyCharm can't even help us work with it. So let's go ahead and imagine we had one of these. It's got a really creative implementation like that.
0:41 We'll import it. No, no, no. Now notice PyCharm is not suggesting that we can import calculator because what I have
0:49 to type here is I'd have to type from code dot, I can't do 03. Well, I guess too bad. You can't work with those unless I rename these folders
1:02 to like ch03 or whatever. So the problem is PyCharm and Python in general, if it runs with that as the working directory,
1:11 it doesn't see calculator as a thing. So what we can do is just come down here and say mark directory as a sources root.
1:18 That adds this to the Python path, which now means that we can say import calculator like that. And notice the X and Y pop up.
1:28 Looks like things are making sense here. Okay, great, does it run? Yes, four, one plus three is four. All that work to figure this out.
1:37 We could have done that. But now let's imagine we're in the scenario where we have this super useful calculator app, but for whatever reason,
1:48 we don't have the ability to change it. It's an external package that doesn't have type information
1:53 or it's our own code and we're just in the maintenance. We will not be touching this or I guess even,
2:01 I hope not, but even possibly it could be the case that you need that to run in Python 2 as well. What do you do? You check typeshed.
2:10 Typeshed doesn't exist for our code. So what we're gonna do is create another file and I'll just call it a blank file.
2:17 I'll call this calculator.pyi for information. And then we're just going to define a function with exactly the same signature.
2:28 Copy and paste is allowed here like this. Right, so you get it exactly right. And then we add the type information. That's not valid code as it is.
2:45 You can see the error here. There's like a warning. It expects something. You could type pass, But Python actually has a meaning for the triple dot.
2:57 Like let's just really quick print that and then let's print, watch this, type of triple dot. Look at that, ellipsis, class ellipsis.
3:08 Like that is a thing, but it also stands in for placeholder. So you can put it even like this if you prefer.
3:14 So now we can come over here without adding types to this and we look at what goes here And it says, oh, it takes an integer, right?
3:25 So if I come down here and try to call it with a 1.1, error. Oh, that's crazy. So pretty cool, right? All we have to do is define this PYI file
3:38 and the same like next to the PYI file or somewhere available within our project and then put structure that we want to type into it
3:48 and then put your ellipsis here to just say there is no implementation. We're intentionally leaving it empty because of this.


Talk Python's Mastodon Michael Kennedy's Mastodon