4 ms·
Looks like that works but it’s unintuitive. Pydantic can handle this situation without the weird KW_ONLY thing, so dataclasses should too
by goodoldneon 4y ago
Looks like that works but it’s unintuitive. Pydantic can handle this situation without the weird KW_ONLY thing, so dataclasses should too
- zzzeek 4y agohm it implicitly decides to be KW_ONLY? OK does not work: from pydantic import dataclasses @dataclasses.dataclass class Base: a: int b: int = 1 @dataclasses.dataclass class Child(Base): c: int error File "pydantic/dataclasses.py", line 265, in pydantic.dataclasses.dataclass ... File "/opt/python-3.10.0/lib/python3.10/dataclasses.py", line 539, in _init_fn raise TypeError(f'non-default argument {f.name!r} ' TypeError: non-default argument 'c' follows default argument it works with KW_ONLY though awkward that pydantic.dataclasses doesn't supply it directly, need to split up imports among pydantic.dataclasses and dataclasses.
- goodoldneon 4y agoExtend from pydantic.BaseModel instead of using the dataclasses decorator
- zzzeek 4y agonot SQLAlchemy compatible, interest lost :)
- goodoldneon 4y agoI didn’t mean “always use Pydantic instead of dataclasses”. I meant “dataclasses have a default property problem but it’s solvable since Pydantic handles them well”