Pydantic Has Saved Me Countless Hours Of Debugging

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Are you spending hours trying to debug your  application due to an unexpected data type.   If you're anything like me exceptions became the  rule instead of the exception. Well starting right   now it's time to Take Back Control it's time  for Pydantic. So Pydantic is a data validation   library for python it and has a couple key  features. The first one being data validation,   Pydantic ensures that the data You're Expecting  matches the data in the type hints of your python   application. Which really means if you're  expecting a string you're going to get a   string and if someone tries to pass an INT the  application will fail and raise an exception.   The second key feature is data parsing which  means you are automatically going to be able   to convert the data into Data that you can use  within your application so think of it as an API   request comes in and it's all text we can change  and convert that into a python object. Pydantic   is a key component into modern python applications  so let's dive in and check out the main features   of Pydantic so here's a FastAPI. Application now  FastAPI uses Pydantic automatically. It's embedded   within the framework of FastAPI so I thought that  would be the best way of displaying the power of   Pydantic so I already have Pydantic imported using  the base model this is all FastAPI application   stuff outside the Pydantic words so let's go ahead  and say we just have a class of user create which   takes in Pydantic so this is just going to be the  initial Foundation of Pydantic in our application   the user create has an email of type string  password of type string and then we have a class   user which inherits our user create that just  has a uuid now this application is a post request   where we just pass in a user of user create so  it's just going to be the email and password and   then we're just going to be returning that user  where we hard code the uuid and then we pass   in the email and the password that we get from  the request so let's just see this application   first and then we will go ahead and add Pydantic  features to it so to start if we just have this   Swagger documentation and we just want to call  the Post request to our application we can open   this up and say hey we want to call this create  user request which takes in an email of string   and password of string and if we said we wanted  to try it out we can type anything we want here   with just a random couple characters for each  and then we can say execute where we are going   to get a successful 201 of HTTP created with an  email of ypu a password of this and a uuid that   we statically added now if you ask me this is not  that great a email just can't be three characters   a password like this might work we might want  to say that it needs some symbols or numbers I   roughed up my head this works because we only have  one user but if we went ahead and created another   user we have this statically added so we need  something that's going to make this uuid a little   bit more Dynamic so let's go back to our IDE and  let's add some Pydantic features the first one   we're going to add is to the password so we can  go ahead and in this area of from Pydantic import   based model let's go ahead and just say we want  to add field and then save our application but   what field allows us to do is add some very brief  data validation to our data that we're passing in   using the request so what I mean by this is the  password can be in any amount of characters right   now and we want there to be a minimum of five  characters and a maximum of 12 characters so here   we can say equals field and inside the parentheses  we can say minimal length of equals to 5 and max   length equals 12. just this change will already  start adding some data validation to our classes   that we're getting from the request so if we open  up our application again and we just refresh the   page let's say and say try it out enter password  let's go ahead and just type in three characters   e-r-y if we try to execute this we are going to  get an error and this error is going to even have   a message that says string should be at least  five characters with string two short so we're   already implementing some kind of data validation  within our application by just implementing the   field from Pydantic which says that the minimal  length must be 5 and the maximum length can be   12. so just to test out the maximum length if we  just keep adding things until we believe it's over   12 characters and we click execute we're going  to get the exact same thing of string too long   um should be at most uh 12 characters so this is  working for the initial data validation for our   application all right let's go back to our visual  studio and inside here we can see that we now have   field implemented on at least the password but  one thing that we see is that we have our uuid   statically being added now this is something that  we don't want the request to do we want Pydantic   to handle this automatically so to do this we  can use the default Factory and that is part of   the field so we can say equals field and inside  here we want to pass in the default Factory of   uuid4 after doing this we can just remove this  complete static ID and now let's go back to our   applications browser so if we come back here and  we refresh the page because that's from the last   one this should work just string on both of them  and we click execute we can see that we get a 201   with the email passing data validation with string  password also passing and now our ID of our uuid   starts with an f and ends with 247. if we click  execute again we can see that the uuid changes   each time and now what the default Factory allows  us to do is generate some kind of data before   the object is created and in this fact we are  generating a new uuid each time a new user create   is passed through as a request all right now let's  go ahead and say we want to add a new field to our   user create and this is going to be an age and  we'll say of type int let's go ahead and say for   a field we want to add some restrictions well Min  length and max length are based on the characters   of password so we're talking about a string and  we're validating the amount of characters within   that string well using an INT instead of using Min  length and max length we can use greater than and   less than so we can say that you must be greater  than 12 to use this application so that means   you're 13 you have to be greater than 12 but let's  also just for fun throwing you must be less than   100. for some reason this application is 13 to 99  only so this will work but there's going to be a   little twist to it but let's find that out in a  second if we save this application and we go back   to our browser and we just refresh the page we can  see that age starts off at zero let's go ahead and   change that to 20 to pass our data validation and  click execute well when that happens we get in   error and that error is to be expected that's  the trick up the sleeve that I just mentioned   and the reason that's happening is we're  expecting an age what we're passing in an age   but we're not converting the new user to have this  age so when we create this user this user has an   age that request gets passed in but when we want  to use the actual user within our application it   no longer has an age so the Quick Fix is we can  just say age equals user.h this would fix our   problem but there is a better way of doing this  in Pydantic and it's called model dump which means   we are going to dump all of the data from the  request into the python object we're trying to   create so this is a good way if there's only like  one or maybe two Fields within the object but what   if we have multiple developers and they just keep  adding to our user create I mean user create could   have address it could have Country phone number  zip code it could have so much information well a   quick way of doing this is we can just remove all  of these fields and just say user dot model dump this means everything that we get from our user  create request we're just going to dump that into   our python object of user so if we go back to  our application and we refresh we can say try   it out change age to 20 click execute and we  still get an error well if we go back to our   application what we need to do here with the  user.model dump is just add two stars in front   the two stars in front means we're going to pass  in all of the arguments so now that we're going   to pass in all of the arguments if we go ahead  and say execute we're going to get it HTTP 201   with the email string password string age  20 with a random uuid alright so now that   we're getting closer let's go ahead and add a  Json schema now a Json schema means that right   here in the very beginning where our email says  string or password says string and our age says   zero we can change that data so the user won't  be surprised on what they're running into now we   can do that using a Json config from the class  config so right under here let's go ahead and   say class config where we want to create a new  Json schema extra now for example right here we   created our new class config which is inside the  class user create we have our Json schema extra   where we're saying this is going to be an  example so it's going to be an example user   for our application we want the email to be  test email.com we want the password to be   test1234 exclamation mark with the age of 65. so  if we say this application and now go back to our   browser we can see that this example data is now  age 65 email test email email.com and a password   test1234 exclamation mark now if you don't already  see how much Pydantic can influence and add data   parsing and data validation to our application  this thing allows you to be able to accelerate   your application by so much now another thing  we can do is field validation so on the end of   field we can say field validator now we already  have our normal field which is min length max   length greater than less than but what if we want  to add a little bit more validation that's not as   simple as counting the characters or checking  the condition of a number well what we can do   here is add a field validator so right under  our config so still within the class of user   create we can say at field validator and we want  this to be on our password so here we're saying   this field validator is going to be part of our  password and now we can say Def password validator where we pass in our class and a value and now we can just make sure that the user  just doesn't submit the default value so let's   go ahead and just say if value is equal to test  one two three four exclamation mark well then we   want to raise a value error or we're going to  say please do not use default password and we   can say dot because for obvious reasons if it  doesn't and we do pass the value well then we   can just return the value if we save this and  we go back to our browser and we refresh if we   say try it out execute well we're gonna get a  value error please do not use default password   of dot dot dot based on what we just created  which was the field validator for our password   field where we're saying it cannot be equal to  test one two three four exclamation mark now   there's one more thing that we can really do  and it's adding some validation to our email   right now our email is just a string and it  accepts pretty much everything so what we can   do is just let's cancel out of our application  real quick and say pip install email validator   and let's press enter now what this did was  installed a new Pydantic mini package that   will allow us to be able to validate emails super  easily so what we can say is now from Pydantic we   want to add our email string and now from here we  can say just equals our email string so now if we   go ahead and restart our application and we go  back to our browser if we say try it out and we   remove for example that um at sign that a lot of  emails have and we try and execute we can see that   we're going to get an error oh that's the first  one that's one that we created of uh do not use   our default password so I'm going to change it to  two exclamation marks and then click execute well   immediately we can see that it does work right  now let's go ahead and just make this a little   bit worse by saying we want it to just be test  EMA execute let's see if we're getting any type   of error here let's close and let's restart  this application oh okay so it went through   because it's not equals email string that email  string is the type so right here we just want   to change it from a string to email string all  right so let's try this again and refresh and   now if we say try it out and we remove this at  sign and maybe this Dot and then we say execute there we go we're actually getting two errors this  time the first one saying we are not passing in a   valid email and the second one being our personal  validation that we added to our password now I   hope this helped you just be able to understand  the power of Pydantic and how awesome it is and   how much you can just speed up data validation  and data parsing within your own application if   you enjoyed this video please like comment and  subscribe and I will see you in the next video
Info
Channel: Eric Roby
Views: 4,771
Rating: undefined out of 5
Keywords: pydantic, fastapi, pydantic schema, config management, pydantic data validation, pydantic validator, pydantic settings, fastapi python, pydantic models, python pydantic, data validation in python, python data validation, fastapi pydantic, pydantic tutorial, python, json, api, fast api, python api, python fastapi
Id: fCsV3kCOeKc
Channel Id: undefined
Length: 14min 51sec (891 seconds)
Published: Tue Aug 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.