#460 Overlooked Python Typing - podcast episode cover

#460 Overlooked Python Typing

Dec 01, 202524 minEp. 460
--:--
--:--
Download Metacast podcast app
Listen to this episode in Metacast mobile app
Don't just listen to podcasts. Learn from them with transcripts, summaries, and chapters for every episode. Skim, search, and bookmark insights. Learn more

Episode description

Topics covered in this episode:
See the full show notes for this episode on the website at pythonbytes.fm/460

Transcript

Michael Kennedy

Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds. This is episode 460, recorded, unbelievably, December 1st. I'm Michael Kennedy. And I'm Brian Okken. And this episode is brought to you by us, especially Black Friday things that we have on offer. I don't know, it's been a great Black Friday for me. A lot of people really interested in the stuff that I put together, hopefully for you as well, Brian.

Brian Okken

Yeah, I kind of forgot about Black Friday. Well, the Black Friday has been going good. What I forgot about was Cyber Monday. So I did add today so people can use Cyber Monday code for the course today. But yeah, I just added that this morning.

Michael Kennedy

Well, people can look for an email from me if they're signed up to the newsletters. Is your stuff, what's yours through? Is it through today? I think I'm going to have it end just tomorrow morning. So in case people are like, oh, no, I always get emails like, I missed it by an hour. Or just let it go another day and then turn it off.

Brian Okken

So if you're not watching this live, you're kind of SOL. Sorry.

Michael Kennedy

No, I mean, they can listen today. Like, this is timely news. People have got to stay on top of Python by saying can't let it accumulate. No, it's fine if you listen backwards. There's a lot of stuff that's not that timely. But one of my topics certainly is. Anyway, yeah, that's going good. Subscribe to the newsletter. Follow us on the socials. Make sure to subscribe here on YouTube as well if you're interested in catching that. We intend the podcast to be an audio podcast.

But while we're doing the live stream, we do put stuff up on the screen. And sometimes it helps to see it, you know, even though we do our best to keep it audio friendly. Yeah. Speaking of putting stuff on the screen, what do you got for us, Brian?

Brian Okken

Well, today is December 1st, and that means that it's the first day of Advent of Code. So I want to look to see how long this has been going on. And I guess I don't. I'll have to look harder. I don't know how long this has been going on. This is, so what this is, it's code puzzles that you can do in any language. The intent is to do them in any language. But of course, our listeners are probably doing them in Python. Maybe Rust this year. Who knows? But this is from Eric.

Michael Kennedy

Assembly as a stretch goal? Come on.

Brian Okken

Yeah. This is from Eric Wassel. And I really appreciate it. This year's a couple changes. There's no, let's see, there's 12 days. And actually, I'm kind of grateful because I've never actually gotten through all 25. And I might, or 24, or how many of them anywhere. But so I'm kind of grateful for a shorter one. So I can take a couple days to try them out. I might try this year. And there's, what are the other couple changes? There's the global leaderboard has gone away for maintenance reasons.

So I do appreciate, it's one of the things is we should say thanks for people doing cool stuff. So Adventic culture is cool. It is sponsored. There are some sponsors that have helped keep this going. But one of the things I noticed this year, and maybe it's been there and I just haven't noticed, is there's a swag page. You can go look at some cool stuff and you can grab a mug or a shirt or something kind of fun. One of the things I kind of like about it is it's not, the year isn't on there.

So this can be one that you can just, you can pull out and wear every year around Christmas. Evergreen swag. Yeah, I like it. So fun thing. I'd love to hear if other people are using or doing the Advent of Code this year. Yeah, I'd like to hear about how it's going. Another kind of associated with this, I saw this up on Reddit.

Somebody wrote a, I know there's been other helpers out there, but there is a Python project that is called Elf, which is a modern advent of code helper that fetches inputs, submits answers and tracks your progress. So we're going to link to that as well. Just saw this. Oh, they've been working on it for a few months, getting ready for it. That's cool. So Elf is a command line interface to kind of play with the advent of code stuff. So we'll link to that as well.

Michael Kennedy

Yeah, very cool. The advent of code stuff is interesting, but I've never really put much time and energy into it personally. I just have so many projects that I have ideas for and I want to build and I can't even focus on them. So it's like, it is cool. I'm personally thankful for surviving Hocktoberfest without 10 PRs suggesting that I put a comma in my readme. Did you get any? No, I used to. I used to get a bunch. Like, hey, we've improved your readme.

And it's like, you're looking to do a PR is what you're looking to do. You're just wasting my time. But actually, no, maybe people put up rules around it or they just put up admonishment. Like, please don't bother people like that. But yeah, not this year. Yeah, cool. Let's talk about Django, huh? That's what I want to cover next. Yeah, Django. And what's cool is Django is coming up with a major new release. They're knocking out the versions pretty quickly.

And as I did point out at the opening, this is expected December 2025. So I'm expecting this any moment now, Brian. Okay. Seriously, Django 6 has some really cool features, actually some genuinely useful ones. The first thing I want to point out about it is how aggressive they are at saying no to older versions. Like no old Python. They only support Python 3.12 and above. That's pretty hardcore, honestly.

Brian Okken

Yeah, but it's an application. So you get to decide. It's not like if you're building on top of Django, you have to support backwards compatible stuff.

Michael Kennedy

Yeah, but you may have a five-year-old Django app you want to upgrade that has some weird dependency that goes up to 3.10. You know what I mean? And I'm not saying this is a bad thing. I think this is awesome. I think there's a lot of benefit that people are missing out.

Like when I did the year in review sort of thing, article at JetBrains earlier this year, one of the areas I worked on and like sort of did a bunch of math and like predictions or stats on or whatever was this what version of Python are people running on? And a huge bunch of people are still running on 310 or older. And your code is so much faster and uses less memory and just so much better. Like forget the new features that you get to use.

Just literally it's like 50% faster or something just by changing what version of Python you're running on. And this sort of encourages the Python people who also Django to ride at the further out on the edge of that wave, I suppose, which is really good because, yeah, it definitely helps. So 5.2 is the last one to support, 3.10 and 3.11. So they dropped two years worth of Python in one year. Pretty good. So features, content security policy.

We all do not love our cross-site scripting and other badnesses. So it comes now with a built-in support for the content security policy standard. So that's cool. Avoid CSS or XSS and those kinds of things. Just install it and configure it. Basically, it sets some headers, which then tell the web browsers how they're allowed to behave, cross-site, and that kind of thing. Another one, I feel like this one probably has Carlton Gibson fingerprints on it here, is they now have template partials.

This is super cool. So I'm really surprised how much HTMX, the JavaScript where you run in framework, where you write no JavaScript sort of thing, is how popular it has become in Django. It's featured in a lot of talks and stuff. And one of the things that you really, really need to focus on when you're doing that kind of programming is how do I take portions of my page and then return them

from server-side code. And you can end up with lots of duplication or other weirdnesses. So this basically addresses that if you want to return a fragment of a page, then you can say, I know here's the whole page, the whole template of HTML markup with a Django syntax. But in this view, when somebody makes this request, I want to return just this portion of it. And you can even do things like, is the request coming in a regular request? We'll return the whole page.

No, is it an HTMLX partial request? Then just return the partial. So this is really, really valuable and basically makes those kinds of frameworks that exchange partial bits of HTML, regardless of whether it's HTML, nicer. That's pretty cool. Yeah. Another really nice one is a background tasks. So just a thread that cruises around in the background that you can throw stuff at. way more useful than you would think.

Sometimes you want to process something that takes a little bit longer, but you don't want to block up the request response. Like, hey, I want to sign up for your newsletter. If you want to send them a welcome to my newsletter or whatever email, you could on that request block and let it sit there and spin, fire up your email API, connect, send, wait for that actually to send, get a response back and then say, welcome, we've sent you an email, right?

That's one way, but that makes your code potentially slow. It'd be nice to go instantly, hey, thanks for signing up. You're on the newsletter. And then kick off a task to the background, like send that person an email. For one email, it's not that big of a deal. If you've got to send a lot or do a bunch of stuff, it can time out your web requests. You get normally 20 seconds or something like that. And if it takes longer than that, it's a problem.

Of course, people are going to be freaked out and reload or whatever as well. But anyway, this is like a real simple way so you don't have to run other servers and message queues and all that kind of stuff. You just put it in the background of your app and let it go. It's not as durable, but it's great.

Brian Okken

Almost every app needs some sort of background task. So this is awesome that it's included now.

Michael Kennedy

It's cool that it's included, absolutely. Speaking of email, email handling in Django now uses the Python's modern API introduced in 3.6 using email.message.emailmessage. Super cool. Easier to send that way. and then a bunch of minor features that I'm not going to go into. Anyway, that's a pretty big set of releases or features for a year.

Brian Okken

Yeah. Yeah, so now we have to see how many people are going to update all of their Django books and tutorials and everything, Django 6. It's both an opportunity and a bit of a challenge. Yeah. So Django's not a-- that isn't just a year, though, is it? Doesn't there is the release cycle like a year and a half or two years or something like that? Let's see.

Michael Kennedy

I feel like, where's the release? Here we go. It's yearly. Yeah, it's yearly. Okay. Well, yeah. Hold on. It depends on what you call. Like, what is the version bump? There is a release every year, but it's not always a major version bump. Like, 6 is coming, but then 6.1 and 6.2 are planned. And I think 6.2 might be the LTS. Yeah. So it bumps between LTSs. And the LTSs are on a three-year cycle. long-term support so you're not forced to keep rolling to get security fixes and stuff yeah

Brian Okken

yeah and i think that's because of some deprecation uh stuff and everything so all right cool gotta love advancements in django yep um okay well i um i'm gonna look take a look at some typing stuff how's that sound i love typing let's do it um so there's a there's an article um called advanced overlooked python typing and there's some goodies in here that i didn't know about, which is cool. So I was slow to come on board with typing, but I like all of the extra. I'm kind of a fan now.

Plus, it only takes having one package that's kind of popular that people will demand that you-- so anyway, so some cool extra things if you possibly may not have known about. The first I'll jump into is-- oh, there's a disclaimer here that it says that it's looking at modern stuff. So Python 312 or 313 or newer for some of this stuff. So assert never. I didn't know this was a thing. So let's say you've got a match case thing like switches. And you've got your catch all at the end.

And you want to never hit that. Usually I'll throw an assert there or something just in case. But there's an assert never. That comes from typing. Didn't know that was there. that you can make sure that this default case has never hit, or if you have other cases that should never be hit. Throw that in there. That's cool. Didn't know that existed. There's getArgs. And the idea around getArgs is, oh, this is a little small, so I'm going to see if I can make this a little bigger.

The idea around getArgs is, so if you've got, like here we've got frozen sets to have literals. There's literals before, but you kind of had to have a duplication of all the actual literals for typing for different things. And that's in a set of literals, and that's sort of a pain. So instead of that, you can do get args, which creates essentially a set of literals for types, for things that are in something. So you kind of have to see the code here to understand that.

It helps with keeping it in line so that when you create an extra element in your set, it automatically gets an extra type. TypeGuard we've covered before. I think we've covered it. It gets you the exact type-narrowing logic. I haven't used TypeGuard a lot, but there's sort of a pickier version. Type is a stricter and more prophesied type-narrowing than TypeGuard. by enabling bidirectional narrowing. Okay, so kind of fun with type is.

Anyway, some extra, oh, we've got unpacking and concatenation for callables. Oh, callable with dot, dot. Anyway, a lot of goodies for if you want to like really get into some of the nitty gritty and some of the extra fun things with typing. This is a fun article.

Michael Kennedy

Yeah, very nice. I actually have an interesting one to add to that is no return. Oh, really? Okay. So you can go to a function and you can say arrow, I mean, goes to int, goes to stir, goes to optional customer, whatever. You can even say it returns none, which is literally it returns none. But no return is, it's not like a void. It's different. It basically says the only way that this function exits is through an exception. There's no way out of it.

So you might have a while application is still running. And then in some case, if it decides it needs to stop, it raises an aborted, task aborted exception. And it's like, while true, if exit, raise task aborted. Like there's no regular way out. The only way out is through an exception than this. So you might think, why would I ever do that?

Well, some of the web frameworks, when you do a redirect, the way they do the redirect is they raise an HTTP redirect sort of exception type thing, which is more likely to just like make it get all the way out right away rather than if you forget to say return redirect, right? Something like that. So you'd put a no return there. Isn't that a weird

Brian Okken

one? Yeah. Yeah. So I would have expected like one of the commenters, I thought no return was equivalent to return none. So if you don't have a return, that's not what it means. I'm like, oh,

Michael Kennedy

this is a really cool way to say it only returns none ever, which is the case if you never, if you don't say the keyword return, it still returns none. But no, this says the only way it gets out is through exceptions. Yeah, it was weird, right? Timing module, annotate functions that never return normally. I wanted a void. Where's my void? You know what I mean? There's nothing coming out of this. This is not it, but this is something else in that realm of advanced type.

Brian Okken

Yeah, and those things do exist. So yeah, interesting.

Michael Kennedy

They sure do. So you know what else exists is misspellings. So I got on my Talk Python in production book, Christian Klaus was kind enough to send me a message saying, hey, you know, so you may have some misspellings. And he sent me this thing, right? As a PR. However, it says fixed typos discovered by code spell, all one word, code spell. - Okay. - Wait, what is this code spell? Well, code spell is a tool that checks for common misspellings in source code.

It works on other files as well, but it especially works on different types of source files 'cause it ignores backslash escapes and other things that are common in code. But it's not a spell checker. It's a misspelling finder, let's say. So what it does is it goes and it says, I'm gonna look for words that are commonly misspelled. Like in my example, what two things had I misspelled. I had FOM instead of from, F-O-M.

had plausible with able instead of able on it. Right. So it looks for common misspellings. It doesn't look for words. It doesn't know because those are super annoying. You're like, here's 723 misspellings. Like, no, those are acronyms. You know what I mean? Yeah. like that is a library I'm importing. It's not misspelled. If I change it, it doesn't work. Right. Like that kind of stuff. So what this does is it looks for like common misspellings, like ADN or, TEH or what, you know, stuff like that.

Anyway, there's not a whole lot more to say about it other than it's pretty cool, it's configurable, you can like put words that are, you know, learn the spelling equivalents and put other types of config files and whatnot. Oh, this is cool. Yeah, you can even put it into a pyproject.toml in settings if you wish. Cool. Yeah, it's got 2.3,000, 2,300 GitHub stars. So yeah, pretty popular actually.

Brian Okken

As somebody that uses a code editor exclusively for all of my text writing needs.

Michael Kennedy

This will be good. Yeah, indeed. I think Christian even set it up as a pre-commit hook. Oh, that's a good idea. Which is kind of interesting because it's apparently pretty fast. I think this is the one we were talking about. Yeah, it has like, there's something about pre-commit like right there in the last commit. So I'm guessing it does something with that. All right. How are you feeling on your extras? I got a few extras. Okay. Yeah, go for it.

Brian Okken

Okay. Let's see. I'll start with Hatch. So Hatch 1.16 came out. So some exciting features of Hatch. So for Hatch backends and Hatch itself, what do we got? We got dependency groups and there are workspaces. So workspaces allow you to, like if you have a monorepo and you have a bunch of different projects within a monorepo, this might help with that. And then there's one, this is interesting. It supports software bill of materials now. So that's going to be, that's becoming a growing thing.

So update to Hatch and another update on a different project we talked about last week. Was it last week? We talked about in one of the things we talked about was Zensical. And a listener over on Mastodon, Fasodon said, one of the things we forgot to mention is Zensical is a replacement for MakeDocs plus, what was it, material theme? And also one of the things they talked about was MakeDocs doesn't seem like it's been maintained since the middle of 2024.

So there's a link in here to, is MakeDocs still maintained? And I scrolled. It's an interesting read, but I kind of think the answer is maybe-ish, but maybe not. So if you always have to,

Michael Kennedy

you always have to split that difference of like, it could just be done, you know, certain things are just, they really don't need more and they're supposed to be simple, but I feel like MK docs is something that could continually take on new variations and features and updates.

Brian Okken

Yeah. So not even to say no, we're not longer being maintained. Is this the, anyway, interesting read if you're curious and want to depend on that, but I think I'll be, I'll be checking out Zensical as well.

Michael Kennedy

Right. And as you pointed out last week, you said, for sure that it's a rewrite. Zensical is not just building on MKDocs. It's just the people that were working on material for MKDocs are now building a completely from scratch documentation thing, right?

Brian Okken

Yeah, it's backwards compatible if you used MKDocs before, but it is something completely different.

Michael Kennedy

All right. All right. How about, do you have any extras? Or something completely different? I do. Let's go back to, was it last week or week before? I talked about TAC, T-A-C-H, and how it creates one of those basically architectural layered graphs that let you show which parts of your app depend upon which other parts of your app, that kind of thing. And it was pointed out that, hey, hey, that thing looks kind of unmaintained.

Well, Gerber Decker sent me a message and says, TAC has been unmaintained for a bit, but it's not anymore. It was the main project of Gage, which is a Y Combinator startup that pivoted to something unrelated and based on AI, surprise, surprise. And they abandoned TAC. However, detached head from GitHub forked it and now has access to the main repo and has committed to maintaining it. So it's back. Also rough analyzed graph. It is fully independent of TAC. And they actually looked

into using that as an alternative once it became unmaintained. But yeah, anyway, that's TAC is back. TAC is back. TAC is back. Also quick shout out to Doug Farrell and thanks. He wrote a Review my book and yeah, very nice. So I linked over to that if people are interested. Thank you, Doug for writing that TLDR says recommended so appreciate that. All right

Are we ready to joke about something? Yeah. Okay, so you've heard of Platform as a service PA AS you've heard about infrastructure as a service IAS or IAAS DB as a service DB as A-A-S. Sometimes, though, your productivity just demands that you stay focused and you just say no. Right, Brian?

Brian Okken

Yeah.

Michael Kennedy

So I present to you no as a service. So this is an API that will simply return a random but realistic excuse for saying no. So you can turn it down and stop. Oh, nice. Yeah. Built by humans, excuses, and humor. This project is sponsored by Git Ads. Git Ads. I don't know what that is.

but anyway so if you pull it up it's um n-a-a-s i think it's n-o-a-s but you know whatever it's that's not the domain they got and you just get a little bit of json reason i'm finding an invisible dragon at home it's taking longer than expected or not my circus not my monkey so definitely not my act to perform cinderella left one shoe here and i need to help her find it instead of going out I would come, but I'm trying this new thing where I just don't.

So if you need to integrate no for whatever reason into your application, well, here's the API right here. You get a JSON response.

Brian Okken

It is built as an API. This is great. You can just query it.

Michael Kennedy

Yeah, we're going to get probably an MCP built over as well. So you can integrate into your AI workflow as well. I mean, it's going to be powerful.

Brian Okken

Why use a constant when you can call an API?

Michael Kennedy

guy exactly anyway I thought no as a service is a pretty good joke I I love this last one I would

Brian Okken

come but I'm trying this new thing where I just don't exactly I my favorite is I can't make it

Michael Kennedy

because I don't want to exactly no don't want to all right well uh fun episode as always thank you everyone for listening thanks thanks Brian thank you thanks all yeah bye

Transcript source: Provided by creator in RSS feed: download file
For the best experience, listen in Metacast app for iOS or Android