Latest / Talk Python To Me / #313: Automate your data exchange with Pydantic
Transcript
- 0:00Data validation and conversion is one of the truly tricky parts of getting external data into your app.
- 0:05This might come from a REST API or a file on disk or somewhere else.
- 0:09It includes checking for required fields, the correct data types, converting from potentially compatible types,
- 0:16for example, from strings to numbers if you have quote 7 but not the value 7, and much more.
- 0:21Pydantic is one of the best ways to do this in modern Python,
- 0:24using data class-like constructs and type annotations to make it all seamless and automatic.
- 0:30We welcome Samuel Colvin, creator of Pydantic, to the show.
- 0:34We'll dive into the history of Pydantic and its many uses and benefits.
- 0:37This is Talk Python To Me, episode 331, recorded April 14th, 2021.
- 0:43Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the ecosystem, and the personalities.
- 1:02This is your host, Michael Kennedy.
- 1:04Follow me on Twitter where I'm @mkennedy,
- 1:07and keep up with the show and listen to past episodes at talkpython.fm,
- 1:10and follow the show on Twitter via at Talk Python.
- 1:13This episode is brought to you by 45 Drives and us over at Talk Python Training.
- 1:18Please check out what we're offering during those segments.
- 1:21It really helps support the show.
- 1:22Hey, Samuel.
- 1:23Hi, Mike. Great to meet you. Very excited to be here.
- 1:25Yeah, I'm really excited to have you here.
- 1:27You've been working on one of my favorite projects these days that is just super, super neat,
- 1:31solving a lot of problems, Pydantic, and I'm really excited to talk to you about it.
- 1:36Yeah, as I say, it's great to be here.
- 1:37I've been doing Pydantic on and off now since 2017,
- 1:41but I guess it was when Sebastian Ramirez started using it on FastAPI a couple of years ago.
- 1:46A couple of years ago, I don't know when, that it really went crazy.
- 1:49And so now it's a lot of work, but it's exciting to see something this popular.
- 1:53I'm sure it's a lot of work, but it really seems to have caught the imagination of people
- 1:58and really excited about it.
- 1:59So fantastic work.
- 2:01Before we get into the details there, let's start with just your background.
- 2:06How did you get into programming Python?
- 2:07I did a bit of programming at university, a lot of, a bit of MATLAB and a bit of C++.
- 2:11And then my first job after university, I worked on oil rigs in Indonesia, of all strange things.
- 2:17There's a lot of time on an oil rig when you're flat out, and there's a lot of other time when
- 2:22you're doing absolutely nothing, don't have much to do.
- 2:24And so I programmed quite a lot in that time.
- 2:26And I suppose that was when I really got into loving it rather than just doing it when I had to.
- 2:29And then I guess Python from there, a bit of JavaScript, a bit of Python ever since.
- 2:33Yeah, really cool.
- 2:34I think things like MATLAB often do sort of pull people in and they have to learn a little bit
- 2:39of programming because it's a pain to just keep typing it into whatever the equivalent of the
- 2:43REPL is that MATLAB has.
- 2:45You do the .imp file and you kind of get going and you start to combine them.
- 2:49And then all of a sudden, it sort of sucks you into the programming world when you maybe
- 2:52didn't plan to go that way.
- 2:54I worry maybe other people learn it the right way and sit down and read a book and understand
- 2:58how to do stuff.
- 2:58But there's a lot of things that I wish I had known back then that I learned only through
- 3:02reading other code or through banging my head against the wall that would have been really
- 3:06easy to learn if someone had showed me them.
- 3:08But hey, we got here.
- 3:09Yeah, I think many, I'd probably say most people got into programming that way.
- 3:13And I think it's all fine.
- 3:14Yeah, it's all good.
- 3:15So what was living on an oil rig like?
- 3:18That must have been insane.
- 3:19It was pretty peculiar.
- 3:21So half the time I was on land rigs and half the time I was offshore.
- 3:24Offshore, the conditions were a lot better.
- 3:26Land rigs, the food was pretty horrific.
- 3:28And a lot of they were all, I always did like a 14 hour night shift and then occasionally
- 3:33had to do the day shift as well and then go on to the night shift.
- 3:35So sleep was a problem and the heat was a problem.
- 3:38Yeah, it was hard baptism of the real working world after university, I must say.
- 3:43Yeah, I guess so.
- 3:43It sounds really interesting.
- 3:46Not an interesting like, wow, I really want to necessarily go out there and do it.
- 3:49But you just must have come away with some like some wild stories and different perspective.
- 3:54Yeah, lots of stories that I won't retell now.
- 3:56I don't think that's the kind of subject matter you want on your podcast.
- 3:59But yeah, I mean, how oil rigs work, well, whatever you think about it and however much
- 4:03we all want to get away from them is crazy what you can do.
- 4:06And so being like up against the at the coalface of that was really fascinating.
- 4:10It's kind of like aerospace, but no one minds crashing.
- 4:12So you can innovate.
- 4:14You can try a new thing.
- 4:15You just the faster you can drill, the better.
- 4:17It's all that anyone cares about.
- 4:18Yeah.
- 4:18Well, most people what they care about.
- 4:20Yeah, it must have been a cool adventure.
- 4:21Awesome.
- 4:22So I thought it'd be fun to start our conversation, not exactly about Pydantic, but just about this
- 4:27larger story that larger space that Pydantic lives in.
- 4:33But maybe to set the stage, give us like a real quick overview of what problem does Pydantic
- 4:38solve for the world?
- 4:38What is it and what does it solve for the world?
- 4:40So the most simplest way of thinking about it is you've got some user somewhere.
- 4:44They might be on the other end of a web connection.
- 4:46They might be using a GUI.
- 4:48They might be using a CLI.
- 4:50It doesn't really matter.
- 4:51And they're entering some data.
- 4:52And you need to trust the data that you that they put in is correct.
- 4:57So with Pydantic, I don't really care what someone enters.
- 5:00All I care about is that you can validate it to get what you want.
- 5:04So if I need an integer and a string and a list of bytes, all I care about is that I can
- 5:10get to that integer, string, and list of bytes.
- 5:12I don't care if someone entered bytes for the string field as long as I can decode it.
- 5:16Or if they entered a tuple instead of a list.
- 5:18But yeah, the fundamental problem is people, I suppose in theory, like intentionally trying
- 5:23to do it wrong, but mostly innocently getting something wrong, trying to coerce data into
- 5:27the shape that you want it to be in.
- 5:29Yeah, it's super easy to say like, well, I read this value from a file and the value
- 5:34in the file is a number.
- 5:36It says one, but you read it as strings.
- 5:39So the thing you sent over is quote one, not the number one.
- 5:42And in programming, computers hate that.
- 5:45They don't think those things are the same unless maybe your JavaScript and you do it in
- 5:49the right order, maybe.
- 5:50But excluding that odd case, right?
- 5:53A lot of times it's just crash wrong data format or whatever.
- 5:56We expected an integer and gave us a string.
- 5:58But, you know, Pydantic just looks at it and says, it could be an integer if you want it
- 6:02to be.
- 6:02So we'll just do that.
- 6:04Yeah.
- 6:04And I think it's something that there's been a lot of discussion about on Pydantic's issue
- 6:09tracker.
- 6:09There's an entire label strict about the discussions about how strict we should be.
- 6:13And I definitely, I think it's fair to say Pydantic compared to its, the other libraries
- 6:17is more lenient, more keen on if we can try and coerce it into that type, we will.
- 6:22That really started for me trying to make it simple and performant.
- 6:25And I just called the, I decided if something wasn't in by calling int on it and seeing what
- 6:29happened.
- 6:29That had some kind of strange side cases because I called list on something to see if it was
- 6:35a list.
- 6:35And that meant you could put a string of integers, a string into a list of ints field.
- 6:40And it would first of all, call list on it, turn it into a list and then call int on each
- 6:44member.
- 6:45And that was completely crazy.
- 6:46So we've got stricter over the years.
- 6:48And I think that in the future, Pydantic will have to get a bit stricter in particular stuff
- 6:52like coercing from a float to an int quite often doesn't make sense or isn't what people
- 6:57want.
- 6:57But most of the time, just working is really powerful, really helpful.
- 7:01If there's going to be some kind of data loss, right?
- 7:03If it's 1.0000, coercing that to one's fine.
- 7:06If it's 1.52, maybe not.
- 7:09Yeah, but it's difficult because we're in the Python world where Python is pretty lenient.
- 7:13So it doesn't mind you adding two floats together.
- 7:15If you call the int function on a float, it's fine.
- 7:20So it's also trying to be Pythonic at the same time as being strict enough for people, but
- 7:24without doing stuff that obviously, without not doing stuff that's obvious, because lots
- 7:29of people do just want coercion to work.
- 7:31Right.
- 7:31We'll get into it.
- 7:32There's lots of places to plug in and write your own code and do those kinds of checks if
- 7:36you really need to.
- 7:36Yeah.
- 7:37All right.
- 7:37Carlos has a good question out there in the live stream, but I'm going to get to that later
- 7:42when we get a little bit more into it.
- 7:43First, let's talk about some of the other things.
- 7:45So Pythonic is surprisingly popular these days, but there's plenty of people, I'm sure, who
- 7:51haven't heard of it, who are hearing about it the first time here.
- 7:54And there's other libraries that try to solve these same type of problems, right?
- 7:59Problem is I've got data in often a dictionary list mix, right?
- 8:04Like a list of dictionaries or a dictionary, which contains lists, which contains like that
- 8:08kind of object graph type of thing.
- 8:11And I want to turn that into probably a class, probably a Python class of some sort or understand
- 8:17it in some way.
- 8:18So, you know, we've got some of the really simple ways of doing this where I just have
- 8:23those.
- 8:23I want to stash that into a binary form, which is pickle.
- 8:26Pickle has been bad in certain ways because it can run arbitrary Python code.
- 8:31That's not ideal.
- 8:31There's another related library called quickle, which is like pickle, but doesn't run arbitrary
- 8:37code.
- 8:37And that's pretty nice.
- 8:38We have data classes that look very much like what you're doing with Pydantic.
- 8:42We have Marshmallow, which I hear often being used with like Flask and SQLAlchemy and Marshmallow.
- 8:48You may want to just sort of give your perspective on like what the choices are out there and where
- 8:53things are learning from other libraries and growing from.
- 8:55Yeah, I put serialization as in pickle, JSON, YAML, TOML, all of them into a different category
- 9:01message pack as a slightly different thing from taking Python objects and trying to turn
- 9:06them into classes.
- 9:07So putting them to one side, because I think that's a kind of different problem that Pydantic
- 9:12and Marshmallow and people aren't trying to solve exactly.
- 9:14Then there's data classes, which are the kind of canonical standard library way of doing this.
- 9:18They're great, but they don't provide any validation.
- 9:20So you can add a type in that says age is an integer, but data classes don't care.
- 9:27Whatever you put in will end up in that field.
- 9:29And so that's useful if you have a fully type-inted system where you know already that something's
- 9:34an integer before you pass it to a data class.
- 9:36But if you're loading it from a foreign source, you often don't have that certainty.
- 9:40And so that's where libraries like Pydantic and Marshmallow come in.
- 9:43Actually, Pydantic has a wrapper for data classes.
- 9:47So you basically import the Pydantic version of data classes instead of normal data classes.
- 9:52And from there on, Pydantic will do all the validation, give you back a completely standard
- 9:56data class.
- 9:56Having done the validation.
- 9:58Marshmallow is probably, well, it is undoubtedly the biggest, most obvious competitor to Pydantic.
- 10:04And it's great.
- 10:05I'm not going to sit here and bad mouth it.
- 10:07It's been around for longer and it does a lot of things really well.
- 10:10Pydantic has just overtaken a few months ago Marshmallow in terms of popularity, in terms
- 10:16of GitHub stars.
- 10:17Whether you care about that or not is another matter.
- 10:19There's also Atras, which kind of predates data classes and is closer to data classes.
- 10:24But the big difference between Pydantic and Marshmallow and most of the other competitors
- 10:29is Pydantic uses type hints.
- 10:31So that one means you don't have to learn a whole new kind of micro language to define
- 10:36types.
- 10:36You just write your classes and it works.
- 10:39It works with mypy and with your static type analysis.
- 10:43It works with your IDE, like, well, Pycharm now, because there's an amazing extension.
- 10:48I forgot the name of the guy who wrote it, but there's an amazing extension that I used the
- 10:52whole time with Pycharm that means it works seamlessly with Pydantic.
- 10:56And there's some exciting stuff happening.
- 10:58Microsoft, they emailed me actually two days ago.
- 11:00One of their technical fellows about extending their language server or their front-end for
- 11:06language server, Pyright, to work with Pydantic and other such libraries.
- 11:10So because you're using standard type hints, all the other stuff, including your brain, should,
- 11:14in theory, click into place.
- 11:16Yeah, that's really neat.
- 11:17I do think it makes sense to separate sort of the serialization file, save me a file,
- 11:21load a file type of thing out.
- 11:23I really love the way that the type hints work in there because you can almost immediately
- 11:28understand what's happening.
- 11:29It's not like, oh, this is the way in which you describe the schema and the way you describe
- 11:34the transformations.
- 11:35It's just, here's a class.
- 11:37It has some fields.
- 11:38Those fields have types.
- 11:39That's all you need to know.
- 11:40And Pydantic will make the magic happen.
- 11:42What would you say the big difference between Pydantic and Marshmallow is?
- 11:46And I haven't used Marshmallow that frequently, so I don't know it super well.
- 11:50I would first give the same proviso.
- 11:52I haven't used it that much either.
- 11:53I probably, if I was more disciplined, I'd have sat down and used it for some time before
- 11:58building Pydantic.
- 11:58But that's not always the way things work.
- 12:00The main difference is it doesn't use type hints or it doesn't primarily use type hints
- 12:04as its source of data about what type something is.
- 12:07Pydantic is around, from my memory, I can check, but significantly more performant than Marshmallow.
- 12:14Yeah, you actually have some benchmarks on the site and we can talk about that in a little
- 12:17bit and compare those.
- 12:18Yeah.
- 12:19Yeah.
- 12:19So just briefly, it's about two and a half times faster.
- 12:22The advantage of Marshmallow at the moment is it has more logic around customizing how
- 12:29you serialize types.
- 12:30So when you're going back from a class to a dictionary or list of dictionaries and then
- 12:35out to JSON or whatever, Marshmallow has some really cool tools there, which Pydantic doesn't
- 12:42have yet.
- 12:42And I'm hoping to build into V2 some more powerful ways of customizing serialization.
- 12:47Okay.
- 12:47Fantastic.
- 12:48This portion of Talk Python is brought to you by 45 Drives.
- 12:5345 Drives offers the only enterprise data storage servers powered by open source.
- 12:59They build their solutions with off-the-shelf hardware and use software-defined open source
- 13:04designs that are unmatched in price and flexibility.
- 13:06The open source solutions 45 Drives uses are powerful, robust, and completely different.
- 13:12And best of all, they come with zero software licensing fees and no vendor lock-in.
- 13:1845 Drives offers servers ranging from four to 60 bays and can guide your organization through
- 13:23any sized data storage challenge.
- 13:26Check out what they have to offer over at talkpython.fm/45 Drives.
- 13:31If you get in touch with them and say you heard about their offer from us, you'll get a chance
- 13:35to win a custom front plate.
- 13:36So visit talkpython.fm/45 Drives or just click the link in your podcast player.
- 13:44Let's dive into it.
- 13:46And I want to talk about some of the core features here.
- 13:50Maybe we could start with just you walking us through a simple example of creating a class
- 13:56and then taking some data and parsing over.
- 13:59And you've got this nice example right here on the homepage.
- 14:01I think this is so good to just sort of look.
- 14:03There's a bunch of little nuances to cool things that happen here that I think people will benefit from.
- 14:08Yeah, so you're obviously defining your class user here.
- 14:11Very simple inheritance from base model, no decorator.
- 14:16I thought about the beginning that like this should work for people who haven't been writing
- 14:20Python for the last 10 years and where decorators look like strange magic.
- 14:24I think using inheritance is the obvious way to do it.
- 14:27And then obviously we define our fields.
- 14:30The key thing really is that the type int in the case of ID is used to define what type that
- 14:36field is going to be.
- 14:37And then if we do give it a value as we do with name, that means that the field is not required.
- 14:42It has a default value.
- 14:43And obviously we can infer the type there from the default, which is a string.
- 14:46Then sign up timestamp is obviously an optional date time.
- 14:50So it can be none.
- 14:51And critically here, you could either enter none or leave it blank.
- 14:55And it would again be none.
- 14:58And then we have friends, which is a more complex type.
- 15:00That's a list of integers.
- 15:02And the cool stuff is because we're just using Python type int, we can burrow down into lists
- 15:06ofdicts, of lists, of sets, of whatever you like within reason.
- 15:10And it will all continue to work.
- 15:12And then looking at the external data, again, we see a few things like we were talking about
- 15:15the coercion.
- 15:16Right.
- 15:16This external data is just a dictionary that you probably have gotten from an API call,
- 15:21but it could have come from anywhere.
- 15:22It doesn't have to come from there.
- 15:23Right.
- 15:23Exactly.
- 15:24Anywhere outside.
- 15:25But right now we've got it as far as being a dictionary.
- 15:27So the point here is we're doing a bit of coercion.
- 15:31So trivial converting from a string 123 to the number 123, but then a bit more complex,
- 15:39parsing the date and converting that into a date object.
- 15:43Right.
- 15:43So you have the, in here, the data that's passed in, you've got a quote, 2019-06-01 and
- 15:50a time.
- 15:51And this is notoriously tricky because things like JSON don't even support dates.
- 15:56Like, they'll freak out if you try to send one over.
- 15:58So you just got this string, but it'll be turned into a date.
- 16:01Yeah.
- 16:02And we do a whole bunch of different things to try and do all of the sensible date formats.
- 16:06There's obviously a limit as how far to go, because one of the things Pedantic can do is
- 16:11it can interpret integers as dates using Unix timestamps.
- 16:15And if they're over some threshold in about two centuries from now, it assumes there are
- 16:20milliseconds.
- 16:20So it works with Unix milliseconds, which are often used, but it does also lead to confusions
- 16:26when someone puts in one, two, three as a date and it's three seconds after 1970.
- 16:31There are like, there's an ongoing debate about exactly like what you should try and coerce
- 16:35and when it gets magic.
- 16:36But for me, it's, there's a number of times I've just found it incredibly useful to, that
- 16:41it just works.
- 16:42So for example, the string format that Postgre uses when you use to JSON just works with Pedantic.
- 16:49So you don't even have to think about whether that's come through as a date or as a string
- 16:52until you're worried about a limit of performance.
- 16:54Most of that stuff just works.
- 16:56Yeah.
- 16:56Then one of the things that I think is super interesting is you have these friend IDs that
- 17:01you're passing over.
- 17:02And you said in Pedantic, it gets a list of integers.
- 17:05And in the external data, it is a list of sometimes integers and sometimes strings.
- 17:11But once it gets parsed across, it not just looks at the immediate fields, but it looks at,
- 17:17say, things inside of a list and says, oh, you wanted a list of integers here.
- 17:20This, this list, it has a string in it, but it's like quote three.
- 17:23So it's fine.
- 17:24Yeah.
- 17:24And this is where, where it gets cool because we can go recursively down into the rabbit hole
- 17:29and it will continue to validate.
- 17:31One of the tricky things that I think is what most people want, but where our language fails us is about the word validation.
- 17:38Because quite often validation sounds like I'm checking the input data is in the form that I said.
- 17:43That's kind of not what Pedantic is doing.
- 17:45It's optimistically trying to parse.
- 17:47It doesn't care what the list contains in a sense, as long as it can find a way to make that into an int.
- 17:52So this wouldn't be a good library to use for, for like unit testing and checking that something is the way that it should be,
- 17:58because it's going to accept a million different things.
- 18:01It's going to be as lenient as possible in what it will take in.
- 18:04But that's by design.
- 18:05Yeah.
- 18:05The way to take this external dictionary and then get Pedantic to parse it is you just pass it as keyword arguments to the class constructor.
- 18:16So you say user of star star dictionary.
- 18:20So that'll, you know, explode that out to keyword arguments.
- 18:23And that's it.
- 18:24That runs the entire parsing, right?
- 18:26That's super simple.
- 18:27Yeah.
- 18:27And that's, again, by design to make it just the simplest way of doing it.
- 18:30If you want to do crazy complex stuff like constructing models without doing validation,
- 18:35because you know it's already been validated, that's all possible.
- 18:38But the simplest interface, just calling in it on the class is designed to work and do the validation.
- 18:44Yeah, cool.
- 18:45One thing I think is really neat and not obvious right away is that you can nest these things as well, right?
- 18:51Like I could have a shopping cart and the shopping cart could have a list of orders and each order in that list could be a Pedantic model itself, right?
- 18:58Exactly.
- 18:59And it's probably an open question as to how complex we should make this first example.
- 19:03Maybe it's already too complicated.
- 19:04Maybe it doesn't demonstrate all the power.
- 19:06But yeah, I think it's probably about right.
- 19:08But yeah, you can go recursive.
- 19:10You can even do some crazy things like the root type of a model can actually not itself
- 19:15be a sequence of fields.
- 19:16It can be a list itself.
- 19:17So there is a long tail of complex stuff you can do.
- 19:21But you're right.
- 19:21The inheritance of different models is a really powerful way of defining types.
- 19:26Because in reality, our models are never a nice key in value of different types.
- 19:31They always have complex items deeper down.
- 19:34Right.
- 19:34That's really cool.
- 19:35And on top of these, I guess we can add validation.
- 19:38Like you have the, you said, the very optimistic validation.
- 19:41Like if in the friends list, it said one, two, comma, Jane.
- 19:46Well, it's probably going to crash when it tries to convert Jane into an integer and say, no, no, no,
- 19:51this is wrong.
- 19:52And it gives you a really good error message.
- 19:53It says something like the third item in this list is Jane.
- 19:57And that's not an integer, right?
- 19:58It doesn't just go, well, data is bad.
- 20:00You know, too bad for you.
- 20:02Yeah.
- 20:02But you could decide, perhaps Jane's a strange case.
- 20:05But if you wanted the word one, O-N-E, to somehow work, you could add your own logic relatively
- 20:10trivially via a decorator, which said, okay, cool.
- 20:13If we get the word one or the word two or the word three, then we can convert them to the equivalent
- 20:18integers.
- 20:19And that's relatively easy to add.
- 20:21Yeah.
- 20:21Cool.
- 20:22Now, one thing that I guess the scenario where people run into this often run into working
- 20:27with Pydantic is it's sort of the exchange layer for FastAPI, which is one of the more
- 20:32popular API frameworks these days.
- 20:34And you just say your API function takes some Pydantic model and it returns some Pydantic model
- 20:40and, you know, magic happens.
- 20:42But if you're not working in that world, if you're not in FastAPI and most people doing
- 20:47web development these days are not because, you know, it's the most popular framework and
- 20:51it's also certainly not the most popular legacy framework.
- 20:54So you can do things like create these models directly in your code, let it do the validation
- 21:01there.
- 21:02And then if you need to return the whole object graph as a dictionary, you can just say model.dict,
- 21:07right?
- 21:07Yeah.
- 21:08And also model.json and that will take it right out to JSON.
- 21:11As a string.
- 21:11Yeah.
- 21:12So it will turn a string of that JSON.
- 21:13Yeah.
- 21:14And you can even swap in more performant JSON encoders and decoders.
- 21:18And this is why I was talking about like there's already some power in what you can do here and
- 21:22which fields you can exclude.
- 21:24And it's pretty powerful.
- 21:25But like, as I say, something in V2 that we might look at to make even more powerful is customizing
- 21:32how you do this.
- 21:32Nice.
- 21:33You might expect to just call .dict and it converts it to a dictionary and reverses it,
- 21:38if you will.
- 21:38But there's actually a lot of parameters and arguments you can pass in to have more control.
- 21:43Do you want to talk about maybe just some of the use cases there?
- 21:47Yeah.
- 21:47As we see here, you can choose to include specific fields and the rest will be excluded
- 21:52by default.
- 21:52You can say exclude, which will exclude certain fields and the rest will be included by default.
- 21:57Those include and exclude fields can do some pretty ridiculously crazy logic going recursively
- 22:03into the models that you're looking at and excluding specific fields from those models or specific
- 22:10items from lists, even some of the code.
- 22:12I forget who wrote that, but like I wrote the first version of it.
- 22:14I wrote the second or third version of it.
- 22:16And I was looking through about the 10th version of it the other day.
- 22:19And it is some of the most complex bits of Pydantic.
- 22:22But that's amazingly powerful.
- 22:24And then we didn't talk about aliases, but you can imagine if you were interacting with the
- 22:28JavaScript framework, you might be using camel case on the front end for like user names,
- 22:33name with a capital N, but have user underscore name in Python world where we're using underscores.
- 22:39We can manage that by setting aliases on each field.
- 22:42So saying that the user underscore name field has the alias user underscore N name.
- 22:47And then we can obviously export to a dictionary using those aliases if we wish.
- 22:52Oh, that's cool.
- 22:52So you can program Pydonic style classes, even if you're consuming, say, a Java or C#
- 23:00API that clearly uses a different format or style for its naming.
- 23:04Exactly.
- 23:04And you can then export again to back to those aliases when you want to go on to like pipe
- 23:12it on down your data processing flow or however you're going to do it.
- 23:15And in fact, one of the things that will come up in future will be different aliases for import
- 23:19on the inside way and the out way.
- 23:21But until so far, there's just one, but that's powerful.
- 23:24And then you can exclude fields that have defaults.
- 23:28So if you're trying to save that data to a database and you don't want to save more than
- 23:32you need to, you can exclude stuff where it has the default value.
- 23:35And you can exclude fields which are none, which again is often the same thing, but there
- 23:40are subtle cases where those are different requirements.
- 23:42Yeah.
- 23:42And that also might really just, even if you're not saving it to a database, you know, that
- 23:46will lower the size of the JSON traffic between say microservices or something like that.
- 23:51If you don't need to send the defaults over, especially the nones, because they're probably
- 23:55going to go to the thing they get and go, give me the value or none in whatever language
- 24:00they're using, right?
- 24:01Something to that effect.
- 24:02It'll mean the same.
- 24:03Yeah.
- 24:04So we have complex stuff here.
- 24:05So we have exclude unset and exclude defaults.
- 24:09So you can decide exactly how you want to exclude it to get just the fields that are actually live,
- 24:14as it were, that have custom values.
- 24:15Yeah.
- 24:16So you have this dict thing, which will turn a dictionary and then you have the JSON one,
- 24:20which is interesting.
- 24:22We talked about that one.
- 24:23It also, you also have copy.
- 24:24So you, is that like a shallow copy if you want to clone it off or something?
- 24:29It can be a deep copy, but you can update some fields on the way through.
- 24:33So there are multiple different contexts where you might want to do that, where you want
- 24:36different models where you can go and edit one of them and not damage the other one,
- 24:40or you want to modify a model as you're copying it.
- 24:43We also have a setting on config that prevents you, that makes fields pseudo immutable.
- 24:49So it doesn't allow you to change that value.
- 24:52It doesn't stop you changing stuff within that value because there's no way of doing that in Python.
- 24:55But that's another case where you might want to use copy because you've set your model up to be
- 24:58effectively static and you want to create a new one.
- 25:01If you were being super careful and strict, you would say, I'm never going to modify my model.
- 25:05I'm just going to copy it when I want a new one.
- 25:07Yeah.
- 25:07Perfect.
- 25:08Yeah.
- 25:08Certain things you could enforce that they don't change like strings and numbers.
- 25:11But if it's a list, like the list can't point somewhere else, but what's in the list,
- 25:15you can't really do anything about that, right?
- 25:16Yeah.
- 25:17So another thing that I think is worth touching on that's interesting is there's some obvious
- 25:21things that are the types that can be set for the field types.
- 25:26And then the data exchange conversion types, bools, integers, floats, strings, and so on.
- 25:31But then it gets further, it gets more specialized down there as you go.
- 25:36So for example, you can have unions, you can have frozen sets, you can have iterables,
- 25:42callables, network addresses, all kinds of stuff, right?
- 25:46Yeah.
- 25:46We try and support almost anything you can think of from the Python standard library.
- 25:50And then if you go on down, we get to a whole bunch of things that aren't supported
- 25:54or don't have an obvious equivalent within the standard library, but where you go on
- 25:59down to Pydantic types on the right.
- 26:02So where you're looking is in the example, but Pydantic types, then we get into things
- 26:07that don't exist within, or there isn't an obvious type for in Pydantic, in Python.
- 26:13There's a kind of subtle point here that like often these types are used to enforce more
- 26:19validation.
- 26:19So what's returned is an existing Python type.
- 26:23So here we have file path, which returns a path, but it just guarantees that that file
- 26:28exists.
- 26:28And directory path, similarly, is just a path, but it will confirm that it is a directory.
- 26:32And email string just returns you a string, but it's validated that it's a legitimate email
- 26:37address.
- 26:38But then some do more complex stuff.
- 26:40So name email will return you, allow you to split out names and email addresses and more
- 26:45complex things.
- 26:46Got credit cards, colors, URLs, all sorts of good stuff there.
- 26:51Yeah.
- 26:51Yes.
- 26:51So there's a Mexican bank called, excuse my pronunciation, Cavenka, who use the credit card
- 26:57fields for all of their validation of credit card numbers.
- 27:00Yeah.
- 27:00It's super interesting.
- 27:01And you think about how do you do this validation?
- 27:03How do you do these conversions yourself?
- 27:04And not only do you have to figure that out, but then you've got to do it in the context of
- 27:09like a larger data conversion.
- 27:10And here you just say, this field is a type email and it either is an email or it's going
- 27:16to tell you that it's invalid.
- 27:17Right.
- 27:17Yeah.
- 27:17I like this a lot.
- 27:18You know, one thing that we've been talking about that's pretty straightforward is using
- 27:22this for APIs, right?
- 27:24Like somebody is doing a JSON post and you get that as a dictionary in your web framework,
- 27:29and then you kind of validate it and convert it and so on.
- 27:31But it seems like you could even use this for like web forms and other type of things,
- 27:37right?
- 27:37Somebody's submitting some kind of ATP post of a form and it comes over if you want to say
- 27:42these values are required, this one's an email and so on.
- 27:45Yeah.
- 27:46So all of the form validation for multiple different projects I've built, some open source, lots
- 27:51proprietary use Pydantic for form validation and use the error messages directly back to the
- 27:56user.
- 27:57I've got quite a lot of JavaScript that basically works with Pydantic to build forms and do validation
- 28:03and return that validation to the user.
- 28:05I've never quite gone far enough to really open source that and push it as a React plugin
- 28:10or something, but it wouldn't be so hard to do.
- 28:12So let me ask you this.
- 28:13And maybe when you say React, maybe that's enough of an answer already.
- 28:16But if it's a server side processing form and it's not a single page sort of front and framework
- 28:22style of form submission, often what you need to do is return back to them the wrong data,
- 28:28right?
- 28:29If they type in something that's not an email and over there it's not a number, you still
- 28:33want to leave the form filled with that bad email and that thing that's not a number.
- 28:38So you can say, those are wrong.
- 28:39Keep on typing them.
- 28:41Is there a way to make that kind of round tripping work with Pydantic?
- 28:45I've tried and haven't been able to do it.
- 28:47But if it's a front end framework, it's easy to say, submit this form, catch the error and
- 28:52show the error because you're not actually leaving the page or clearing the form with a
- 28:56reload.
- 28:57Yeah, the short answer is no.
- 28:58And it's something there's an issue about for V2 to return like the wrong value always
- 29:03in the context of each error.
- 29:04Often you kind of need it to make the error make sense.
- 29:07But at the moment, it's generally not available and we will add it in V2.
- 29:11What I've done in React is obviously you've still got the values that went into on the
- 29:15form.
- 29:15So you don't clear any of the inputs.
- 29:18You just add the errors to those fields and set the whatever it is, invalid header.
- 29:23So they're nice and red.
- 29:24Yeah, exactly.
- 29:24So if it was a front end framework like React or Vue, I can see this working perfectly as
- 29:29you try to submit it for a form.
- 29:30But if you got to round trip it with like a Flask, a Pyramid or Django form or something
- 29:35that doesn't have it, then the fact that it doesn't capture the data.
- 29:39I mean, I guess you could return back just the original posted dictionary, but yeah.
- 29:42Yeah.
- 29:42So you have the original.
- 29:43Yeah.
- 29:44Like you say, you've normally normally there.
- 29:46It's not too many different levels and it's relatively simple to combine up the dictionary
- 29:50you got straight off form submission with the errors.
- 29:53But yeah, it's you're right.
- 29:54It's something we should improve.
- 29:55Yeah.
- 29:56I'm not sure that you necessarily need to improve it because it it's mission is being fulfilled
- 30:01exactly how it is.
- 30:03And if it has this time, but well, sometimes it throws errors and sometimes it doesn't.
- 30:06And then like just report, I don't know, it seems like you could overly complicate it as
- 30:10well.
- 30:11I think that there's like a really difficult challenge in tragedy of the commons in like
- 30:15someone wants a niche feature.
- 30:17Someone else wants that feature.
- 30:19You get 10 people wanting that feature.
- 30:20You feel under overwhelming pressure to implement that feature.
- 30:23But you forget that there's, I forget, you know, there's like 6,000 people who started,
- 30:27but it's like 10, 12,000 projects that use Pydantic.
- 30:31Those people haven't asked for that.
- 30:33Do they want it or would they actively prefer that Pydantic was simpler, faster, smaller?
- 30:38Exactly.
- 30:39Right.
- 30:40Part of the beauty of it is it's so simple, right?
- 30:42I do.
- 30:43I get the value.
- 30:43I define the class.
- 30:44I star, star, take the data.
- 30:46And it's either good or it's crashed, right?
- 30:49If it gets past that line, you should be happy.
- 30:51Yeah.
- 30:52And I think that like I'm pretty determined to keep that stuff that simple.
- 30:55There are those who want to change it, who say initializing the class shouldn't do the
- 30:59validation, then you should call a validate method.
- 31:01I'm not into that at all.
- 31:03There's stuff where I'm definitely going to keep it simple and there's stuff where I'm
- 31:06really happy to add more things.
- 31:07So we were talking about custom types before.
- 31:09I'm really happy to add virtually, not virtually any, but like a lot of custom types when someone
- 31:14wants it, because if you don't need it, it just sits there and mostly doesn't affect
- 31:19people.
- 31:19Right.
- 31:19If you don't specify a column or a field of that type, it doesn't matter.
- 31:23You'll never know it or care.
- 31:24Yeah.
- 31:25Yeah.
- 31:25So there's a couple of comments in the live stream.
- 31:28And I think maybe we can go ahead and touch on them about sort of future plans.
- 31:33So you did mention that there's going to be kind of a major upgrade of V2.
- 31:37And Carlos out there asks, is there any plans for Pydantic to give support for PySpark data
- 31:43frame scheme validation?
- 31:44Or, you know, let me ask more broadly, like any of the data science world integration with
- 31:50like Pandas or other, you know, NumPy or other things like that?
- 31:53There's been a lot of issues on NumPy arrays and like validating them using list types without
- 32:00going all the way to a like Python list because that can have performance problems.
- 32:05I can't remember because it was a long time ago, but people, whoever it was, found a solution.
- 32:10And like Pydantic is used a lot now in data science.
- 32:13If you look at the projects it's used in by Uber and by Facebook, they're like big machine
- 32:18learning projects.
- 32:19Fast Facebook's fast MRI library uses it.
- 32:23Like it's used a reasonable amount in like big data validation pipelines.
- 32:27So I don't know about PySpark.
- 32:30So I'm not going to be able to give a definitive answer for that.
- 32:33If you create an issue, I'll endeavor to remember to look at it and have a look and give an answer.
- 32:38But you'll start your PySpark research project.
- 32:40Yeah.
- 32:41Nice.
- 32:41Also from Carlos really is like, what's the timeframe for V2?
- 32:45Someone joked to me the other day that the release date was originally put down as the end of March
- 32:502020 and that didn't get reached.
- 32:52And it's still, the short answer is that like, I need to, there are two problems.
- 32:56One is I need to set some time aside to sit there and build quite a lot of code.
- 33:01Second problem is the number of open PRs and the number of issues.
- 33:04I find it hard sometimes to bring myself to go and work on Pydantic when I have time off,
- 33:09because a lot of it is like the trawl of going through issues and reviewing pull requests.
- 33:13And when I'm not doing my day job of writing code, I want to like write code on something fun and not have to review other people's code.
- 33:20Because I do that for a day job quite a lot.
- 33:22So I've had like a bit of trouble getting my like back ending gear to go and like work on Pydantic because I feel like there's 20 hours of reviewing other people's code before I can do anything fun.
- 33:34And I think one of the solutions to that is I'm just going to start building V2 and ignore some pull requests and might have to break some eggs to make an omelet.
- 33:42But I think that that's better.
- 33:43Okay.
- 33:43Yeah.
- 33:44Well, and also in your defense, a lot of things were planned for March 2020.
- 33:49Yeah.
- 33:49And got ready.
- 33:50That is true.
- 33:51That is true.
- 33:51I have sat at my desk in my office for a total of about eight hours since then.
- 33:55Yeah.
- 33:56So I haven't been back to the office in London at all.
- 33:58So, so yeah, I would hope this year.
- 34:01Yeah.
- 34:01Cool.
- 34:01Talk Python To Me is partially supported by our training courses.
- 34:06Do you want to learn Python, but you can't bear to subscribe to yet another service?
- 34:11At Talk Python Training, we hate subscriptions too.
- 34:14That's why our course bundle gives you full access to the entire library of courses for one fair price.
- 34:20That's right.
- 34:21With the course bundle, you save 70% off the full price of our courses and you own them all forever.
- 34:28That includes courses published at the time of the purchase, as well as courses released within about a year of the bundle.
- 34:33So stop subscribing and start learning at talkpython.fm/everything.
- 34:39And then related to that, a risky chance asks, where should people who want to contribute to PyDentix start?
- 34:47And I would help you kick off this conversation by just pointing out that you have tagged a bunch of issues as help wanted.
- 34:55And then also maybe reviewing PRs, but what else would you add to that?
- 34:58I think the first thing I would say is, and I know this isn't the most fun thing to do, but if people could help with reviewing discussions and issues.
- 35:06Like triage type stuff?
- 35:07Yeah, but if you go on to discussions, we use the GitHub discussions, which maybe people don't even see.
- 35:14But these are all questions you can go in and answer if someone has a problem.
- 35:18Lots of them aren't that complicated.
- 35:19I know that's perhaps not what risky chance meant in terms of writing code.
- 35:24And that's obviously, for some of us, where the fun lives.
- 35:26But these questions would be enormously helpful if people could help.
- 35:29You can see some of them are answered, and that's great.
- 35:31But there are others that aren't.
- 35:33And then, yeah, reviewing pull requests would be the second most useful thing that people could do.
- 35:38And then if there are help wanted issues, just checking that we're still on for it and it's the right time to do it.
- 35:44And then I do love submissions.
- 35:46I noticed today there were 200 and something people who've contributed to Pydantic.
- 35:50So I do do my best to support anyone who comes along, however inexperienced or experienced building features or fixing bugs.
- 35:57Yeah, fantastic.
- 35:57Another thing I want to talk to you about, is this the right one?
- 36:00I believe.
- 36:01No, the validating decorator.
- 36:03Well, let's talk about validators first.
- 36:04We touched on this a little bit.
- 36:06So one thing you can do is you can write functions that you decorate with this validator decorator.
- 36:13And it says, this is the function whose job is to do a deeper check on a field, right?
- 36:18So you can say this is a validator for name, this is a validator for username or validator for email or whatever.
- 36:24And those functions are ways in which you can take better control over what is a valid value and stuff like that, right?
- 36:31Yeah, but you can do more.
- 36:33You can't just be stricter, as in raising error, if it's not how you want it.
- 36:37You can also change the value that you're going to that's come in.
- 36:42So you can see in the first case of name contains a space, we check that the name doesn't contain a space as a dummy example, but we also return title.
- 36:51So capitalize the first letter.
- 36:53So you can also change the value you're going to put in.
- 36:56So coming back to the date case we were hearing about earlier, if you knew your users were going to use some very specific date format of day of the week as a string, followed by day of the month, followed by year in Roman numerals, you could spot that with a regex, have your own logic to do the validation.
- 37:13And then if it's any other date, pass it through to the normal pydantic logic, which will carry on and do its normal stuff on strings.
- 37:20Cool. Now this stuff is pretty advanced, but you can also do simple stuff like set an inner class, which is a config, and just set things like any string strip off the white space or lowercase all the strings or stuff like that, right?
- 37:34Yeah. And there's allow mutation, which you've got to there, which is super helpful.
- 37:38That's where we can stop fields from being modified.
- 37:40There's extra there, which is something people often want, which is what do we do with extra fields that we haven't defined on our model?
- 37:47Do we, is that an error? Do we just ignore them or do we allow them and just like bung them on the class and we won't have any type hints for them, but they are there if we want them.
- 37:55Yeah, very cool. Okay. So the other thing I wanted to ask you about is really interesting because part of what I think makes pydantic really interesting is its deep leveraging of type hints, right?
- 38:04And in Python, type hints are a suggestion. They're things that make our editors light up. They are things that if you really, really tried, I don't think most people do this, but you could run something like mypy against it and it would tell you if it's accurate or not.
- 38:18I think most people just put it as there's extra information, you know, maybe PyCharm or VS Code tells you you're doing it right or gives you better autocomplete.
- 38:26But under no circumstance or almost no circumstance does having a function called add that says x colon int comma y colon int only work if you pass integers, right?
- 38:37You could pass strings to it and probably get a concatenated string out of that Python function because there's no, it's not like C++ or something where it compiles down and checks the thing right.
- 38:47But you also have this validating decorator thing, which it seems to me like this will actually sort of add that runtime check for the types. Is that correct?
- 38:57That's exactly what it's what it's designed to do. It's always been a kind of like interest to me almost a kind of, yeah, just a kind of experiment to see whether this is possible, whether we could like have semi strictly typed logic in Python.
- 39:09I should say before we go any further, this isn't to be used on like every function. It's not like Rust where doing that validation actually makes it faster. This is going to make calling your function way, way slower because inside validate arguments, we're going to go off and do a whole bunch of logic to validate every field.
- 39:26But there are situations where it can be really useful and where creating that Pydantic model was a bit onerous, but where we can just bang on the decorator and get some validation kind of for free.
- 39:36Right. Because the decorator basically does the same thing. I mean, sorry, the classes do the same thing as this decorator might, but instead of having a class, you have arguments.
- 39:44And under the hood, what validate arguments is doing is it's expecting that function, taking out the arguments, building them into a Pydantic model and then running the input against that into that Pydantic model and then using the result to call on the, to call the function.
- 40:00Yeah. And that sounds like more work than just calling the function for sure. It depends on how much it does, right?
- 40:05Does it cache that kind of, does it like cache the class that it creates when it decorates a function?
- 40:09It caches the model.
- 40:10Yeah.
- 40:11Same as we do in other places, but yes, it's still a lot more like Pydantic fast for data validation, but it's data validation, not a compiler.
- 40:19Yeah. So maybe this would make sense if I'm writing a data science library and at the very outer shell, I pass in a whole bunch of data, then it goes off to all sorts of places.
- 40:28Maybe it might make sense to put this on the boundary entry point type of thing, but nowhere else.
- 40:33Yeah, exactly. Where someone's going to find it much easier to see a Pydantic error saying these fields were wrong rather than seeing some strange matrix that comes out the wrong shape because they passed in something as a, as a string, not an int.
- 40:45Or none type has no attribute such and such.
- 40:48Yeah.
- 40:49Whatever.
- 40:49Right.
- 40:50That standard error they always run into.
- 40:51Okay.
- 40:52That's pretty interesting.
- 40:53Let's talk a little bit about speed.
- 40:56You have talked about this a couple of times, but maybe it's just worth throwing up a simple example here to put them together.
- 41:03So we've got Pydantic, we've got Adders, we've got Valadeer, which I've never heard about, but very cool.
- 41:08Marshmallow and a couple of others like Django REST Framework and Cerebus.
- 41:13So it has the, all of these in relative time to some benchmark code that you have, but it basically gives it as a percentage or a factor of performance, right?
- 41:23Yeah.
- 41:24And the first thing I'll say is that there were lies, damn lies and benchmarks.
- 41:28Like you'll get, you might well get different results, but my impression from what I've seen is that Pydantic is as fast, if not faster than the other ways of doing it in Python.
- 41:38Short of writing your own custom code in each place to be like, yeah, to do manual validation, which is a massive pain.
- 41:44And if you're doing that, you probably want to go and write it in a proper compiled language anyway.
- 41:48Right.
- 41:48Right.
- 41:49Or maybe just use Cython on some little section, something like that, right?
- 41:53So all of Pydantic is compiled with Cython and is about twice as fast.
- 41:58If you install it with pip, you will get mostly the compiled version.
- 42:02There are binaries available for Windows, Mac and Linux, Windows 64-bit, not 32, and maybe some other extreme, and it will compile for other operating systems.
- 42:12So it's already faster than just calling Python.
- 42:16Well, I don't know about whether the validation with Pydantic that's compiled is faster than raw Python, but like it'll be of the same order of magnitude.
- 42:23Yeah.
- 42:23Yeah.
- 42:24Fantastic.
- 42:24Okay.
- 42:24I didn't realize it was compiled with Cython.
- 42:26That's great.
- 42:27Yeah.
- 42:27It's part of the magic, huh?
- 42:28Making it faster.
- 42:29Yeah.
- 42:29So that was David Montague a year and a half ago, put an enormous amount of effort into it and, yeah, about doubled the performance.
- 42:36It's Python compiled with Cython rather than real Cython code.
- 42:40So it's not as C speed, but it's faster than just calling Python.
- 42:44Yeah, absolutely.
- 42:45And Cython, taking the type hint information and working so well with it these days, it probably was easier than it used to be or didn't require as many changes as it might.
- 42:54Otherwise, I think it's an open question.
- 42:56I think it's an open question whether Cython is faster with type hints.
- 42:58It does.
- 42:58It's in places actually adding type hints makes it slower because it does its own checks that I think is a string when you've said it's a string.
- 43:05But yeah, I think it does use it in places.
- 43:07Yeah.
- 43:07I was thinking more like you don't have to rewrite it in Cython.
- 43:10I don't have to convert Python code to Cython code where it has its own sort of descriptor language.
- 43:15But like if you have Python code that's type annotated, it'll take that and run with it these days.
- 43:21I think it isn't any faster or any better because of the type hints much.
- 43:25Although someone out there is an expert and I don't want to say that.
- 43:27So I'm not sure.
- 43:28Yeah, I hear you.
- 43:29All right.
- 43:30Another thing I want to touch on is the data model code generator.
- 43:33You want to tell us about this thing?
- 43:34What is this?
- 43:34I haven't used it much.
- 43:36But yes, what we haven't talked about here.
- 43:37It's just what it is.
- 43:38Is JSON Schema, which is what Sebastian Ramirez implemented a couple of years ago when he was first starting out on FastAPI.
- 43:45And it's one of the coolest features of FastAPI.
- 43:47And Pydantic is that once you've created your model, you don't just get model and model validation.
- 43:54You also get a schema generated for your model.
- 43:56And in FastAPI, that's automatically created with Redock into really smart documentation.
- 44:01So you don't even have to think about documentation most of the time if it's internal or it's not widely used API.
- 44:08And even if it's widely used, add some doc strings and you've got yourself like amazing API documentation, just straight from your model.
- 44:16And data model code generation, as I understand it, is generating those JSON schema schemas for models.
- 44:23Is that right?
- 44:24Yeah, I think so.
- 44:24It feels to me like it's the reverse of what you described from what Sebastian has created, right?
- 44:29Like given one of these open API definitions, it will generate the Pydantic model for you.
- 44:36So if I was going to consume an API and I'm like, well, I got to write some Pydantic models to match it.
- 44:42Like you could run this thing to say, well, give me a good shot at getting pretty close to what it's going to be.
- 44:47Yeah.
- 44:47Yeah.
- 44:48Yeah.
- 44:48I had it around the wrong way.
- 44:49But yeah, my instinct is I haven't used it, but that it gets you, it does 90% of the work for you.
- 44:55And then there's a bit of like manual tinkering around the edge to change some of the types I suspect.
- 44:59But like, yeah, really useful.
- 45:01Yeah.
- 45:01And it supports different frameworks and stuff.
- 45:03And I haven't used it either, but it just seemed like it was a cool thing related to sort of quickly get people started if they've got something complex to do with Pydantic.
- 45:11So for example, I built this weather, real time, weather, live weather data service for one of my classes over at weather.talkpython.fm.
- 45:19I built that in FastAPI and it exchanges Pydantic models.
- 45:23And all you got to do in order to see the documentation, just go to slash docs.
- 45:28And then it gives you the JSON schema.
- 45:31So presumably I could point that thing at this and then it would generate.
- 45:36And go back to the model.
- 45:37Exactly.
- 45:38And get a fairly complicated Pydantic model prebuilt for me, which I think is pretty excellent.
- 45:42Yeah.
- 45:43It's worth saying maybe you disagree, but I think the redock version of the documentation or autodox is even smarter than that one.
- 45:51I don't know if you've got it.
- 45:52Yeah, that one I think is even smarter.
- 45:53Oh yeah, this is a really nice one.
- 45:54I like this one a lot.
- 45:55Yeah, it even gives you the responses there.
- 45:57It could be 200 or 422, which I did build that into there, but I didn't expect it to actually know.
- 46:02That's pretty interesting.
- 46:03Yeah, it's cool.
- 46:03Yeah, it's very cool.
- 46:04So they're both there, either slash docs or slash redock.
- 46:08FastAPI will pull them.
- 46:10You can switch one off or change the endpoints, but yeah.
- 46:12Yeah.
- 46:13And by the way, if you're putting out a FastAPI and you don't want public documentation,
- 46:18make sure that you set docs, the docs URL and the redox URL to none.
- 46:22And when you're creating your app or your API instance.
- 46:25So yeah, that's always on unless you take action.
- 46:28So you better be sure you want to.
- 46:29Or you can do what I've done, which is protect it with authentication so the front end developers can use it, but it's not publicly available.
- 46:36So if you're building like a React app, it's really useful to have your front end engineers be able to go and see that stuff and understand what the fields are.
- 46:43But it's a bit of a weird thing to make public, even if there's nothing particularly sensitive.
- 46:47So yeah, you can put it behind authentication.
- 46:49Yeah.
- 46:49Yeah, very good.
- 46:50All right.
- 46:51You already talked about the PyCharm plugin, but maybe give us a sense for why do we need a PyCharm plugin?
- 46:57I have PyCharm.
- 46:59And if it has the type information, a lot of times it seems like it's already good to go.
- 47:04So what do I get from this PyCharm plugin?
- 47:06Like, why should I go put this in?
- 47:07So once you've created your model, if we think about the example on the index page again, we would, once we've created our model, accessing .friends or .id or .name will work.
- 47:18And PyCharm will correctly give us information about, we'll say, okay, first name exists, like foobar name doesn't exist.
- 47:28It's a string, so it makes sense to add it to another string.
- 47:31But when we initialize a model, it doesn't know how, like, the init function of Pydantic just looks like, take all of the things and pass them to some magic function that will then do the valid thing.
- 47:40I see.
- 47:41It looks like star, star, KWR, good luck, go read the docs.
- 47:45Yeah, exactly that.
- 47:46But this is where the PyCharm plugin comes in because it gives you documentation on the arguments.
- 47:51Okay, so it looks at the fields and their types and says, well, these are actually keyword arguments to the constructor, the initializer.
- 48:00Yeah.
- 48:00Okay.
- 48:00Yeah, got it.
- 48:01That's very cool.
- 48:02And it will also, I don't even know what it does.
- 48:06I just use it the whole time and it works.
- 48:07You know those things, but you don't even think about them.
- 48:09Yeah, cool.
- 48:10So it gives you auto-completion and type checking, which is cool for the initializer, right?
- 48:14So if you were to try to pass in something wrong, it lets you know.
- 48:17Also, it says it supports refactoring.
- 48:19If you refactor the keyword.
- 48:21One of the really useful things it does is when we talked about validators, which are done by a decorator, they are class methods.
- 48:28It's very specifically because you might think that they're instance methods and you have access to self.
- 48:33You don't because they're called before the model itself is initialized.
- 48:37So the first argument to them should be class CLS.
- 48:40It will automatically give you an error if you put self, which is really helpful when you're creating those validators because otherwise without it, HighCharm assumes it's an instance method, gives you self, and then you get yourself into hot water when you access self.userid and it breaks.
- 48:55Oh, interesting.
- 48:56Okay.
- 48:56Yeah, that makes a lot of sense.
- 48:57Because it's converting and checking all the values and then it creates the object and assigns the fields, right?
- 49:04Yeah, so we can access other values during validation from the values keyword argument to the validator, but not via like self.userid or whatever.
- 49:13Yeah, cool.
- 49:14And Risky Chance loves that it works with aliases too, which is pretty cool.
- 49:18Oh, yeah.
- 49:19It does.
- 49:19It does lots of cool things.
- 49:21I'm really impressed by it.
- 49:22It's one of the coolest things that come out of Pydantic.
- 49:24Awesome.
- 49:25Yeah, I've installed it and I'm like, I'm sure my Pydantic experience is better, but I just don't know what is built in and what is coming from this thing.
- 49:32So, yeah, that's...
- 49:33We're also used to PyCharm just working on so many things that you don't even notice.
- 49:38Like, yeah, you only notice when it doesn't work, so...
- 49:40Yeah, absolutely.
- 49:41So, we're getting a little short on time, but I did want to ask you about Python DevTools because you talked about having Pydantic work well with DevTools as well.
- 49:53Yeah.
- 49:53What are these?
- 49:54You are also the author of Python DevTools, yeah?
- 49:56Yeah, what is this?
- 49:57For me, it's just a debug print command that puts stuff pretty and gives it color and tells me what line it was printed on.
- 50:03And I use it the whole time in development instead of print.
- 50:05And obviously, I wanted it to show me my Pydantic models in a pretty way, so it has integration.
- 50:11There are some hooks in DevTools that allow it to customize the way stuff's printed.
- 50:16And I actually know that the author of Rich, he slightly frustratingly has used a different system all over again, but he's also supported Pydantic.
- 50:23So, Pydantic will also print pretty with Rich as well as with DevTools.
- 50:27Yeah, cool.
- 50:28Okay, really nice.
- 50:29Yeah, Rich is a great TUI, Terminal User Interface Library for Python.
- 50:34Yeah, it's cool.
- 50:35It's different from DevTools.
- 50:36I wouldn't say they compete.
- 50:37DevTools is, for me, it's just...
- 50:38It does have some other things, some timing tools and some formatting, but for me, it's just the debug print command that Python never had.
- 50:45Nice.
- 50:45So, what's the Pydantic plugin here, or connection, rather, here?
- 50:50So, if I debug out of DevTools a model, I get just a really nice representation?
- 50:55Yeah, exactly that.
- 50:56It's not showing it...
- 50:58It's because you're in the DevTools docs.
- 50:59There's some other docs in Pydantic to give you an example.
- 51:02But it'll give you a nice example if it expanded out rather than squashed into one line.
- 51:07So, usage with DevTools is the last...
- 51:09There you go.
- 51:09Got it.
- 51:09Yeah, so you see that.
- 51:11The user picked out nicely instead of, yeah, done like that.
- 51:15I suppose that's kind of...
- 51:16That demonstrates its usage for me.
- 51:18Yeah, perfect.
- 51:19That looks really good.
- 51:20It's nice to be able to just print out these sorts of things and see them really quickly.
- 51:25What's the just basic string representation of a Pydantic model?
- 51:29For example, if I'm in PyCharm and I hit a breakpoint, or I'm just curious what something is and I just print it, PyCharm will put a little grayed out...
- 51:37String, str representation.
- 51:39It's right there.
- 51:40I think that's the string representation you're looking at right there.
- 51:42Yeah, perfect.
- 51:43So, you get a really rich sort of view of it embedded in the editor or if you print it...
- 51:48And if you use Repra, then you get basically wrapped in user.
- 51:51All right.
- 51:51Okay.
- 51:52So, it gives you...
- 51:53As if it were...
- 51:53Yeah.
- 51:54You're trying to construct it out of that data.
- 51:56Yeah.
- 51:56Okay.
- 51:57Fantastic.
- 51:57Well, you know, we've covered a bunch of things and I know there's a lot more.
- 52:02I don't recall whether we talked about this while we were recording or whether we talked about it before and we were just setting up what we wanted to talk about.
- 52:11But it's worth emphasizing that this is not just a FastAPI validation data exchange thing.
- 52:16It works really great.
- 52:17A lot of the stuff happens there.
- 52:18But if you're using Flask, if you're using Pyramid, if you're using...
- 52:22I don't know about Django so much because of the models and stuff there, but...
- 52:25There is.
- 52:26Someone's built Django admin.
- 52:28So, one of the things we haven't talked about as well is settings management, which Pydantic has some pretty powerful features for.
- 52:33And actually, one of the things was added in 1.7 or 1.8 was basically a system for plugins to do even crazier stuff with settings.
- 52:40So, not just loading them from environment variables and from .m files, but also from Docker secrets.
- 52:47Now we have an interface to load them from kind of anywhere.
- 52:49So, you can build your own interface for loading settings from places.
- 52:53But someone's built a Django settings tool with Pydantic to kind of validate your Django settings using Pydantic.
- 53:00But yeah, I think it's what's cool about Pydantic is it's not part of a kind of walled garden of tools that all fit together.
- 53:07Well, that have to be used with each other.
- 53:08It fits with Pydantic, but it's used in lots of other big projects.
- 53:12Or you can just use it in Flask or in Django or wherever you like.
- 53:16Right.
- 53:16If you're reading JSON files off a disk, it could totally make sense to use it.
- 53:20Or you're doing screen scraping, potentially it makes sense.
- 53:23Or just calling API, but you're the client of that API.
- 53:26It could totally make sense to do that.
- 53:28Yeah.
- 53:28Yeah.
- 53:28I just want to point out, like it's super broadly applicable, not just where people see it being really used.
- 53:34And Nick H out there is definitely going to try this with Django.
- 53:38So, awesome.
- 53:38That's cool.
- 53:39Yeah.
- 53:39So, let's wrap this up with just, I know we spoke a little bit about V2 and the timing.
- 53:45Like, what are the major features that you think, like what are the highlights that people should look forward to or be excited about?
- 53:51There's a bigger problem at hand, which is the Python 3.10 at the moment in PEP.
- 53:56I'm going to try and remind myself of the exact number.
- 53:59But like in PEP 6.9, no, in 5.6.3, basically all type hints become strings instead of Python objects.
- 54:08And so, and like that's been available in future.
- 54:10Right.
- 54:11Is that the lazy evaluation of the annotation, something like that?
- 54:15But it's not even a lazy evaluation.
- 54:17It's a non-evaluation.
- 54:18And it seems like unless Python themselves, the core team, are prepared to move on this and like be practical about things, it might be that Pydantic becomes either like hard to use or even not useful in 3.10.
- 54:30It sounds like I'm talking at the Python Summit in PyCon US in May in the like language in the bit where people discuss it.
- 54:39And I'm going to try and put this forward.
- 54:41But like I had a conversation today just before I came online now with someone who's created a PEP that should fix this.
- 54:48But the current response from the core developers is to refuse it.
- 54:51So I'm like really worried and frustrated that might happen.
- 54:55And lots of tools, FastAPI, Pydantic, Typer, and others, again, it get broken for the sake of principle effectively that type hints should only be used for static type analysis.
- 55:05So we'll see what happens.
- 55:06And normally with open source, people find a way around.
- 55:08But like I think that's really worrying.
- 55:10And I'll create an issue on Pydantic to track this properly.
- 55:12But it is something to be aware of.
- 55:14And it's something that like I think those of us who use these libraries need to like it's very easy to wait until after something's released and then be frustrated.
- 55:22It's important sometimes to notice before they're released and make a point.
- 55:25Wow.
- 55:26Well, I'm really glad you pointed that out.
- 55:27I had no idea.
- 55:28I mean, I knew there were minor behind the scenes changes from a consumer perspective of type annotations.
- 55:33But that sounds like there's more going on for libraries like this.
- 55:38There's a PEP that will fix this, which is PEP 649, which I have not yet read because I only got the email about it two hours ago.
- 55:45But if anyone's looking into it, I will create an issue on Pydantic to talk about this.
- 55:50But something like this needs to happen.
- 55:52Also, Larry emailed me an hour or two hours ago to talk about this.
- 55:55But this is a really big problem that we need to like prevent like breaking lots of cool stuff that's happening in Python.
- 56:02All right.
- 56:03Well, I agree.
- 56:05First impressions is I absolutely agree because I do think what you guys are doing, what you're doing with Pydantic, what is happening with FastAPI and these types of systems.
- 56:14It's a really fantastic direction and really building on top of the type annotation world.
- 56:21And I would hate to see that get squashed.
- 56:23What's incredible about it just briefly is that it's used by Microsoft in core bits of Office.
- 56:28It's used by Uber, by the NSA, by like banks.
- 56:32It's used by JP Morgan.
- 56:34But it's also really easy to get started with at the very beginning.
- 56:37And it's wonderful for me that we can build open source code that can be useful to like the biggest organizations in the world and to someone when they're first getting started.
- 56:45Not this idea that it has to be like dense and mainframe and impossible or like Mickey Mouse and not worth using.
- 56:52Right.
- 56:52Like FastAPI and Pydantic seem to be managing to be both.
- 56:55I agree.
- 56:55I think they are absolutely.
- 56:57So, well, congratulations on building something amazing.
- 57:00Thank you very much, Michael.
- 57:02And hopefully, Pep 649 keeps things rolling smooth.
- 57:06Hopefully that gets ironed out.
- 57:08All right.
- 57:09Now, we're pretty much out of time.
- 57:10But before I let you out of here, let me ask the final two questions.
- 57:13So if you're going to write some code, if you're going to work on Pydantic, what editor do you use?
- 57:17I use PyCharm.
- 57:18Right on.
- 57:19And the Pydantic plugin, I'm guessing.
- 57:21And the Pydantic plugin, yeah.
- 57:22Right on.
- 57:24And then if you've got a package out on PyPI that you think is interesting, maybe not the most popular, but you're like, oh, I ran across this thing that's amazing.
- 57:32You should know about it.
- 57:34I should probably not break the rule and talk about my own.
- 57:36But like DevTools, which I talked about, is incredibly useful to me.
- 57:39And so I would spread the word a bit on that.
- 57:42Other than that, I just do a shout out to all of those packages that people don't see that are the bedrock of everything.
- 57:47So from Coverage to Starlette, which is the other library that's the basis of FastAPI.
- 57:51Sebastian's great.
- 57:52And I mean, no offense to him, but he stands on the shoulders of people who've done lots of other things.
- 57:57And they're really, really powerful.
- 57:58So I would spare a bit of time for them.
- 58:01If you're thinking of sponsoring someone, think about sponsoring Ned who does Coverage or any of those other bits of pytest, all the workhorses that aren't particularly headlined, but are really, really valuable to all of our daily life writing code.
- 58:16Yeah. And I'm going to go with Vlad out there.
- 58:18He says, you need us know about PyTonic because of FastAPI.
- 58:22I agree.
- 58:22But FastAPI, as you pointed out, absolutely stands on top of Starlette, which there's just this whole chain of things that each one adds their own special sauce.
- 58:33But they're there because of...
- 58:34But I should say again, FastAPI is awesome.
- 58:36I didn't use it initially.
- 58:37I'm a contributor to aiohttp, which is also really cool.
- 58:41But I've, over the last year, become a complete convert to FastAPI.
- 58:45I use it.
- 58:46It's my go-to tool now.
- 58:47So it's awesome.
- 58:48Yeah.
- 58:48Fantastic.
- 58:49All right.
- 58:49Final call to action.
- 58:51People want to check out PyTonic.
- 58:53Maybe they want to contribute to PyTonic.
- 58:55What do you tell them?
- 58:56Go and have a read through the docs.
- 58:57And yeah, go from there.
- 58:59If you can make a tweet to the docs to make it easier to read, you can answer someone's question or even create a feature.
- 59:05That would be...
- 59:05That's awesome.
- 59:06Cool.
- 59:06All right.
- 59:07And if I'm not there immediately and I don't reply for weeks, I'm sorry.
- 59:10And I promise to as soon as I can.
- 59:11Fantastic.
- 59:13All right.
- 59:13Samuel, thanks for being on the show.
- 59:14It's been great to learn more deep information about PyTonic because it's so simple to use it.
- 59:19It's easy to just skim the surface.
- 59:21Awesome, Michael.
- 59:21Thank you very much.
- 59:22Yeah, you bet.
- 59:23Bye-bye.
- 59:23Yes, bye.
- 59:24This has been another episode of Talk Python To Me.
- 59:27Our guest in this episode was Samuel Colvin.
- 59:29And it's been brought to you by 45 Drives and us over at Talk Python Training.
- 59:35Solve your storage challenges with hardware powered by open source.
- 59:38Check out 45 Drives storage servers at talkpython.fm/45 Drives and skip the vendor lock-in and software licensing fees.
- 59:46Want to level up your Python?
- 59:48We have one of the largest catalogs of Python video courses over at Talk Python.
- 59:52Our content ranges from true beginners to deeply advanced topics like memory and async.
- 59:57And best of all, there's not a subscription in sight.
- 1:00:00Check it out for yourself at training.talkpython.fm.
- 1:00:03Be sure to subscribe to the show.
- 1:00:05Open your favorite podcast app and search for Python.
- 1:00:08We should be right at the top.
- 1:00:09You can also find the iTunes feed at /itunes, the Google Play feed at /play, and the direct RSS feed at /rss on talkpython.fm.
- 1:00:18We're live streaming most of our recordings these days.
- 1:00:22If you want to be part of the show and have your comments featured on the air, be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:00:30This is your host, Michael Kennedy.
- 1:00:31Thanks so much for listening.
- 1:00:33I really appreciate it.
- 1:00:34Now get out there and write some Python code.
- 1:00:36Thank you.
- 1:00:36Bye.
- 1:00:37Bye.
- 1:00:38Bye.
- 1:00:39Bye.
- 1:00:40Bye.
- 1:00:40Bye.
- 1:00:40Bye.
- 1:00:40Bye.
- 1:00:40Bye.
- 1:00:41Bye.
- 1:00:42Bye.
- 1:00:42Bye.
- 1:00:42Bye.
- 1:00:42Bye.
- 1:00:43Bye.
- 1:00:43Bye.
- 1:00:44Bye.
- 1:00:44Bye.
- 1:00:44Bye.
- 1:00:45Bye.
- 1:00:46Bye.
- 1:00:46Bye.
- 1:00:46Bye.
- 1:00:47Bye.
- 1:00:48Bye.
- 1:00:48Bye.
- 1:00:49Bye.
- 1:00:50Bye.
- 1:00:50Bye.
- 1:00:50Bye.
- 1:00:51Bye.
- 1:00:52Bye.
- 1:00:52Bye.
- 1:00:53you
- 1:00:53you
- 1:00:54Thank you.
- 1:00:56Thank you.