Why You Should Use Pydantic in 2024 | Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Python's Dynamic type system is incredibly user friendly but it can sometimes lead to issues when handling data since we might end up with unexpected results that's where penic comes in it's the Ideal tool to help us be meticulous when dealing with our data it's particularly useful when working with external data sources such as apis or user input if you'd like to learn how to design software from scratch I have a free guide for you you can get it at iron. c/d designu covers the seven steps that I follow when designing new piece of software hope it proves helpful in avoiding some of the mistakes that I made in the past you can also find a link to the guide in description of this video now let's dive into pantic here's the example that I'm going to be working on today as you can see it Imports a couple of things from pantic so what is pantic it's a library that allows you to do data validation and serialization that relies on the type annotation mechanism in Python knowing that your data is valid and it follows certain rules and structures is really helpful and the earlier you detect this the better it's sort of the fail fast principle that you apply here so when you work with data that's not well structured or defined in the way that you want it to then it's a good thing to fail fast and fill early on so that data doesn't I don't know mess up the rest of your data Pipeline and this is where piden can be really helpful another area where validation and serialization is really really useful is in developing apis when users send requests to your API you need to make sure that the data they're providing is valid so that you can't get hacked or anything like that and this is why pantic is also really helpful and that's why it's been widely used by popular Frameworks such as fast API for data validation poentic Works in a very similar way to data class so you use type annotations to indicate the type of instance variables part of an object the main difference is that pantic adds much more powerful validation and serialization options and the built-in data classes if you don't need those things then using data classes is perfectly fine but if you do need validation then pantic is really a great option in the example that I have today I have a p project file that contains the dependencies and as you can see pantic is in there and on top of that I've also added an extra which is email this allows me to do email address validation and next to that we also have fast API and htpx which I'll use in an example later on in the video so in this particular example I have a couple of enom and classes so there's the role enome which is an INT and that's either author editor developer or admin which can be either one of these three and then I have a user class and that inherits from base model so base model is a class that comes from pantic and that's basically implements the basics of the whole validation system of pantic and when you define your model your pantic class you inherit from base model and then you add the fields that are supposed to be part of it so one of them is name what pantic specifically does that use a combination of these type anotations that you see here and the field function so in this case the user has a name email password and a role and the field function has a couple of options so one thing that you can do is that you can add some examples of what are valid values for this thing so here is an example of a name is Ariel or in the case of an email you can provide examples of valid email address you can also provide a description of what it is and this can then be used by fast API for documentation which is really cool so you see that there is another type here called email email string and this also something that I imported from pantic so next to these built-in types like strings or integers or anything else that you would like to use you can also use these types which already have some basic validation for you so this email string is a string that's supposed to be be an email address and if you supply something that isn't an email address this is going to complain another example here is password which is a secret string and that simply means that if you're going to print the password it's going to print out a bunch of asteris instead of the actual password and finally you can also directly use enom like the role that I defined here and also Supply that as a pantic type and then it's also going to make sure that the values that you provide when you create this rle actually match the value in this enumerated type and there's other options as well for example Frozen that allows you to only set it and not change it afterwards so these are just some examples of how pantic can help you get more control over what your data looks like and then what I've done here is Define a validate function that calls model validate on the user so this allows us to validate whether data adheres to the user structure and if so then I simply print the user and if not I print that the user is invalid and I print out the errors and then in the main function I have a good data example which simply has a name email and password and I have bad data which has something that's not an email address and the name is also missing when I run this then this is actually what you get so the first error that we get is that there is something missing namely the name and the second error is a value error which is in the email field which is that value is not a valid email address so this is all already built into pantic so you don't have to write this validation code yourself finally another thing that's nice is that actually you get reference to the documentation with your error so you can click on this and look up what the actual error is but what if you want to do a bit more than just use these built-in types that are offered by prentic well you can also write your own custom validators and add them to the classes so that's what I did here so I just created a few Rec xess to validate names and passwords and then I modified the user base model and added these field validators so if you want to add a custom validation to a field in a pantic object you use the field validator decorator and you supply the name of the field so this is typically what that looks like so you have your field validator you define a class method in this case I'm calling this validate name makes sense right that gets the value and that also should return the value so you can actually modify it if you want to in this case the only thing we're doing is we simply check that this regx matches the value that we get and if not we erase a value error that a name should only contain letters and be at least two characters long the standard way to build these validation functions that they should normally either return the value which is what happens here or they raise a value error or an assertion error you also have some control over when this validation take place you can indicate a mode can be before or after if you choose before it means that the value has not yet been set in the object and that's what you see here so I've added a field validator for the r as before the nice thing about using the before mode is that you can specify what kind of input you want to work with so in this case this can be an integer a string or an R enum value and then I simply try to convert the value into the actual Ro type and if that doesn't work I raise a value error if you use the before mode then these validation functions should be class methods like I defined them here if instead of the before mode you use the after mode so that means that the instance is already created then these are not class methods but these are regular instance methods so you just pass the self and then you can work with the self object and of course you can combine these you can have both validators for before and for after for certain Fields so you can make this as complicated as you want next to field validators which validate specific fields in a model we also have a model validator which allows you to validate the entire instance data all at once and that can sometimes be helpful for example let's say you want to make sure sure that a password doesn't contain the username well in this case if we create a field validator for the password we don't have access to the username so we can't do that but in model validator you can because you get all the data in a dictionary in this case so here you can add some checks like does it have either name or password they should be required you can add a check that the password doesn't contain the name which isn't very secure and you can add other if statements and checks here as well now this one for example is a password recx check so I think this one could actually be a separate password validator but by using a combination of field validators and model validators you can do your data validation in the way that you see fit and here you also see a usage of returning the value when when you're done validating so in this case we hash the password before actually storing it in the instance and then finally I have my validate function again I created some test data so we have good data we have uh dat data with a bad role so there is no programmer type then more bad data missing role or there is uh a name that's not that contains invalid characters we have a duplicate some missing data and then basically when we run this and then this is what we get so for all these things we get the appropriate validation Errors By the way if you're interested in joining a community of developers I have a free Discord server you can join via the link below there's tons of very knowledgeable people there we always have fun discussions about Python and software development in general so I hope you'll join I mentioned pantic also allows you to serialize objects and also has all sorts of settings for that so here's another example that shows how serialization works so serialization means that we transform an object an instance to some other data format often this is Json data so here I have again my user class but what I did in this case that for the password I added an extra option here which is exclude and that means that when you serial I this object that password is not included we don't want to print the password or include it in the serialized form of the user so if I scroll down you see some examples of what you can do to have control over how objects are being serialized so you can add a field serializer for the RO for example in case of Json so what we want to do in this case is that when we serialize the object we want to make sure that the name of the RO is returned so that we can store that in Json data and similarly we can have a model serializer that gives us some control over how users are being serialized and then again I have the main function with some data and then I can use various modes of serialization so here I'm serializing to dictionary using model dump and here I'm serializing to adjacent string and when it serializes to Json then it's actually going to use these field and model serializer since I've indicated that right here as an option finally you can also indicate whether for example some things need to be excluded such as the role in this case so when I run this example then you see here we have the dict serializer so that simply Returns the rooll as an actual enum value the Json string turns this into a string value the Json string without the r simply removes the RO and here finally is an example of serialize that simply encodes all the values to a dictionary in that case you see when it prints the password it actually doesn't print the actual password but uses the secret string print meth me final thing I want to show you I mentioned that penic is used by many popular Frameworks including fast API and that allows you to specify how data should be validated in your rest API when you're using fast API so here's an example of what that looks like so again I have my pantic user model and you can specify extra options here are more suitable to using pantic with apis so one thing that I've added here for example is a configuration setting that forbids adding extra attributes so you don't want somebody to call your API and just add a bunch of attributes to the body right you only want the attributes that are actually being part of the object so those are the types of settings that you can add here again we have name email and these things work just like before but now they're part of an API and I've added a couple of things here as well so you can add a list of friends you can have an optional date time uh you can have an ID which is useful if you want to store the user in database and you can also Define your serial Isis here so that you specify how when you return this objects in your API what should happen when you serialize it to Json so then in fast API we Define the endpoint so we have getting the users creating a user and what's nice is that because we pass the user here as an argument fast API will make sure to actually validate it and then I have main function with a test client so that I can just run some test on using the fast API app and when I run this then you you see it passes all these asserts so I've just included this in the main file normally you would of course move this out of the main file into a separate test folder so that you can run these things as unit test but the important thing is that fast API natively integrates really well with pantic and that allows you to do lots of validation work and make your API really robust so to conclude pantic is a really powerful library for validating your data for serializing your data so if you're building out an API if you're processing data then pantic is really useful to have in your toolkit so I'd like to hear from you have you used pantic in your project in what way are you using it and what were your experiences with it let me know in the comments so like I've shown here pen works really well with fast API if you want to learn more about how fast API Works check out this video next where I dive into the details thanks for watching and see you next time
Info
Channel: ArjanCodes
Views: 46,914
Rating: undefined out of 5
Keywords: pydantic tutorial, pydantic, pydantic tool, pydantic package, pydantic python, pydantic python tutorial, pydantic validation, pydantic library python, python pydantic, pydantic validator, data validation in python, pydantic vs dataclasses, pydantic models, python tutorial, data validation, python dataclasses, python pydantic tutorial, python data validation, python fastapi pydantic, data classes, dataclasses vs pydantic, pydantic model validator, pydantic field validator
Id: 502XOB0u8OY
Channel Id: undefined
Length: 13min 55sec (835 seconds)
Published: Fri Mar 08 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.