▲ | BiteCode_dev 4 days ago | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
You can certainly do it, but since serialization and validation are the main benefit from using Pydantic, I/O are why it exists. Outside of I/O, the whole machinery has little use. And since pydantic models are used by introspection to build APIs, automatic deserializer and arg parsing, making it fit the I/O is where the money is. Also, remember that despite all the improved perf of pydantic recently, they are still more expensive than dataclass, themselves more than classes. They are 8 times more expensive to instanciate than regular classes, but above all, attribute access is 50% slower. Now I get that in Python this is not a primary concern, but still, pydantic is not a free lunch. I'd say it's also important to state what it conveys. When I see a Pydantic objects, I expect some I/O somewhere. Breaking this expectation would take me by surprise and lower my trust of the rest of the code. Unless you are deep in defensive programming, there is no reason to validate input far from the boundaries of the program. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
▲ | JackSlateur 4 days ago | parent [-] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This is true, there is a performance cost Apart from what has been said, I find pydantic interesting even in the middle of my code: it can be seen as an overpowered assert It helps making sure that the complex data structure returned by that method is valid (for instance) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|