Pydantic Tutorial • Solving Python's Biggest Problem

2024 ж. 12 Мам.
231 230 Рет қаралды

Learn how to use Pydantic in this short tutorial!
Pydantic is the most widely used data validation library for Python. It lets you structure your data, gives you type-hints and auto-complete in your IDE, and helps to serialize to and from JSON. Learn how to use it in just 10 minutes!
👉 Links
🔗 Pydantic Docs: docs.pydantic.dev/
🔗 Pydantic GitHub: github.com/samuelcolvin/pydantic
📚 Chapters
00:00 Python's Dynamic Typing Problem
02:11 How To Use Pydantic
05:04 Validating Data with Pydantic
06:36 Custom Field Validation
07:58 JSON Serialization
08:49 Pydantic vs Dataclasses
#pixegami #python

Пікірлер
  • You have a gift to explain complex things in a very simple to understand way.... great video Keep up the awesome work 💪

    @ChelseaSaint@ChelseaSaint7 ай бұрын
    • Thank you! Glad you enjoyed it :)

      @pixegami@pixegami7 ай бұрын
  • Nice video, -not too big, not to short -presents the most popular points without sticking to details -compares to alternatives Thank you!

    @user-mf1xr5ki9j@user-mf1xr5ki9j7 ай бұрын
    • Thank you!

      @pixegami@pixegami7 ай бұрын
  • The explanation on the why and what problems Pydantic solves is one of the best that i have seen. I just had to subscribe!

    @pythonantole9892@pythonantole98927 ай бұрын
    • X2

      @pipeherrera5010@pipeherrera5010Ай бұрын
  • Best moments of the video: 7:22 the whole example of Pydantic with data and custom fields validation 9:12 the alternative built-in Python datclasses 9:33 discussing differences between Pydantic and built-in dataclasses

    @UTJK.@UTJK.5 ай бұрын
  • The pydantic model still does not do strict typechecking , for eg you can pass int to a class of pydantic model which accepts str , it'll typecast the int to str and pass the check. Although there is a solution which you can use , you can import StrictStr from pydantic for such cases

    @002_aarishalam8@002_aarishalam84 ай бұрын
  • Thanks for the hint and this will less stress me finding solutions to what you have explained. Keep it up you have a natural gift to explain in a way that anyone can understand

    @sheshanaturupana2481@sheshanaturupana24815 ай бұрын
    • Thank you :) I appreciate the comment!

      @pixegami@pixegami4 ай бұрын
  • Best intro to pydantic I've seen so far. Thanks!

    @jabuci@jabuci5 ай бұрын
    • Glad you enjoyed it!

      @pixegami@pixegami5 ай бұрын
  • Coming from C++ and Rust, it's not that I want to work with Types, I kinda cannot work without them haha. So, that's the first topic I looked for when I had to do some stuff in Python. Well-made video, Mate. Thanks.

    @theintjengineer@theintjengineer5 ай бұрын
    • Thanks :) I totally understand - once you've come to rely on type-defs it's harder to work without them.

      @pixegami@pixegami5 ай бұрын
  • Very nice. Had been working with some parts of dataclasses in the past and used guard clauses instead of validators. Will try to use it in the future. Thank you

    @DaniEIdiomas@DaniEIdiomas5 ай бұрын
    • Thanks! I hope it is useful for you :)

      @pixegami@pixegami5 ай бұрын
  • Mypy solves kind of a similar problem (regarding validation), though with static type checking. It is generally more useful in my opinion, but yeah, pydantic seems to be a great tool for runtime validation

    @user-vz9cq8ci9b@user-vz9cq8ci9b7 ай бұрын
    • Sure, there's a lot of tools that solve similar problems, and I'm not saying Pydantic is necessarily the best one. Definitely use what works best for you (and if Python's inbuilt features are enough, even better).

      @pixegami@pixegami7 ай бұрын
  • Excellent video. Thank you for getting straight to the point with clarity.

    @chakib2378@chakib23783 ай бұрын
  • This is being improved upon in 3.12. I did not use it but I saw something about it in patch notes. I could be wrong but it is nice of Python to actually fix things if this is the case!

    @jason6569@jason65696 ай бұрын
    • Actually my biggest issue (type hinting) is already available in Python since 3.8 and I've found it good enough for the most part. If I need more, I go for @dataclass decorator. But I think overall, a lot of frameworks and teams still use Pydantic for the validation too.

      @pixegami@pixegami5 ай бұрын
  • Crap! First 4 videos of yours I watch and they all solve real world problems I have been facing. Freak'n AWESOME! Thank you. FYI I'm coming from the Typescript world. Saves a lot of custom decorators we create.

    @jimg8296@jimg8296Ай бұрын
  • Wow, never understood that stuff until I watched this and it's pretty simple the way you explain it. Thanks...subbed!

    @gatorpika@gatorpika3 ай бұрын
    • Awesome, thank you!

      @pixegami@pixegami2 ай бұрын
  • it's hilarious how many decades it took for python users to understand the benefit of strict typing

    @NikolajLepka@NikolajLepka5 ай бұрын
    • I suspect it's not native Python users bringing that change, but users of other strictly typed languages coming into Python and feeling like something is missing 😬

      @pixegami@pixegami4 ай бұрын
    • I think it’s hilarious that non-Python users think dynamic typing is a negative

      @OctagonalSquare@OctagonalSquare4 ай бұрын
    • @@OctagonalSquare if it wasn't, python users wouldn't be working so hard to put static typing into the language

      @NikolajLepka@NikolajLepka4 ай бұрын
    • i love dynamic typing... as much as it slows my code, i can handle errors better and create my own errors and etc... ints also have no bounds (they have, virtually but you can bypass it), extremely easy data modification support, etc etc etc... so no I'm not fighting to make it static... dynamic is what makes python python ​@@NikolajLepka

      @shaurryabaheti@shaurryabaheti3 ай бұрын
    • ​@@NikolajLepka there's nothing inherently better, or worse for that matter, with either static or dynamic typing. Users bring their own biases and preferences.

      @MikeHurt@MikeHurt2 ай бұрын
  • Man, awesome content like I never saw before! Really, really good! Thank you!

    @Nardiso@Nardiso5 ай бұрын
    • Glad you enjoyed it!

      @pixegami@pixegami5 ай бұрын
  • very useful! I'll start adding Dataclass support to my code, and when it matters, Pydantic. Subscribed!

    @JohnMitchellCalif@JohnMitchellCalif3 ай бұрын
    • Thank you, glad it was useful :)

      @pixegami@pixegami2 ай бұрын
  • very clear and concise, well organized, thanks.

    @kentuckeytom@kentuckeytom3 ай бұрын
    • Thanks!

      @pixegami@pixegami2 ай бұрын
  • Just wanted to say I love your video! Learned something new!

    @luftstolle@luftstolle5 ай бұрын
    • Thank you! Glad it was helpful :)

      @pixegami@pixegami4 ай бұрын
  • Either use a statically-typed language if you absolutely need one OR use available tools to add typedefs. Dynamic typing is a feature, not a problem.

    @drewsarkisian9375@drewsarkisian93757 ай бұрын
    • That's what I believe too and that's make it special too tbh !!

      @Holasticlogger@Holasticlogger7 ай бұрын
    • Perhaps it was unfair for me to call "lack of static typing" a problem. The real issue I'm referring to is the user experience from having type hints and validation. "Use a statically-typed language" - Although this is a nice user experience, it's rarely the top reason for choosing a language (for me anyways) - especially over say, frameworks or domain utility. "Use available tools to add typedefs" - Sure, if you only need typedefs, Python's inbuilt support is good enough. If you also want validation and deep serialisation too-well, at what point is re-implementing all those features easier/better than a pip install?

      @pixegami@pixegami7 ай бұрын
    • totally agree, dynamic typing can be powerful and flexible. if you need stricter type checking, statically-typed languages or tools like typedefs can be helpful. it's all about choosing the right tool for the job.

      @SOLDAT_MENDES@SOLDAT_MENDES7 ай бұрын
    • @@SOLDAT_MENDES The good thing about pydantic is that it's possible to have strict type checking and validations without having to change the whole language of your Python project. A lot of times it's better to install an extra library than to refactor your entire project.

      @blackdereker4023@blackdereker40235 ай бұрын
  • Amazing video, I am starting my journey in python, and pydantic appeared to me in a fastapi course, I was so confused, but now I fully understand its utility, thank you!

    @GilbertoMadeira83@GilbertoMadeira834 ай бұрын
    • Yay, glad it was helpful!

      @pixegami@pixegami3 ай бұрын
  • Awesome Pydantic summary - concise and quintessential. Actually in Pydantic v2 '@validator' is deprecated and replaced with '@field_validator'. The same is valid for these three: - 'json' method is deprecated and replaced with 'model_dump_json' instead. - 'dict' method is deprecated and replaced with 'model_dump' instead. - 'parse_raw' method is deprecated and replaced with 'model_validate_json' instead. Have that in mind. Amended code snippet accordingly: user_json_str = user.model_dump_json() user_json_obj = user.model_dump() user = User.model_validate_json(user_json_str) print(f'{user_json_str} {user_json_obj} {user}')

    @cyberslot@cyberslot4 ай бұрын
    • I see - thank you for sharing those updates :)

      @pixegami@pixegami4 ай бұрын
    • @@pixegami You're very welcome! :)

      @cyberslot@cyberslot4 ай бұрын
    • @field_validator("id") def validate_id(cls, value: int) -> int: if value

      @aryangoyal5176@aryangoyal51762 ай бұрын
  • Very clear explanation - thanks for sharing !

    @christophehanon8396@christophehanon83962 ай бұрын
    • Thank you!

      @pixegami@pixegami2 ай бұрын
  • I don't remember exactly why, but I prefer attrs. Could you compare both ? With custom validation, custom initialization for fields, also post init processing ? I think that's all that I'm using. I'm only deserializing from yaml though

    @romsthe@romsthe7 ай бұрын
    • I haven't used attrs myself, but I understand it's a bit more heavyweight than the inbuilt dataclasses. Not sure how it differs from Pydantic, but it's probably something I should compare and check out :)

      @pixegami@pixegami7 ай бұрын
  • Great Video! Thank you! Thank you! Keep up the great work!

    @Andromeda26_@Andromeda26_Ай бұрын
    • Glad you enjoyed it!

      @pixegami@pixegami27 күн бұрын
  • Great explanation!! Thanks for creating such as amazing content

    @balapraneeth9708@balapraneeth97084 ай бұрын
    • Thank you, glad you liked it!

      @pixegami@pixegami2 ай бұрын
  • Amazing video, everything was well explained, thanks!

    @luis96xd@luis96xd3 ай бұрын
    • Glad you enjoyed it!

      @pixegami@pixegami2 ай бұрын
  • Thanks, nice & clear explanation!

    @RuudJeursen@RuudJeursen4 ай бұрын
    • Glad you enjoyed it!

      @pixegami@pixegami4 ай бұрын
  • love your explanation, as advanced programmer I don't feel I spent too much time to learn something useful. Keep it up! You have a new subscriber.

    @cjbarroso@cjbarroso6 ай бұрын
    • Glad to hear it :) Thank you!

      @pixegami@pixegami5 ай бұрын
  • Very well presented. Thanks!

    @thebuggser2752@thebuggser27523 ай бұрын
    • Thanks for watching!

      @pixegami@pixegami2 ай бұрын
  • Hey man, I love the tutorial. It's very easy to follow along with your explanations. I do have one issue in that when attempting to use your @validator function in pycharm and kaggle it does not seem to work and still allows me to enter negative numbers without raising an error. i changed the value to

    @dogzabob@dogzabob3 ай бұрын
    • Thank you! Glad you're enjoying the video. Hmm, maybe there's an issue with the way you've set up the validator, but it's hard to say without seeing your code. I recommend dropping your code into ChatGPT and ask to see if it can spot the bug. But the best thing to do is probably to read through the manuals directly and understand how the validators work and how to debug them: docs.pydantic.dev/latest/concepts/validators/

      @pixegami@pixegami2 ай бұрын
  • Very useful, Thank You!

    @alexanderzikal7244@alexanderzikal72443 ай бұрын
    • Glad to hear that!

      @pixegami@pixegami2 ай бұрын
  • Thank you so very much buddy 🤩 its a great help 🥳

    @ananyamahapatra6597@ananyamahapatra659712 күн бұрын
    • Thank you, glad you liked it!

      @pixegami@pixegamiКүн бұрын
  • I love the production value of your videos (in addition to the content, of course!) I’m curious what tools you like for recording and editing videos?

    @alexhasha@alexhasha5 ай бұрын
    • Thank you! I use OBS studio to record, and I use Premiere Pro to edit. Actually my editing is really light and minimal (just direct cuts) so any editing software is fine.

      @pixegami@pixegami5 ай бұрын
  • Very helpful!

    @footflaps@footflaps2 ай бұрын
  • Dynamic typing for me is what makes python cool. If i need to switch data type, i just cast it to the type if the value are the same

    @shedrachugochukwu6245@shedrachugochukwu624513 күн бұрын
    • Hints and validation are still good for more complex data-types that can't be casted (think nested objects), or tuples where the order matters. And if you want a field that can understand multiple data types you can also use something like "Union[str, int]" or "str | int" in the type hint.

      @pixegami@pixegamiКүн бұрын
  • Can pydentic work with the terminal editors like vim or neovim?

    @Dopefeezy@Dopefeezy7 ай бұрын
    • I'm not sure, I haven't really used them. But I think all of the type hinting features in an IDE come from having a language server or linter anyway, so I think it should be possible.

      @pixegami@pixegami7 ай бұрын
  • I’m wondering how it compares with the typing package included in Python. And, is the typing module the same as mypy?

    @a0um@a0um5 ай бұрын
    • I think for just the "typing" part, Pydantic doesn't really give you much advantage over the built-in Python type hints. The real value add of Pydantic comes from its validation and serialisation abilities-and the use case is integrating with external databases or APIs with your data :)

      @pixegami@pixegami5 ай бұрын
  • Nice video, though lack of static typing is not a problem, it is a feature. It was done on purpose. Like JavaScript vs TypeScript, I would rather see Tython (Typed Python).

    @adam_fakes@adam_fakes4 ай бұрын
    • Yes, these bugs/features are very much a non issue, meaning it's a facet of the language that is lauded as much as it is hated. One could consider Sqlite's lack of types to be a feature, or two pass versus one pass interpreters; neither is better. Python occupies that odd zone where it might be used for a ten line convenience script, or for the basis of a serious application.

      @catsupchutney@catsupchutney3 ай бұрын
    • I think it's more of a "UX" problem for developers who are used to the nice experience that static typing or typed languages give you. So it's not really a big deal, but it does make the experience better for many people.

      @pixegami@pixegami3 ай бұрын
  • I love your videos man. Just a quick question. Can't we define the default types of our instances in python for example x = 10 x: int = 10

    @mohibahmed5098@mohibahmed509813 күн бұрын
    • Thanks! Glad you enjoyed it. Yes if you just want type hints, that's exactly what I'd do. Pydantic becomes more useful if you need validation, or you need to work with APIs or databases.

      @pixegami@pixegamiКүн бұрын
  • Thanks. U have explained very well

    @yashwanthsai3401@yashwanthsai34014 ай бұрын
    • Thank you!

      @pixegami@pixegami4 ай бұрын
  • Great explanation. Btw which vs code theme u r using?

    @ahmednfaihi9091@ahmednfaihi90915 ай бұрын
    • Monokai Pro :)

      @pixegami@pixegami5 ай бұрын
  • Thanks Bro!

    @youssef.elyaakoubi@youssef.elyaakoubi8 күн бұрын
    • You're welcome!

      @pixegami@pixegamiКүн бұрын
  • Awesome video. But what about including Marshmallow on your comparison matrix?

    @claudemirtonmacedo6639@claudemirtonmacedo66394 ай бұрын
    • Thanks for the suggestion. I haven't used it before. But I'll leave a link here for anyone who is reading and is curious: marshmallow.readthedocs.io/en/stable/why.html

      @pixegami@pixegami4 ай бұрын
  • How do these things affect performance? I'd assume they add a good amount of overhead. If you are going to take the pain of making things statically typed, why not just used a language that is efficient with it. Kotlin is a great bet. Is the use case to have the external api typed strongly with internals relaxed so that you can stay in Python and still use it's libraries for an application? Just curious.

    @AdityaDodda@AdityaDodda4 ай бұрын
    • Thanks for the great question. The performance hit is not noticeable (probably a few microseconds on each request at most) - and I don't think there's any Python use-cases where that would matter (if milli-sec latency was a concern, then Python itself already isn't a good fit). As for using a (different) statically typed language - sure, if you are picking a new language to learn or starting a new project. But Python has a lot of libraries/SDKs/support that do not exist in other languages. It also has high adoption. Some people may just want to continue using Python but just want to upgrade this one part of the user experience. I think the best use-case for Pydantic is for writing Python logic that interacts with other services (or databases) via API, and you want an easy way to validate/serialize your data.

      @pixegami@pixegami4 ай бұрын
  • Thank you , like this video so much.

    @guohaigao2397@guohaigao23974 ай бұрын
    • Glad you liked it!

      @pixegami@pixegami4 ай бұрын
  • great explanation!

    @fatmasliti2161@fatmasliti21614 ай бұрын
    • Thanks!

      @pixegami@pixegami4 ай бұрын
  • New here, also not a software engineer by trade. Is this essentially allowing you to create structs like you would in golang? Learning here, just seemed familiar in its use case. Any commentary is welcomed. Thanks!

    @jteds711@jteds7116 ай бұрын
    • Welcome! Yes, it's quite similar. Structs is built into Golang (which is "typed" by design). Python is dynamic (or "untyped"), so to get similar functionality you need to add it in deliberate (using something like Pydantic).

      @pixegami@pixegami5 ай бұрын
    • @@pixegami The other similarity is that Golang's marshalling in the standard library also provides the serialization/deserialization part (which is what pydantic actually offers as opposed to dataclass libraries like attrs).

      @BosonCollider@BosonCollider5 ай бұрын
    • @@BosonCollider Oh that’s cool, I’m not super familiar with Go and didn’t know they did JSON marshalling by default. TIL!

      @pixegami@pixegami5 ай бұрын
  • Excellent vidéo. What tools do you use to create this kind of content

    @nulops@nulops3 ай бұрын
    • Thank you! I use OBS to record the video/screen, Adobe Premiere to edit it, and I also have a couple of my own Python scripts to create slides/titles/diagrams.

      @pixegami@pixegami2 ай бұрын
  • Great video, thanks a lot! I hear some background noise in your audio though. Do you not use audacity to remove the background noise?

    @mayanksharma6927@mayanksharma69273 ай бұрын
    • Thanks! I live in quite a loud area, so a lot of noise does get through. I've used software to remove it in the past, but sometimes it does make the sound a bit less crisp. Thanks for the feedback though, I'm working on it :)

      @pixegami@pixegami2 ай бұрын
    • ​@@pixegamiI sometimes use the app Dolby On to remove noise, it's very good.

      @mayanksharma6927@mayanksharma69272 ай бұрын
  • I wouldn’t call dynamic typing as an “issue”. It’s just how the language is, with its own design.

    @404nohandlefound@404nohandlefound5 ай бұрын
    • True, it’s not really the issue. It’s just an oversimplification of what the real issue is: the user experience.

      @pixegami@pixegami5 ай бұрын
  • Good tutorial!

    @fengjeremy7878@fengjeremy78784 ай бұрын
    • Thanks!

      @pixegami@pixegami3 ай бұрын
  • Do we really need data validation? At 5:55 ? I mean, we can change the data type of age variable when we get from user, right? Like age= int(age) . Correct me if iam wrong.

    @LeeYeon-qv1tz@LeeYeon-qv1tz4 ай бұрын
    • What you're suggesting is "casting" the data. It works if it's small transitions of things like ints to floats. But the validation becomes useful if it's something more complex like named dicts, such as (e.g. "vCPU: [1024, 2048, 9096]"), or percentages (e.g. must be between 0-100).

      @pixegami@pixegami2 ай бұрын
    • ​@@pixegamithanks!

      @LeeYeon-qv1tz@LeeYeon-qv1tz2 ай бұрын
  • how do you create those code boxes? It looks so good!

    @deepschoolai@deepschoolai3 ай бұрын
    • I used a custom script in Python, using libraries like pygments.org/ and PIL :)

      @pixegami@pixegami2 ай бұрын
  • Not sure what the fascination with type is. It’s a helpful feature to not have to type variables. The web in general benefits from this, if one is sensible and clever in how to use this - and when validation of specific types is needed, induce it.

    @morespinach9832@morespinach983214 күн бұрын
    • It's think just a big "user experience" problem for a lot of people who are more comfortable working with types/validation.

      @pixegami@pixegamiКүн бұрын
  • We can mix wit Sqlalchemy models?

    @chriskeo392@chriskeo3925 ай бұрын
  • Ok, I was thinking what’s the need for this if you are organized. But, when you said that the library has built in format validation for things like emails I now have a lightbulb above my head that has literally erased so many lines of code later using any type of my own format validation.

    @PeterRichardsandYoureNot@PeterRichardsandYoureNot4 ай бұрын
    • Thanks for sharing that lightbulb moment :) I'm glad the example helped it to click.

      @pixegami@pixegami3 ай бұрын
  • Nice presentation.

    @UTJK.@UTJK.5 ай бұрын
    • Glad you liked it

      @pixegami@pixegami5 ай бұрын
  • I always do user_input = int(user_input) etc after each input only where needed which prevents every such error.

    @ThankYouESM@ThankYouESMАй бұрын
  • If you want static typing in Python just use Numpy. It's both faster and takes less memory.

    @nas8318@nas83189 күн бұрын
    • Numpy is excellent, but I think that's solving a different problem for people who want to add type hints and validation to their custom data types.

      @pixegami@pixegamiКүн бұрын
  • 🎯 Key Takeaways for quick navigation: 00:00 🐍 *Python's Dynamic Typing Limitation* - Discusses the limitation of Python's dynamic typing and the problems it can cause, - Highlights the risk of accidental creation of invalid objects due to loosely defined variable types, - Emphasizes the difficulties in debugging failures caused by incorrect use of dynamic typing. 01:53 🎁 *Introduction to Pydantic* - Introduces Pydantic as a powerful tool to model data, with validation capabilities for avoiding the problems caused by Python's dynamic typing, - Discusses Pydantic's use in popular Python modules and its benefits, - Explains how Pydantic improves IDE support for type-hints and allows for easy serialization. 03:19 💻 *Creating and Using Pydantic Models* - Demonstrates how to create models in Pydantic and how Pydantic ensures that only valid data is used in models, - Shows the added advantage of type hinting provided by Pydantic models in an IDE, - Illustrates custom data validation in Pydantic by enforcing that all account IDs must be a positive number. 06:36 🔄 *JSON Serialization with Pydantic* - Details how Pydantic supports JSON serialization, which proves beneficial in integrating Python code with external applications, - Provides an example of converting a Pydantic model to a JSON string using the JSON method. 08:03 📊 *Comparing Pydantic to Dataclasses* - Compares Pydantic to Python's inbuilt dataclasses module, - Evaluates both modules based on type hints, data validation, and JSON serialization, - Suggests appropriate cases of usage for both modules depending on specific programming needs. Made with HARPA AI

    @roberthuff3122@roberthuff31223 ай бұрын
  • I have experience using Pydantic, but it's somehow wordy for me especially if I don't have a plan to do OOP. My preference would be "mypy" (or pytype) + "typedict" instead.

    @thebatprem9271@thebatprem92715 ай бұрын
    • Yes. Truthfully I just stick to dataclasses since that’s good enough for 90% of my use cases too. But here I just wanted to open with a common problem as a way to introduce Pydantic.

      @pixegami@pixegami5 ай бұрын
  • Dynamic typing is what Python stands for. I use type hints only in functions or methods parameters for better understanding to other developers.

    @krzysiekkrzysiek9059@krzysiekkrzysiek90597 ай бұрын
    • I agree - I just wanted to highlight the value of having type hints and validation. Normally for my own projects, unless I need all three Pydantic features I mentioned (serialisation, validation, and typing), just the built-in type hinting is good enough.

      @pixegami@pixegami7 ай бұрын
    • When you are building an API or a CLI application, validations is a must.

      @blackdereker4023@blackdereker40235 ай бұрын
  • We can use it like this in normal python myvalue:str ="hello"

    @Learnbyflow@Learnbyflow4 ай бұрын
  • I like pydantic a lot. But when working on projects where packages size is a consideration, I couldnt understand why such lightweight functionality requires code that is so heavy. That's a bummer

    @hananamar2686@hananamar26864 ай бұрын
    • Hmm, what do you mean by "heavy"? As in the implementation of Pydantic itself? On that note-if you just want type hinting, Python has that built-in. And if you need structs, @dataclass will do it for you (also built in). But if you need validation and serialisation that's probably when Pydantic might be useful.

      @pixegami@pixegami4 ай бұрын
  • Thank you

    @jairajsahgal7101@jairajsahgal71014 ай бұрын
    • You're welcome!

      @pixegami@pixegami4 ай бұрын
  • How is this different (or better) than dataclasses?

    @nedac279@nedac2795 ай бұрын
    • I cover this a bit towards the end of the video, but Pydantic gives you a bit more validation and serialisation capabilities. It's also generally more widely used in frameworks that deal with APIs or databases (e.g. FastAPI). But if the typing and structure is all I want, I'd go with dataclasses personally :)

      @pixegami@pixegami5 ай бұрын
  • What is your vscode theme and font?

    @akshatgupta1658@akshatgupta16582 ай бұрын
    • I'm using Monokai Pro theme, and Roboto Mono font.

      @pixegami@pixegami2 ай бұрын
  • Это всё называется - костыли)

    @tirsky@tirsky4 ай бұрын
  • The bigger problem is the GIL. Although I can really see how accidentally changing a variable's type might cause a difficult to spot problem in a large program.

    @bobchannell3553@bobchannell35533 ай бұрын
    • Haha yes, truly the "biggest problem" is a subjective thing. But the static typing thing is just something I hear people complain about most often (anecdotally).

      @pixegami@pixegami2 ай бұрын
  • I would say the 'lack of static typing' is a feature, not a bug of the Python language.

    @PaulaBean@PaulaBean2 ай бұрын
    • I think everyone has different views about the technology and implementation itself. But if we consider the actual user experience, I don't think anyone _prefers_ to have errors show up late in the runtime, or lack the type hints at coding time.

      @pixegami@pixegami2 ай бұрын
    • @@pixegami Indeed. I think Swift hits this sweet spot.

      @PaulaBean@PaulaBean2 ай бұрын
  • thank you for the explanation, and please next time leave a link for donation

    @programmingwiththotho4641@programmingwiththotho4641Ай бұрын
    • Thank you! Glad it was useful :)

      @pixegami@pixegamiАй бұрын
  • "One of the biggest issues is Python's lack of static typing" You got that the wrong way around. The biggest issue in most typed languages is that the typing system is static. Python does something incredibly useful: value and runtime typing. Static typing is not the end-game of programming language systems, and the assumption that static typing will make programs fault-free is flawed. Without this assumption static typing is left with: being useful for code completion in IDEs, efficiency optimization in resource-constrained environments, and documentation. Python once again does something incredibly useful here: type annotations. Type annotations support the code completion case, and support the code documentation case, while also being part of the runtime information which can be leverages at runtime to perform validation or type checking. As for resource-constraints, it's Python, an interpreted language, if resource constraints where an issue, you wouldn't be using Python or any other interpreted language. Reflect on these questions: When and where do the errors happen? And what am I doing about it? The answers are: During runtime when in use. And adding user input validation code. Notice the absence of static or "compile time typing". Most static typing can only detect faults during compile time and not during runtime, since during runtime most static typed languages don't have typing information. Either the language compiler doesn't add this typing information, or the developers prefer to remove the typing information for runtime for various "optimization" reasons. Some examples of this: TypeScript is only static typed, yet the core runtime, JavaScript, has a very loose runtime type concept, and the TypeScript static types are usually stripped in transpilation. C has static typed compilers, but assembly or hex-code itself is only minimally typed (bits, bytes, addresses). Additionally, static typing systems in the most common languages are incredibly naive and in some cases counter-productive. Classic and everyday examples of this can be found in the Java-memes. These memes are founded on the reality of regular Java code regularly requiring extensive type coupling code in the form of Interface/Adapter/Factory and the resulting coupling code, solely transforming namespaced data types from almost identical static type hierarchy to another. In professional environment, this code incurs heavy technical depths: it makes coupling two or more systems incredibly brittle, and is a great source of simple programming errors. Another feature that points to problems with static typing systems are Generics. A complex and error-fraud feature in many languages, developed to somehow improve code reusability while mostly simply increasing code base complexity. In addition to Python's type annotations, other solutions developed from these typing problems are Test Driven Development and Unit Testing. These cover the runtime problems that are not covered by static typing. Another great solution to the coupling types problem are Go Interfaces. Just to make this clear: Static typing does provide value, but it's extremely specific where and when it provides that value. And it's these specifics exactly that don't make static typing the ideal solution to programming language complexity.

    @FrederikSchumacher@FrederikSchumacher5 ай бұрын
    • A thoughtful deep-dive into the topic and good counter-arguments for what's claimed in the video. I appreciate you sharing these thoughts (and I don't disagree with them either - I felt my script may have been too simplified/opinionated and lost a lot of critical detail).

      @pixegami@pixegami5 ай бұрын
    • @@pixegami I appreciate you taking time to answer in such a reflective and respectful way. Other than the premise at the very start, your video is a great introduction to validation and introduction to Pydantic. I enjoyed you including that example for email address validation, which highlights one of the issues with typing systems and validation systems very well. It's an example I use regularly when discussing typing and teaching junior developers, because it highlights the confusion of typing vs validation very well. Validation can be expressed via (static) types, but it must feature runtime code. Some languages like Python allow implementing this via typing, while others (like C/C++) cannot or only with great difficulty. And while Pydantic presents this as declarative feature looking like (static) typing, it constructs code that's pretty much pure runtime. The video title is a little provocative and click-baity, but can't say it's incorrect, as input validation is one of the most common problems developers have to solve in many applications in many languages - which includes Python. Even as viewer I understand how "the algoritm" pushes creators into doing this, I just wish they didn't have to. Here's an idea if you want to explore both validation and decorators more perhaps in a future video on advanced Python - an extension to your previous primer video on decorators and this video: You can leverage the type annotations and decorators to implement "functional" validation on function arguments to enable code like: @validate_args def cool_func(person_value: has_keys("mail"), mail_value: matches_pattern("\w+@\w+")): person_value["mail"] = mail_value This can of course be extended into less trivial and less nonsensical applications. It's mostly a more "functional" approach to the classic declarative types approach and more an exercise. Personally I mostly prefer declarative types for this sort of thing. However, I find such code examples help understand why and how several Python concepts exist in the form they do.

      @FrederikSchumacher@FrederikSchumacher5 ай бұрын
  • I really don't understand why not just using a typed programming language? Don't get me wrong. I love python, but sometimes it is not the right tool. For small cli applications I love python. But it's not the right tool for everything. The last videos I watched about python was all about getting around it's downsides. Especially the Gil and multiprocessing. Now a way to make it typed? For me that's the wrong way. When I want a typed PL also with parallel processing I just use dotnet. When I want to have a beautiful UI with the best performance, I use c++ with Qt. When I want a small and fast to implement webservice I use python, as long as I don't need some multiprocessing. What I try to say is, that there is the right tool for every problem. You don't have to use the same tool for everything.

    @kaimildner2153@kaimildner21533 ай бұрын
    • I think the difference in perspective for me here is that Python's typing system (nor multi-processing or GIL) are big enough factors to make it not the right tool for the job in a lot of use-cases. For example, it's more important that you're using a language that has the vast library selection that Python has (especially in DS/ML/AI), or has wide platform and SDK support, or maybe it's a tactical decision because your team already knows it (and it's easy to hire for). At that stage, the typing experience just kinda becomes a sharp edge that people who have already settled on Python tend to focus on. Not enough of a show stopper to migrate to Rust or GoLang, but also painful enough to look for an easy solution to.

      @pixegami@pixegami2 ай бұрын
  • 1:24, so age is, in fact, not just a number. It's settled now.

    @thygrrr@thygrrr5 ай бұрын
    • It never was :P

      @pixegami@pixegami5 ай бұрын
  • nice video, but for non-negatives you only declare Field(ge=0)

    @lupusexperience@lupusexperience4 ай бұрын
    • Thank you!

      @pixegami@pixegami4 ай бұрын
  • 00:02 Pydantic module helps solve Python's lack of static typing. 01:20 Pydantic is a data validation library in Python with powerful tools to model and validate data. 02:49 Pydantic allows you to create models in Python with type hints 04:12 Pydantic provides data validation and type hinting, making code easier to work with 05:36 Pydantic provides built-in validation for different types of data. 07:02 Pydantic provides built-in support for JSON serialization. 08:26 Pydantic provides easy integration with JSON and external applications 09:50 Pydantic is recommended for complex data models and JSON serialization. Crafted by Merlin AI.

    @aitools24@aitools246 ай бұрын
  • Ide theme name?

    @shahiburRM@shahiburRM4 ай бұрын
    • Monokai Pro :)

      @pixegami@pixegami4 ай бұрын
  • Python debugging needs to up its game. So it cant tell you where the error is coming from and the reason for the error? That’s weak.

    @nicholastoo858@nicholastoo8582 ай бұрын
  • Problem with static typing in python is not true.Theres an optional typing module for python for your information.

    @kikocometa@kikocometa4 ай бұрын
    • Yup, if you just want to get type hints, then I recommend just using the built-in Python features (I think I also mentioned them at the end of the video).

      @pixegami@pixegami4 ай бұрын
  • If your software has to fail, it's better it fails as early as possible. - pixegami

    @noname2588o@noname2588o27 күн бұрын
  • Still easier to code using type-hinted python than to write java 😂

    @iwatchtvwithportal5367@iwatchtvwithportal5367Ай бұрын
  • People are still writing codes in notepad?

    @jnsougata@jnsougata4 ай бұрын
  • "If your software has to fail, then it is better to fail as early as possible"

    @amirjamal4803@amirjamal48033 ай бұрын
    • I agree :)

      @pixegami@pixegami2 ай бұрын
  • If you need data typing, why not use another language that uses it? I don't understand why you would use python and then use an external library to bend it to your needs. Why not use go?

    @fstemarie@fstemarie5 ай бұрын
    • Hmm, as nice as data-typing is, I don't think it's a first-class criteria for language section. The most important things will probably be stuff like: 1) what frameworks/libraries are available? 2) what SDKs do the services I want to use support? 3) How popular is the language (e.g. if you wanted to hire people to work on it)? That said, it's still a commonly cited pain point amongst Python developers, and a lot of popular libraries have approached this problem using libraries like Pydantic. This is especially useful if you need to save/store the data between databases or with other apps as well.

      @pixegami@pixegami5 ай бұрын
    • pydantic is about validation. Look at how FastAPI is using it.

      @minciNashu@minciNashu5 ай бұрын
  • 👍

    @Andy_B.@Andy_B.Ай бұрын
  • So this is like Zod for python.

    @Ironication@Ironication6 ай бұрын
  • gj!

    @agnichatian@agnichatian2 ай бұрын
  • Variables don't have types in Python. The value they keep has a type.

    @egonkirchof@egonkirchof2 ай бұрын
  • Mojo?

    @RodyDavis@RodyDavis5 ай бұрын
    • I'm actually just starting to learn about it!

      @pixegami@pixegami5 ай бұрын
  • what about marshmallow

    @liuzhibo3918@liuzhibo39184 ай бұрын
    • I haven't looked into that yet :( I'm a little behind the curve...

      @pixegami@pixegami4 ай бұрын
  • I really don’t see the actual point. If you can’t use dynamic typing without getting mixed up, then use a language with strict typing. It’s like buying an F-150 but putting a Ram’s engine in it. Just buy the Ram to begin with. If you feel a feature of a programming language is a problem, then you need to use a different language or learn to leverage the feature better

    @OctagonalSquare@OctagonalSquare4 ай бұрын
    • Hmm, I think the analogy of the "typing" being the engine doesn't really hit the mark here. It's more of a comfort feature, so in terms of car analogy, it's more like air-conditioned seats or tinted windows or a sun-roof. Maybe you want the engine of the F-150 (i.e. Python's vast libraries, ubiquity in AI/data, and wide platform/SDK support), but just want UX experience of runtime type validation.

      @pixegami@pixegami2 ай бұрын
  • emmm, it's time for some neural nets.

    @hanyanglee9018@hanyanglee90185 ай бұрын
  • Static typing and mandatory type declarations are not the same thing. Type inference can make the latter unnecessary.

    @0LoneTech@0LoneTech5 ай бұрын
  • First of all, Python's dynamic typing is not a problem. Secondly, even if it was, it's not the problem Pydantic is trying to solve. I can't think of a problem where your Person example results in hard-to-find issues, or any issue at all, for that matter. If the age variable absolutely has to be an integer, your program will likely use it in a way that only integers can be used, and Python's duck typing will infer that you can only pass an integer to it. Pydantic is meant for data parsing. Not for validation (in the dictionary sense of the word).

    @ThomasVanhelden@ThomasVanhelden5 ай бұрын
    • Thanks for the thoughtful counter-point. I do admit the title/intro was a bit click-baity but I still think the data-validation and type-hinting aspects of Pydantic are extremely useful. Although truth be told, if typing is all I wanted I would use @dataclass and type hints instead (I think I mentioned that towards the end of the video too).

      @pixegami@pixegami5 ай бұрын
  • If you use Pydantic to model internal app state, you are doing it wrong.

    @SkyyySi@SkyyySi5 ай бұрын
    • Right. If you don't need the validation and serialisation capabilities, then dataclasses is a better choice.

      @pixegami@pixegami5 ай бұрын
  • I think that people saying Python's dynamic typing is a problem are doing Python wrong. They're using Python to write C. Python's dynamic typing is a benefit, not a problem. And with type hinting, it's the best of both worlds. You can keep track of your data types without being tied to anything.

    @CristiNeagu@CristiNeagu5 ай бұрын
    • Good perspective :)

      @pixegami@pixegami5 ай бұрын
  • Do people really get confused about what their variables are? I always prefix all my vars with tags that identify what they are: i_ for int, str_, df_, ld_ for list of dicts, etc

    @5508Vanderdekken@5508Vanderdekken4 ай бұрын
    • This is called "Hungarian Notation" and it used to be quite a widespread tactic. But I think since IDEs improved and Python support for type hinting has been added, the way to signal the type is usually just to use the built-in type hints now. But back to the topic- the "typing" feature is a nice introduction to one of the main problem Pydantic solves (since it's quite a common grievance in Python). However the library offers more value (over type-hints) in its validation and serialisation capabilities.

      @pixegami@pixegami4 ай бұрын
  • - Understand the drawbacks of Python's dynamic typing at [0:04]. - Recognize the potential for creating invalid objects in Python at [1:06]. - Consider using Pydantic to solve type-related issues in Python at [1:49]. - Install Pydantic to enhance data validation and typing at [2:53]. - Create a Pydantic model by inheriting from the base model class at [2:59]. - Utilize Pydantic's type hints and autocomplete for easier coding at [3:49]. - Implement Pydantic's data validation to catch errors early at [5:03]. - Add custom validation logic to your Pydantic model at [6:43]. - Use Pydantic's JSON serialization for easy data interchange at [8:00]. - Compare Pydantic to Python's built-in dataclasses for your needs at [8:47].

    @ReflectionOcean@ReflectionOcean4 ай бұрын
  • If you're incapable of maintaining a codebase without static typing then you should reconsider your career. Muddling data types is a myth. It just doesn't happen. That's why none of the major libraries use type hinting. It's not a real issue.

    @davidmurphy563@davidmurphy56326 күн бұрын
    • I think if the Python community (and creator) saw it that way, then PEP 484 would not have passed: "There has now been enough 3rd party usage for static type analysis that the community would benefit from a standard vocabulary and baseline tools within the standard library." This isn't about making it mandatory, it's just about offering it as an option for those who prefer it (and it seems like a lot of people do).

      @pixegami@pixegami18 күн бұрын
    • @@pixegami I entirely agree with the statement in 484. I would not have type hinting removed from python. There are clear use cases; it can be helpful for beginners who often struggle to understand types, immensely useful tools like dataclasses employ them and they save time / boilerplate, it allows you to recompile into C for speed and it can help with IDE tools, albeit marginally. Rational reasons aside, the reality is that many people are coming from strongly typed languages (whose strong typing serves a performance purpose) and it can just give them a little familiarity / a crutch. I've no objections to any of that. I take issue only with the prevailing myth that type hinting saves python from some natural dynamic state of type spaghetti and _proper python code_ is type hinted. That's false. Pick the top libraries/frameworks in python written and maintained by some of the best coders of the language; requests, numpy, pandas, django, a long list. There's no type hinting and if there were a nett benefit they would take advantage of it. In that context, forcing type hinting is not good practice in my view.

      @davidmurphy563@davidmurphy56318 күн бұрын
KZhead