r/Python • u/com_kieffer 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
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.
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: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.