- Podcasts
- Python Bytes
- #343 So Much Pydantic!
About this episodeTopics covered in this episode: Pydantic v2 released Two Ways to Turbo-Charge tox Awesome Pydantic CLI tools hidden in the Python standard…
- Pydantic v2 released
- Two Ways to Turbo-Charge tox
- Awesome Pydantic
- CLI tools hidden in the Python standard library
- Extras
- Joke
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
#343 So Much Pydantic!
Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds. This is episode 343, recorded July 11th, 2023, and I am Brian Okken. And I am Michael Kennedy. That's pretty exciting to be here. If you're listening to the show, thank you. And also, if you'd like to watch and participate, ask questions or whatever, you can do that by going to pythonbytes.fm/live.
And we'd love to have you, even if it's just once in a while. Yeah, sometimes people show up in the live stream that will possibly get their projects will actually get mentioned as well. And that's always kind of interesting. I'm feeling this might happen today. Yeah. Speaking of that, Seth Larson showed up and we have some Seth Larson news to share.
So that's cool. Hey, Seth. Well, how would you like to start us off today, Michael? I would like to start it with a version update. So let's talk about Pydantic. We talked about the Pydantic version two story and performance. So Pydantic got a major rewrite. This rewrite was, I don't know, maybe a year in undertaking, a year long undertaking.
That's very exciting. Yeah. Yeah. So there's a blog post that says the last few months have involved a whirlwind of work. And finally, we're ready to announce version two. So you get started by just installing it, upgrading it. It requires Python 3.7 and above. So honestly, that's a pretty old bit of Python that you can be based on there.
That's great. Now, there's some pretty major changes in terms of the API, I guess you would call it, the way that you work with Pydantic, right? In some cases, the way you work with base classes or the functions you call and all those things. So there's a migration guide that we can use to go through that.
And if you check out the migration guide, you'll see like this. You got the migration guide. It starts by recommending this thing called Bump Pydantic. And I guess I should probably also just let you know, like, this is like a big Pydantic episode that we're going to be covering a lot of Pydantic things.
So there's this thing called Bump Pydantic. And what you can do is you can run it against your project and it will traverse all the code and find all of the Pydantic things, the classes and functions and so on that you might write with for your Pydantic code and it will upgrade them to the new thing.
So we can check that thing out first. Basically a converter. It's like a two to three, but for Pydantic. It's like a one to two, I guess. And so it'll do things like one of the things you should do is you should have explicitly set the default value. So previously you could have a class to give an example of a user that's a base model.
Right. And a bunch of stuff like that, that you can run and it'll do that automatically. But this is, if you look at the migration guide, this is actually just scratching the surface. So there's, there's a lot of other things that you need to be aware of. And I don't know how far they intend for this bump Pydantic to go and try and to manage those things.
But for example, cool that they have it though. It is pretty cool. They have it. Yeah. That even it's a star, right? Yeah. They let people say, here's the changes you have to make. And also here's a tool that can do at least most of it. So exactly. There's things like in old Pydantic, you had a Dunder fields and now you have model fields as a field to the base class.
You had Dunder private attributes and now you have Dunder Pydantic private. And there's, you know, there's more relevant ones. Like there was a copy function and now there's a model copy. There was a dict and now there's a model dump. So if you had a Pydantic model, you wanted a dictionary, you could just call dict on it.
If it runs and you don't get them, you're probably fine. But if you get errors, then go consult the migration guide. If you get deprecation warnings, do the same with, but maybe with slightly less urgency. So yeah, there's a bunch of stuff that you can go through. One of them was, one that's interesting is they introduced this thing called a type adapter.
Let me see if there's a section where they talk about it. Yeah. So now they have this thing called a type adapter, which is pretty cool. They have some example. There we go. It says in Pydantic version one, we had weak support for validating or serializing non-based model types, like a list of something.
So now you can create this instance of a type called a type adapter and you pass the type. It adapts. So you can say a type adapter of list of int, and then you can just go to it and say validate Python. And for the example here, they have a list of numbers, but they're in strings, like the list string one, string two, string three.
Because working with Pydantic is great. And you can do cool stuff with Pydantic directly. Like you can directly work with it in a Flask app, for example. But if you work with FastAPI or Beanie or all these other frameworks that are built on top of it, you've got to wait for, you know, they are deep down in the guts of Pydantic a lot of times because of the way they work.
Except for now FastAPI, though. FastAPI is already upgraded, right? Yeah. Is it released or just the beta is out for it? I can't remember. So FastAPI released version 0.100.0. Come on, guys. Zero verse still. Anyway, four days ago. And it does look like the last, like five days ago, we got a merge for adding support for Pydantic 2.002 or something.
I'm excited. Grant, on the audience, does point out, tough luck for base models with an existing model attribute. There might be some mechanism to rename that or remap that. You know, there's like an alias you can put on them. But you say, when you look in the data, it looks like this, even though the name is that.
But yeah, there might be some trickiness here. Yeah. Yeah. At least at an API level, you could be able to fix it. I don't know about the Python code level, if that'll help you. All right. Well, anyway, big news. Big news. Pydantic version 2 is out. This was on June 30th, but we were off on vacation, Brian.
Mostly my fault. Sorry. Well, actually. So we didn't cover it. It's all right. Now that we covered it, now we can cover that FastAPI is caught up. Yeah, exactly. Wait long enough things. It's come together. Well, I want to talk about talks a little bit because I learned something new from Hinnick, which isn't that unusual.
I often learn a lot of stuff from him. However, there's an article from him saying two ways to turbo charge talks. And it just starts right off the bat with saying, no, it's not just run parallel. Because actually running parallel is pretty cool. And if you don't know about running parallel, I usually just use the talks-p to run parallel.
If you can run all of your environments in parallel, it's super cool to be able to speed it up. That's the awesomest way to speed it up, I think. He does note that it's broken for some people. So talks for talks for is awesome, and it's got some great new features. But apparently for some people, parallel isn't working.
I'm not one of those people. So most of my, all of my projects that can use talks or parallel seems to be working. But I'm not using something complicated. Apparently it's broken for attrs, for instance, or at least it was last time I checked. Anyway, so what is he recommending if it's not running in parallel?
What should you do? This is something that I didn't really think about before. So one of the things that talks does is for each environment that you set up, like say you've got environments for three versions of Python, and it will create a source distribution of your library or package for each of those, if you're doing a normal, like using talks to test a Python package.
It'll build a source distribution for each environment. Except for that's not really how we use our packages. We build it once and throw it up on PyPI, and everybody uses the same one if one supports all three versions of Python. So he's showing us a way to get around that to say, instead of building it on all environments, go ahead and just build the wheel once.
But so there's a wheel build environment that tells you, tells tox what directory to stick it in. And if it's the same directory for everybody, it'll get used for all environments. And this alone sped up quite a few of my workflows, which is really cool. Just building it once, using it, and testing it in different Python environments.
None of the stuff I work with does. Okay, so that's the first way to speed it up. What's the second way? The second way is if you're in one of these camps, especially one of these camps where you can't run tox in parallel, you can still run pytest in parallel. So he's recommending using pytest xdist as a plugin, and then running pytest dash in auto, or you can give it a number of CPUs or whatever.
And auto just says, pick the number of CPUs you want, and you can run pytest in parallel. This actually, if you have a fast test suite, this might slow down your test. So be careful. Measure it. However, he has an example. In a lot of CPU-intensive test suites, like, for instance, if you're using Hypothesis, and it's running a lot of tests that are similar.
So he's got some stuff here to help you through cleaning up your coverage, which is great. Skipping down. For the attrs project, those two tricks, he started out with 257 seconds and got it down to 69 seconds using parallel pytest and building a wheel only. I had a test suite that was pretty fast anyway, just, you know, 18 seconds.
Yep. And the bigger the project, the more, the more you care, right? Yeah. And I wasn't using a hypothesis. I would like to try this out using hypothesis. It's pretty cool. Mm-hmm. Excellent. Good one. All right. Before we move on to the next item, I have a bit of real-time follow-up, but not real-time follow-up in the traditional sense where we have audience members.
I just got a message from Roman Wright, who is the creator of Beanie, the async MongoDB ODM based on Pydantic. And he said, I am releasing a version of Beanie that supports Pydantic version 2 soon, either this week or next week. So, don't want to put him down for a certain time exactly, but just that project is also moving along like FastAPI did to support it.
So, this is kind of an early-stage project here. It got created five months ago or so, but I feel like it needs people out there who have Pydantic things contributing them back. For example, do we have, I don't even think like SQL Model is listed here, where SQL Model is created by Sebastian Ramirez based on Pydantic and SQLAlchemy, for example.
spaCy, of course, the free open source library for natural NLP, natural language processing. It's a pretty cool one. Gina, J-I-N-A, is geared towards building search systems for any kind of data, including text, image, audio, video, and more. That's pretty excellent. Modular design and multilater abstractions.
You can leverage efficient patterns to build different parts of the system or change them to a flow. That sounds pretty interesting, right? Yeah. Down here, we have the object mapping. We have Beanie, as I just called out, an asynchronous Python object document mapper for MongoDB based on Motor and Pydantic, of course.
So GoodConf, as in GoodConfig, GoodConf is a thin wrapper over Pydantic settings management. So that's pretty cool. Get a little bit of a cleaner or more opinionated way to work with settings. And, of course, FastAPI, Django Ninja are listed here along with some others. And I think I'll probably leave it there with that as the shout out for all those items.
But there's a bunch of cool stuff in here. Awesome things you might say that people can check out. And also, if you have one of these projects or you're a super fan of one of these projects and it's not listed here, PRs are accepted, I believe. Yeah. Cool. Yeah. So that was the next thing in the Pydantic journey of today.
And I think it's also really cool that Simon does a shout out to Seth Larson. Hey, Seth. That Seth pointed out that GZip, the Python GZip module, you can use it as a CLI tool, especially handy if you're on Windows and don't have GZip built in. You can say Python dash M GZip. And you can with a --decompress and give it a GZip file and it'll decompress it for you.
There's quite a few Python modules within standard library or packages, modules, that do these init things or have a command line thing. And so what's kind of interesting is what he's looking for. He's looking for packages that have a if name equals main because that little trick of if to under name equals to under main, that means that if you get if it's imported, it just use it runs like a library.
But if it's not if it's not imported, if it's run directly, then this will be true. And whatever's in there will run as as a command and you can do the dash M thing. And so one of the few fun things that he found, I did know about the server thing, but I always forget about it. Python dash M HTTP dot server and you can give it a port.
Yeah, that's nice. I use that sometimes when things complain and trying to work with like JavaScript and a static HTML file or something. It's like you need a server for this to work because it wants to just do forward slash something. So I'll just run that and then like open up the file right there.
And then it then it starts working instead of trying to get a lot of trouble. Run that in the directory. You're working on it. Yeah. And if you if you want to one of the fun things with this is if you want to just learn a little basic HTML, maybe JavaScript in CSS, you can just use this as a local server to just play around with using all three together.
This is super handy. I didn't know about this. Jason.tool. It's a pretty printer for Jason. So you can just pipe some Jason into it and get some nice formatted output. Kind of nice. Yeah, that's really what I wanted to highlight. Those calendar. I knew about this a long time ago and I totally forgot about it.
Python dash M calendar will print an ASCII calendar of the current year. Why would you need this? But anyway. Because you can, Brian. Because you can. Because you can. Because you can. I used this way, way, way long ago to try to and then parsed it afterwards to hook up a dynamic calendar application with clickable dates.
Interesting. Guess you want to read from 1994. So one thing I think would. I think it might be kind of cool if somebody had. Creates a meta package for this kind of stuff based on this article. What I mean is if you set an entry point in your Python package and you put it into. You install it into your virtual environment or you pip x install it and it becomes a CLI command.
Right. So, for example, pyjoke. Right. I could just go up here and type pyjoke. Oh, pip x install pyjoke first. And it'll give you some kind of. It'll just call that function. Right. Is it jokes perhaps? I'm not sure. Yeah. So if I type pyjoke after installing a pip x install, it says obfuscated reality mappers or items can be useful database tools.
Right. So. Right. But the fact now I have a single command I give instead of, you know, Python dash m pyjoke, I just type pyjoke. Right. So it would be cool if somebody took this and created something that I could pip x install that gave me every single one of these in a more concise way to call it.
You might need to go up with a pg zip or something right like that, which is would be a little bit annoying. But yeah, anyway, it'd be fun if somebody turned these into like a simpler series of commands and just like you got Python, you got you basically have all these. Yeah, it might be kind of difficult.
Thanks, Simon. Thank you, Seth. This is cool. All right. Well, those are our things. So do you have anything extra to share with us? I do have some extras. Back to Pydantic. So over at Talk Python Training, we have a brand new course, a seven hour course for MongoDB called MongoDB with Async Python.
There's tons of stuff to learn here. People can check it out if they're interested in this. And as I said, I was just talking to Roman and Beanie should get its Pydantic version to upgrade as well. So this should only get to be a better story as we go on. Very cool. Now, is this one that you did or was this somebody else?
This is me. Okay. And again, this is like what's powering Python by stat.fm. This is what's powering talkpython.fm. Like those kinds of, it's the same tech behind the scenes. So pretty excellent. Nice. Another one. Did you know that PyPI has a blog? No. It does. This one's been sitting around for a little bit.
I meant to talk about it, but it kept getting edged out by more urgent things. But back in March, end of March, there's now a blog.pypi.org that you can go to. So definitely check that out. One of the things that's a little bit of a hassle is if you take this, like blog.pypi.org, and you drop it into your RSS reader, like reader with two E's for me, it says there's no blog here.
Is there no blog here? Go to the bottom. There's a blog.pypi.org slash feed underscore RSS underscore created dot XML. That is what you got to put into your RSS reader if your RSS reader doesn't find it. Because there's no meta tag for the RSS capabilities. Maybe someone's over there listening, go and throw that in, make that a little bit quicker and simpler for people to find.
But if you want to subscribe to it, that's the business right there. Well, if you go to scroll, yeah, there is a little icon at the bottom right-hand corner, a little RSS thing. So all the way down. Yeah, right there. Yeah. So copy link. Is that the same address? Yeah, that's the same address I suggested.
Oh my gosh, there's a lot of pop-ups on Twitter these days. On Twitter, Lucas Schlinger pointed out, says, wow, meta, as in Facebook, meta commits to dedicate three engineering years to implement the removal of the GIL from Python and fix upcoming compatibility and performance issues as a result of that.
Oh, awesome. How about that? So I believe, I believe this is the, 703 is the no-kill work done by Sam Gross, who is at meta. And there's been some hesitation of like, well, if we adopt this, how much things that are based on C and Rust start breaking? And so it sounds like, you know, with a couple of paragraphs of information that they're saying, look, we'll both fix C-Python.
Just do it anyway and we'll fix it later. Well, I think it's like, we'll fix C-Python. And then maybe, maybe I'm inferring here is like, and we'll fix the popular packages based on it that might run into trouble. I'm not sure. But yeah, so basically three, three years, I think they were saying one to two years of actual calendar time, but with multiple people who are either core developers or very close to core developers to make this happen.
Cool. Yeah. So that's big news. And those are my extras. Nice. I wanted to say congrats to Seth Larson. So we mentioned him at the beginning of the show and a couple of times during the show. Seth is the new security developer in residence. So yay. Congrats, Seth. So there's a, we're linking to an announcement on the PSF blog.
And he also has a, oops, that's blank. He has a blog announcing the change also, a blog post. So pretty cool. I'm excited to see that. So congrats, Seth. Yeah. Congrats, Seth. And there's the, you know, there's all this discussion about supply chain vulnerabilities and security and, and all of those things.
And, I used to like mostly just want the tech stuff off a tech podcast, but I kind of really liked the first part of talk Python where you just like get to know people. and I thought we should do more of that. So, I wanted to, the, wanted to get a podcast together where we mostly focus on the people and less about the tech.
So, that's what Python people is about. Yeah. We talked for like 45 minutes or something, no, 28 minutes and 44 seconds. And we almost don't talk about Python at all. Right. Yeah. A little bit. Yeah. A little bit about what you do. so I thought it'd be fun to like pull in, like why, I mean, everybody knows who you are, but for some people, like, what is your connection to Python?
What is your connection to Python community? But then also just more about you. Like we learned to have about your motorcycling and about, about being a pool shark. Who knew? Who knew? Who knew? And we talk about skateboarding with you. So there's like a ton of other, other cool things that people might be interested in.
Oh yeah. We have a joke. We have a joke. So this is part of the, the, Oh, really book series. you know, O'Reilly always has like an animal or something on there. And so there's the joke series. Oh, really? Question mark. It's kind of, a riffs on that a little bit. This one is Kubernetes for beginners.
It says, what could go wrong? And this is in the dev oops category of tech books. Yeah. And then the cover is, it says containers that'll fix it. And it has that giant evergreen container ship stuck sideways in the Suez canal. What could go wrong? Yeah. And just the, just the title Kubernetes for beginners.
No, beginners aren't there yet. So yeah. Yeah. They probably shouldn't be doing Kubernetes, but that'll fix it. Yeah. Awesome. All right. Then you got a joke for us as well, right? Oh yeah. Just a, just a random one I heard the other day. I thought it was terrible. So I thought I'd share it with the world.
So five ants rent an apartment together. They invite five other ants to share the rent. Also, they got tons of room. So now they're tenants. Ah, tenants. Got it. I love it. All right. I got one more in that vein for you since I just installed Pi joke there. Okay. Single key, single word on the CLI for me.
