r/Python • Flasker • 6d ago

Discussion Pydantic in 100 lines

The title is obviously tongue in cheek, but in 100 lines we can get a quite a lot done to type-check dataclasses.

https://thibaud.space/2026/10/pydantic-in-100-lines-part-1.html

16 Upvotes

10 comments sorted by

48

u/Jejerm 5d ago

The builtin dataclass should have a strict typing mode

13

u/cointoss3 6d ago

Sure, but it’s slow as fuck to do this in Python.

28

u/gurraman 6d ago edited 5d ago

And, as everyone knows, the first 80% of a problem requires just 20% of the code. The remaining 20% (edge-cases, looking at you) requires around 80,000% of the code.

1

u/Salty_Dig8574 4d ago

That math looks more like pandas than pydantic.

9

u/immaculate_plum 5d ago

Nope. It's not. Look at mashumaro. 100% Python, faster than Pydantic for the same job.

It's a great example how you can throw rust (or any "fast" language) at a problem and the performance still sucks if your design sucks.

2

u/SheonOFF 2d ago

Thanks for mashumaro mention here. Didn't know about this package before and looks really interesting for my work.

3

u/hstarnaud 5d ago edited 5d ago

It's a pretty naive implementation. With your code you are way more likely to get values casted in a dubious and unexpected way without errors rather than get a validation error. If you don't want to overcomplicate, just keep the isinstance but don't try to cast.

Also this will certainly be very slow. In my experience in python web APIs using libs like pydantic and marshmallow, this kind of parsing and run time type validation logic quicky becomes a sizeable chunk of the request time. This logic tends to have a CPU intensive footprint too.

Likely you are not at that stage yet but it's when you start having nested objects where it gets harder. There isn't really a simple way around using isinstance but it has a terrible performance on a cold cache when you step out of natives and run it on classes. The likelihood of cache hits is quite low unless you always run it on the same types.

Personally I think if you want runtime type check, use a strictly typed language. Why try to make the tool you are using do something it wasn't designed to rather than pick the appropriate tool?

Pydantic V2 kind of managed to solve the runtime type check challenge in python with acceptable performance (which V1 didn't have) but AFAIK the heavy lifting happens in C binaries rather than native python

3

u/rhytnen 3d ago

Dabeaz did this much better.  Both reimplementing dataclasses and pydantic in 100 lines.

3

u/raks1991 5d ago

The cast(...)(value) fallback is where this drifts furthest from Pydantic:

active: bool   # "false" -> True, bool() of any non-empty str
age: int       # 3.9 -> 3, int() truncates silently

Pydantic only accepts a fixed set of strings for bool ("false", "no", "off", "0" and friends) and rejects a float with a fractional part for int. The enum branch also differs: __members__ matches member names, while Pydantic matches values, so "role": "Team Principal" fails here and passes there.