7 ms·
This principle is how pydantic[0] utterly revolutionized my python development experience. I went from constantly having to test functions in repls, writing ton
by kortex 5y ago
This principle is how pydantic[0] utterly revolutionized my python development experience. I went from constantly having to test functions in repls, writing tons of validation boilerplate, and still getting TypeErrors and NoneTypeErrors and AttributeErrors left and right to like...just writing code. And it working! Like one time I wrote a few hundred lines of python over the course of a day and then just ran it... and it worked. I just sat there shocked, waiting for the inevitable crash and traceback to dive in and fix something, but it never came. In Python! Incredible.
[0] https://pydantic-docs.helpmanual.io/ https://pydantic-docs.helpmanual.io/
- JPKab 5y agoCurious, but how does pydantic compare to marshmallow? I'm currently using marshmallow in a project, specifically using the functionality that builds parsers from dataclasses. I was curious what the differences were.
- AlphaSite 5y agoMarshmallow feels like a more flexible project, but it needs more work for a similar effect IMO. My cons are: - Uses some dsl to define types - Doesn’t marshal to model objects by default, but from DICT to DICT My pro is: - much more configurable and powerful
- goodoldneon 5y agoMy company soured on Marshmallow a while back due to performance. Maybe it’s gotten a lot better, but it has a bad reputation here. Most people seem really happy after we started using Pydantic. Take all that with a grain of salt since I’m just parroting hearsay :)
- kortex 5y agoPersonal opinion: pydantic crushes Marshmallow. Not even a fair fight. Pydantic is more performant, has better mypy/linter integration, and more powerful data model. We had a project where we pre-emptively used marshmallow to marshall/validate data. Had to remove that and solely use it at the ORM layer because of performance (and it still struggles). I haven't used pydantic's ORM integration, but I don't hesitate to use pydantic models everywhere as business logic classes unless I need ludicrous speed. That's all opinion, but I'd definitely give pydantic a swing.
- dnadler 5y agoWe've had a similar experience using pydantic. We integrated it quite tightly with a django project and it's been awesome.
- theptip 5y agoWhere did you find it valuable to wire in to Django?
- dnadler 5y agoWe effectively use them as serializers, and also as a way to allow users to interact with the models in a controlled manner. In our app, django essentially sits between a compute cluster and a front end. The pydantic objects are used to define the work to be done on the compute cluster.
- jimmaswell 5y agoI've found this to be simply a matter of experience, not tooling. As the years go by I find the majority of my code just working right - never touched anything like pydantic or validation boilerplate for my own code, besides having to write unit tests as an afterthought at work to keep the coverage metric up.
- vikingcaffiene 5y agoMan, for a dev with as much experience as you’re claiming to have, this comment ain’t a great look. I’d argue that the more experience you get the more you write code for other people which involves adding lots of tooling, tests, etc. Even if the code works the first time, a more senior dev will make sure others have a “pit of success” they can fall into. This involves a lot more than just some “unit tests as an afterthought to keep the coverage up.”
- mixmastamyk 5y agoAdding lots, no. I agree with the grandparent. Keeping the code simple, finding the right abstractions, untangling coupling, gets the most bang for the buck. See the “beyond pep8” talk for a enlightened perspective. That said, lightweight testing and tools like pyflakes to prevent egregious errors helps an experienced dev write very productively. Typing helps the most with large, venerable projects with numerous devs of differing experience levels.
- jolux 5y agoTyping is just another guardrail, it's not a substitute for finding the right abstractions and keeping things simple.
- hardwaregeek 5y agoI agree but guardrails are pretty awesome. And if people were saying "don't use guardrails, just drive properly", I'd ask why they think guardrails and driving properly are mutually exclusive.
- mixmastamyk 5y agoSounded familiar. Worked correctly the first time it was run, sans types. https://www.python.org/about/success/esr/ https://www.python.org/about/success/esr/
- exdsq 5y agoJust started working on a new SaaS startup and using FastAPI & Pydantic. The development experience has been great.
- omegalulw 5y agoCan't you do this already with Python type annotations? I am a fan of typing in general (not just for data model as this seems to be) and using types everywhere saves a lot of debugging hassle and even allows for catching some bugs with static analysis.
- ElevenPhonons 5y agoI've also found Pydantic to be a valuable library to use. However, it does have a strongly opinionated approach to casting that can sometimes yield non-obvious results. This behavior is documented and I would suggest new potential adopters of the library to explore this casting/coerce feature in the context of your product/app requirements. For the most part, it's not an huge issue, but I've run into a few surprising cases. For example, sys.maxint, 0, '-7', 'inf', float('-inf') are all valid datetime formats. - https://pydantic-docs.helpmanual.io/usage/models/#data-conversion https://pydantic-docs.helpmanual.io/usage/models/#data-conve... - https://gist.github.com/mpkocher/30569c53dc3552bc5ad73e09b4852492 https://gist.github.com/mpkocher/30569c53dc3552bc5ad73e09b48...
- nooorofe 5y agoThere is one thing concerns me about type annotation. With annotation Python code looks like Java code, but with it I don't have advantages of Java (like speed). Besides I see things like `x: typing.Union[str, typing.Any]` - maybe better no annotation.
- kortex 5y ago> x: typing.Union[str, typing.Any] That's absolutely a valid and useful annotation. It tells me, and autocomplete, that "x" is probably a str, more likely than not, but I need to be aware that it might not be.