- Podcasts
- Python Bytes
- #195 Runtime type checking for Python type hints
#195 Runtime type checking for Python type hints
Python Bytes
- August 18, 2020
- 33 min
- Episode 195
About this episodeTopics covered in this episode: watchdog Status code 418 pydantic’s new Validation decorator Building Python Extension Modules in Assembly…
- watchdog
- Status code 418
- pydantic’s new Validation decorator
- Building Python Extension Modules in Assembly
- easy property
- Non Blocking Assertion Failures with pytest-check
- Extras
- Joke
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
#195 Runtime type checking for Python type hints
And maybe you've heard of it. It's the foundation of some web frameworks, for example, and things like that to know if, say, a file has changed that you're editing. Does it need to auto-restart the website? So when you refresh it, it actually reruns it. Things like that. But Watchdog is a cool little library that you can use just on your own to know if something has changed.
So, basically, it's a simple little API. You create this thing called an observer. And you tell it to just start watching in some way. You give it a path. It can recursively look. You can give it, like, a pattern or whatnot. And it will say, just basically start firing events back on this observer thing when stuff happens.
File is created. File is deleted. File is modified. And so on. Nice. This is cool. Yeah. Isn't that cool? It also comes with a CLI script called WatchMeDo. And what you can do with WatchMeDo is just type WatchMeDo, all one word, log. And wherever your current working directory is, when you type that, it will just start watching for files that change right there, just like on the command line.
There's a bunch of other things it does that you can pass commands and stuff, I suppose. Like, you could ask it to recursively watch or whatever, but yeah, I haven't had a use case for that. So, but yeah, this is a cool recommendation from Preycin. Yeah, nice. The example that I think I could possibly use it for is, you know, one of the things that we do that makes Python Bytes, .fm, as well as all the Talk Python, Talk Python training sites ridiculously fast as a user is we don't require you to re-download almost any of the static content, anything JavaScript, any image in the entire site, you know, CSS, all of those things, they're cached for a year, right?
And so a problem with caching stuff for a long time is if you redeploy a new version, something's wacky, right? Something is really weird. Like, I have this problem with Twitter and Firefox. I'll go there and periodically it'll just say something went wrong, can't load it, and a command R, a hard refresh always fixes it because there's probably some JavaScript file that's like out of sync with some other part of some API or some random thing like that.
If it ever changes one character, if it changes, the hash changes and it's now a totally separate thing in the web browser cache, right? All of this is perfect. The one drawback is to make things fast, you're not rehashing all the images on every request. Is it just says, have I seen this file before?
Do I already compute the hash? Just use it, right? So that's fine. But that means that little hash refresh trick only really works if I restart the website. If like, say, only a CSS file changed. I could use this watchdog to watch all the static files that we're hashing. If any of them change, just instantly recompute the hash.
Yes. Have you heard of error code 418 before? I'm a teapot. This is just great. So I love it. I love it. Status code 418. I'm a teapot. Any attempt to brew a coffee with a teapot should result in an error code 418. I'm a teapot. The resulting entity may be short and stout. So the resulting entity may be short and stout.
I love it. This actually got brought up in a conversation I was having this morning. A colleague of mine, Andy Howe, suggested it. He said, so when is Python 3.9 going to come out? Because hasn't it been a while? And as a reminder to everybody, Python 3.9 candidate release or the release candidate one or RCCR or whatever is out now.
So you can play with it. It's probably, I've been using it. It's safe to use. But the schedule for the release, final release is in October, I believe. And then after that, bug fixes, releases every month are planned, if there are any. Andy said, you know, my favorite enhancement for Python 3.9 is this 418.
I'm a teapot. This is new in Python 3.9. HTTP library was missing the status code 418. I'm a teapot. And now it has it in there. But wow, I've got some links for you. Because in the show notes, this is referenced from HTCPCP, which is Hypertext Coffee Pot Control Protocol. And this came out in 1998 as an April Fool's joke.
I really love it. I don't realize, I didn't realize it had been taken so far as this. This is great. And, you know, if you go and look at the thing you link to, the Hypertext Coffee Pot Control Protocol, HTCPCP slash 1.0. The whole memo describes so much of this thing. And it even talks about things like crossing firewalls.
It's beautiful. Yes. And there's a security layer that I forgot about that's funny, too. That's part of this. But I can't find it right now. Yeah, no worries. I threw in a quick link also to this place called HTPstatuses.com, which this is a slightly more serious take, although it does, of course, include the coffee pot.
But it's just, if you were like, what the heck should I be doing, you know, in this situation? Like, here's the error codes here. So, and if you just click on them, it gives you a, like, really nice summary. Like, if you click on 403, for example, it talks about when it's used and when it's, like, how it compares to 404.
And then also how that reference code is set up as an enumeration in different languages. So, for example, in Go, it's HTTP.status forbidden. In Rails, it's colon forbidden. And then Python 3, it's HTTP.htpstatuses.forbidden. And things like that. So, it tells you, like, the language version instead of typing it all in yourself and having magic strings and numbers everywhere.
Yeah. So, it does look like, I don't know what Symfony is, but Symfony and Go do also support 418. Status team. Beautiful. We're on par with Go in terms of the internet now. Very good. That was a good one. It almost could have been the joke, but I like it. All right. Also, things that I like is that we get a chance to create amazing stuff to help people get better with Python out there.
So, over at Talk Python Training, we have three new courses coming very soon. We have Getting Started with Data Science. We have Moving from Excel to Python. And we have Diving into Python Memory Management and Optimizing Your Programs around that. All three of those are coming within probably a month or so.
So, if you want to get notified about that, just visit training.talkpython.fm. And right at the front, there's a little email place you can enter your email address to get notified. How about you? I wanted to highlight Test and Code. So, pytest 6 is out. So, Test and Code is the other podcast I do at testandcode.com.
And I had talked last week that there was going to be a pytest 6 episode. And instead of doing it just by myself, I contacted Anthony Sotilli and had him come on the show. And so, we BS about stuff for about an hour. Some of it's actual content. So, I'm really trying to get – I'd really like to have the shorter episodes on there.
Like, I haven't created any new projects that probably really deserve this. But I think it's definitely one of those things that I want to start using. Because it's just so slick. The ability to say, I have these models. They have these types. You can validate them. You can auto-convert between them.
And so on. Right? So, we talked about that not too long ago, actually. However, Andy Shapiro sent over a heads up about a new feature that's in the beta version of Pydantic that's coming. That makes me pretty excited, actually. I don't know. We'll see how people feel about this. But I'm excited about it.
I'm excited because I'm a fan of type hints. Yeah, me too. I love type hints. Yeah. I love them mostly because the way they make the editor help me write code. Instead of going and saying, oh, how do I do this thing with this library? Right? If I just say the type of this thing is one of the objects out of that library and I hit dot, boom.
The editor shows me all the stuff I can do. It just keeps me in flow and working on what I need to. So, I love type hints mostly for that, but also for the validation. One of the things they are like, but they do not do, is they are not like static typing in other programming languages where somehow that verifies what's being passed.
Right? Yeah. So, like, for example, if I have a function, it says it takes an integer, an age colon int, and that's what it says it takes. But then I go and I write code and I give it quote seven instead of the number seven. That's fine. Python's like, yeah, that's cool. You probably don't know what you're doing, but whatever.
We're just going to let it fly, right? Well, with Pydantic, there's this new type annotation validator. And so, what you can do is you can say, for this function, it has type annotations, and I want those to mean something. Oh, okay. Okay. So, it gives it basically that it's not runtime behavior in the static languages.
It's a compile error, right? Your argument string cannot be converted to enter whatever the compilation error happens to be. But it would give you the runtime equivalent. So, all you have to do is you have a regular Python function, and it has regular types, just like it would, you know, s colon str, count colon int, that type of thing.
But if I say repeat, hello, comma, goodbye, boom, exception, validation error, you know, the value is not a valid integer, type equals type error integer or whatever, like some message there, right? So, it says the count parameter is not and cannot be converted to the type specified by the type annotation.
Oh, yeah, this is cool. How do you feel about this? I obviously don't want runtime typing for all of Python code. It's not, I don't want it. But for special places, like maybe around your API. Exactly. The boundary of libraries, like stuff going in there. Yeah. You're like, please don't send me the wrong data.
Or you're going to send out weird errors like int does not have, you know, type int does not have function split or some weird thing. And people have to realize like, oh, what that means is you type some kind of, you send a number where a string value is accepted, right? So this, if you really do depend on the types that you specify in your type annotation, this seems like a good idea to me.
But Anthony Shaw, he took it to another level, like further down than even something like rewrite bits in C or compile Python to C or something. Yeah. I think this is maybe an example of Anthony just having too much time in his hands. I think maybe. So Anthony wrote a, I just tried it this morning, a project called PyMult, P-Y-M-U-L-T.
So I like it actually. Yeah. And anyone wants to know what the code looks like. It's like move racks comma X, imol, qword of Y, move result to racks, move eddy to result. Like it's, yeah. But then you get a call pylong from long, which is kind of a cool thing. So there's this like interesting mix between the just pure assembly language and the C data types of CPython.
I mean, I wrote some assembly in college. That was a long time ago. Yeah. Well, you had to, Anthony, because that's some impressive stuff. Yep. Would you say it's easy though? No. No. That's hard mode. Yeah. You know what actually is not as easy, I think, as it should be. And it's honestly just bizarre.
Our properties in Python, as in at property, some function return some value converts what looks like a function call over to something that looks like field access. Right. Yeah. But why does it have to have this bizarre sequence of conventions where I have an at property and then a function that has a name?
And then the order of the setter varies as well. Right. It has to be, or is constrained. It has to be after the property a. And what I've found also is that you can run into issues with inheritance. I think if the property is defined in the base class, you try to create a setter on a drive class, things don't work right as well, which is all kinds of weird.
And it doesn't matter what order they are or if they're all defined. Like for example, you can't have a setter without a getter. Now conceptually, you should never really do that. But just, you know, syntactically, like those things have to be defined there. And like I said, the sort of base class, drive class type of thing, it gets wonky as well.
Nice. Isn't that nice? Yeah. So there's a cool little example that he wrote. You know, you can do them separately, which is probably what I would recommend and do. But you can also have an at getter setter. So you can have one function whose job is to both be the getter and the setter operation of the property.
And then the way it works is just the value is set to be like some default value, which is what happens in the, you know, in the get version. You can have an at documenter. You can have an at documenter. So you can also have documentation for your property. And there's a little function that will come up and do that.
So anyway, it's, it looks pretty nice to me. It's not one of those things that's going to be, you're taking a heavy dependency upon to give it a try. Right. It's not like a, a runtime thing. Like for example, that validate arguments, that's in between every time you call a function all the time, all the time.
So this is cool. I like it. Thanks for developing that and sending that over. So we talked about assembly and properties and decorators and stuff. So if anybody's left listening to this podcast, we do have more to talk about. Let's talk about recursion. No, just kidding. I couldn't resist highlighting the last topic.
So pytestCheck is a library that allows you to do multiple failures within a test, mostly because assert normally uses asserts, but asserts stop after the first failure. And sometimes you want more. And I never really thought about the different use cases. But so Ryan has the use case of using it with Selenium because sometimes when you're testing a page coming back, you might have to test multiple aspects of it.
His example was what's the content of some field and also if the URL is correct or something. And I think with like a Selenium or some sort of web test, that totally makes sense because you'd have things like you want to check the error code and content and whether somebody's name shows up, then a whole bunch of stuff you could check and having multiple things.
Right. You don't necessarily have just one assertion that captures all of the essence of what you're trying to determine, right? Yeah. And so I also linked to the pytestCheck library and then or plugin. And then also an article that I wrote back and way back in 2015 where I started thinking about this and thinking how to solve it.
But if I go over to Word and I type pytest is a testing framework, I'll bet you that Word makes it capital. Yeah. Doesn't that make it real? No. I know PowerPoint well. So. No. Yeah. That's always tricky, right? When you have sort of a formal name, but the formal name doesn't have spaces or a slower case.
I mean, so there's some, I guess it's a weird thing with tools and stuff. Some tools don't care whether you capitalize or not. The people in pytest like it not capitalized. So anyway. Yeah. Let's respect their lowercase p's. Yeah. All right. So I have three really quick things. They're all kind of fun.
So PyMC is a cool library for Bayesian analysis and probabilistic programming in Python. So Alex, one of the core developers there, sent me a message, said, hey, we're planning the first ever PyMC on. Okay. I pronounce PyMC on because it's so on, they say. And it's the first Bayesian community online conference around PyMC.
So if you are into probabilistic programming in Python, check it out. There's a bunch of cool stuff they got going on. They're also, I think, have a call for paper. So if you want to do a presentation, shoot them a note. And yeah, I'll link to that in the show notes. Second, a while ago, we talked about Rumps quite a while ago.
Rumps is ridiculously uncomplicated Mac, I don't know, something, menu programs or something like that. And so I ended up, I was sitting around, I have this little library that I run because so many things in my life require taking a title or taking some words and turning them into a file name or a URL name.
All right. So suppose I've got the title of a video and I want to name an MP4 file that. And it's got like a colon in it or like a forward slash or some random thing that shouldn't be in there. I don't want spaces. I want it. So I wrote this little command line utility called URLify that I would just run.
It would take whatever's in the clipboard and replace it with that canonicalized version that would work well as a file name. Oh, cool. Yeah. And that was cool. But then I got tired of like always firing up the command line, the terminal. Maybe it's busy doing something else. So I got to do a new window, run that thing and then close it down.
And I was like, I just want to click something once. All right. I don't want to click the terminal and then start a new terminal. And it was like not that much work, but you can imagine the way I'm going on and on about this. I must be doing it more than is reasonable. So what I did is in like 45 minutes, I converted that command line app to a Mac dot app file, like a full on dot app file that I could just ship to any Mac user.
They have no idea. It uses multiple Python packages, a code that I wrote, and it runs as a GUI auto starting with my macOS when I log in in my menu bar. Oh, wow. Is that cool? Yeah. And so I linked to a little tweet that has a screenshot just says convert text. But actually, I changed it to URL fi text, trim text.
Okay. But I don't have the GitHub repo public yet. So let me just like put like a screenshot and what the heck it is and then I can make it public. But yeah, so I'll put it in the show notes. Neat. Yeah. Yeah. It's super cool. And finally, I just want to let people know who are taking courses over at Talk Python.
I was sitting around and I decided I want as you hover over the scrubber, I want it to show show a little thumbnail of where you would go in the video if you were to click the scrubber in that location. What's the scrubber? We're not like the little like time thing where you can click around and like zoom ahead and whatever.
And we're not like reusing Vimeo or YouTube or something that might potentially do that. I think YouTube does it. I don't know that Vimeo does. Anyway, I recently figured out how to do that. So if people are interested, they can check it out. But there's a cool little trick where you can take a second hidden video player and point it at either the same video or a smaller version of the video.
And then as the mouse moves, just seek it to that time and show it above where the mouse is. Isn't that crazy? Yeah. So if people need something like that, yeah, a little cool trick you might consider. Not that often in Python, but if you happen to make your way to the JavaScript side of things, you'll do a lot of that, I guess.
So is that in your training site? Has it been updated then? Yeah, exactly. It's not updated as we speak because I'm still busy transcoding 200 hours of video to 500 by 200 size. So I'm about halfway through that. I've had yesterday I uploaded 200 gigs of data. I'm so going over my data limit this month.
Cool. How do we use it? No idea. Just memorize these shell commands and type them to sync up. If you get errors, save your work elsewhere, delete the project, and download a fresh copy. It's funny because it happens too often, I think. Yeah. I think most people have like the four or five or six Git commands that they use all the time and everything else they have to look up if they need it.
Yeah. Merge conflict. All right. I'm deleting it. I'm going to copy this back over. Yep. All right. Funny indeed. Well, thanks for being here as always. And thanks to everyone for listening. Thank you. Bye. Bye. Bye. Thank you for listening to Python Bytes. Follow the show on Twitter via at Python Bytes.
