Re:The different uses of Python type hints
by throw_a_grenade on Saturday April 15, 2023 @08:58PM
(#35586236)
Dataclasses, like typing.NamedTuple, do not care about type hints:
[ Parent
| on HN ]
Re:The different uses of Python type hints
by FreakLegion on Saturday April 15, 2023 @09:29PM
(#35586415)
Dataclasses 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'
Re:The different uses of Python type hints
by jselysianeagle on Saturday April 15, 2023 @09:24PM
(#35586394)
The 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.
The only exceptions I can think of is https://docs.python.org/3/library/typing.html#typing.ClassVa...