4 ms·
Dataclasses, like typing.NamedTuple, do not care about type hints: >>> @dataclasses.dataclass ... class D: ... x: int ... >>> D('a')
by throw_a_grenade 3y ago
Dataclasses, like typing.NamedTuple, do not care about type hints:
>>> @dataclasses.dataclass
... class D:
... x: int
...
>>> D('a')
D(x='a')
- jselysianeagle 3y agoThe idea with type hints in Python though is that they’re meant to be checked using some static analysis tool like mypy/pyright/etc. The runtime behavior for the most part remains unchanged in the sense that the Python interpreter won’t enforce the types in cases such as the one you’ve provided.
- FreakLegion 3y agoDataclasses do care about type hints in some cases, for example when determining what counts as a field. @dataclass class A: a: int = 0 b = 1 >>> A(a=1, b=2) TypeError: __init__() got an unexpected keyword argument 'b'
- eirki 3y agoThe only exceptions I can think of is https://docs.python.org/3/library/typing.html#typing.ClassVar https://docs.python.org/3/library/typing.html#typing.ClassVa...