2 ms·
I don't think this is really true, I think native data classes can do just about everything you need, it's just that you need some tooling around them to get th
by flanked-evergl 1y ago
I don't think this is really true, I think native data classes can do just about everything you need, it's just that you need some tooling around them to get the same as Pydantic offers. I'm pretty sure we are going to see better native dataclass support in Pydantic going forward.
Already there is some support in Pydantic for native dataclasses: https://docs.pydantic.dev/latest/concepts/dataclasses/ https://docs.pydantic.dev/latest/concepts/dataclasses/
- kitotik 1y agoI have used native dataclasses with pydantic starting with v1.5 and it’s only improved with the the newer 2.x releases. Aside from basic inheritance and complex nested types, the pydantic ‘TypeAdapter’ is awesome for simply validating native dataclasses. It’s a little slow, but everything pydantic is =)
- phailhaus 1y agoThere is no facility in the type system to express the idea that "whatever class attributes are defined, those are kwargs to the init function". That's hacked in using extra typechecker plugins. That's why we're sitting around talking about if a particular typechecker "supports pydantic", rather than it just working.
- flanked-evergl 1y agoThere is a way to indicate whatever class attributes are defined, those are kwargs to the init function. For one, you can use `kw_only` on the dataclass decorator [1], alternatively you can use the `kw_only` arg on the field function [2]. [1]: https://docs.python.org/3/library/dataclasses.html#dataclasses.dataclass https://docs.python.org/3/library/dataclasses.html#dataclass... [2]: https://docs.python.org/3/library/dataclasses.html#dataclasses.field https://docs.python.org/3/library/dataclasses.html#dataclass...
- phailhaus 1y agoThat's only for dataclasses, you can't implement your own class like this without implementing it as a dataclass, because the typechecking for it is hacked in as a custom plugin. What I'm trying to point out is that these features exist in core Python and yet the type system they built can't express it. By contrast, TypeScript is designed in such a way that you can implement everything yourself without having to write "custom typescript checker plugins".
- bvrmn 1y agoTechnically there is dataclass_transform [1]. It's an ungodly hack, I don't know how it passed PEP review. And support by type checkers is questionable. After TS python hints feels like rigid and total unsound mess. [1]: https://docs.python.org/3/library/typing.html#typing.dataclass_transform https://docs.python.org/3/library/typing.html#typing.datacla...