Hacker News new | ask | show | jobs
by kaashif 1641 days ago
Pydantic is dataclasses, except types are validated at runtime? It's nice and looks just like a normal dataclass looking at https://pydantic-docs.helpmanual.io/

For any larger program, pervasive type annotations and "compile" time checking with mypy is a really good idea though, which somewhat lessens the need for runtime checking.

2 comments

Pydantic types will be checked by mypy or any other static type analysis tool as well.

I don’t expect any type-related thing to be remotely safe in Python without applying at least mypy and pylint, potentially pyright as well, plus, as always with an interpreted language, unit tests for typing issues that would be caught by a compiler in another language

Welp not sure why I caught downvotes for using the two most common static analysis tools for Python, but here we are on Hacker News
Likely the reason is that you really only need just mypy. It handles the type issues. Pylint is useful, but doesn't really overlap with mypy in terms of what it catches, and there's no need to use pyright or to write type-verifying unit tests, or to do runtime type validation if you have mypy.

Using mypy gives you the type safety equivalent of a compiled language. If you're using mypy, you don't need any additional validation that you wouldn't use in java or c++. I didn't downvote you, but the needless defense in depth is weird.

“Using mypy gives you the type safety if a compiled language” is far from the truth. Each tool has things that they don’t catch, or things that they sometimes false alert on.

Yeah mypy will get you maybe 90% of the way there, but the swiss cheese approach of stacking a few tools, even though there are redundancies, helps plug almost all of the holes.

People can argue about unit tests all day, but there’s very little cost to stacking multiple static analysis tools.

Pydantic works with mypy, so you have validation at build-time and parsing at runtime.
Last time I checked. Constrained types do not work with mypy out of the box.

https://github.com/samuelcolvin/pydantic/issues/975

Thanks! I didn’t know about that.