4 ms·
A few other examples for the sections given: Runtime behaviour determination: the stdlib [dataclasses](https://docs.python.org/3/library/dataclasses.html#modul
by BeefySwain 3y ago
A few other examples for the sections given:
Runtime behaviour determination: the stdlib [dataclasses](https://docs.python.org/3/library/dataclasses.html#module-dataclasses https://docs.python.org/3/library/dataclasses.html#module-da...)
Dataclasses is notable because it's the only example (I'm aware of) of type hints effecting runtime behavior as part of the stdlib.
Compiler instructions: mypyc was (one of?) the first to do this, but Cython actually supports this natively now, and is much more active than mypyc is last I checked.
- FreakLegion 3y agoThe Annotated type is worth mentioning as well. Today in something like SQLModel you do (from the readme): class Hero(SQLModel, table=True): id: Optional[int] = Field(default=None, primary_key=True) And that's fine. I wouldn't necessarily change anything here. Annotated gives you the option of approaching things in a different way, though. class Hero(SQLModel, table=True): id: PrimaryKey[Optional[int]] = None I have some use cases where the alternative approach is useful, like quantification of class fields or function arguments.
- eirki 3y agoFastApi very recently added support for Annotated, and now recommends using that over default arguments. https://fastapi.tiangolo.com/release-notes/#0950 https://fastapi.tiangolo.com/release-notes/#0950
- amethyst 3y ago> Dataclasses is notable because it's the only example (I'm aware of) of type hints effecting runtime behavior as part of the stdlib. FWIW, `typing.NamedTuple` did this in Python 3.5, three years before dataclasses was introduced in 3.7. class Foo(typing.NamedTuple): a: int b: str f = Foo(a=1, b="hello") print(f.b) # "hello"
- BeefySwain 3y agoGreat catch, how could I forget!
- auscompgeek 3y agoVariable annotations were only added in Python 3.6. Defining a typed namedtuple in Python 3.5 looked like this: Foo = typing.NamedTuple('Foo', [('a', int), ('b', str)])
- SethMLarson 3y agoProtocols with @runtime_checkable fit your description!
- throw_a_grenade 3y agoDataclasses, 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...
- eesmith 3y agoI managed to find type hints affecting runtime behavior in functools.register. https://docs.python.org/3/library/functools.html?highlight=functools#functools.singledispatch https://docs.python.org/3/library/functools.html?highlight=f... > To add overloaded implementations to the function, use the register() attribute of the generic function, which can be used as a decorator. For functions annotated with types, the decorator will infer the type of the first argument automatically: That appears to be the only other case.