Re:The different uses of Python type hints
by FreakLegion on Saturday April 15, 2023 @05:27PM
(#35584701)
The 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]]
I have some use cases where the alternative approach is useful, like quantification of class fields or function arguments.FastApi very recently added support for Annotated, and now recommends using that over default arguments.
Re:The different uses of Python type hints
by amethyst on Saturday April 15, 2023 @07:09PM
(#35585560)
> 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"
Re:The different uses of Python type hints
by BeefySwain on Saturday April 15, 2023 @08:15PM
(#35585963)
Great catch, how could I forget!
Re:The different uses of Python type hints
by auscompgeek on Saturday April 15, 2023 @10:55PM
(#35586843)
Variable 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)])
Re:The different uses of Python type hints
by SethMLarson on Saturday April 15, 2023 @08:28PM
(#35586044)
Protocols with @runtime_checkable fit your description!
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:
>>> @dataclasses.dataclass
... class D:
... x: int
...
>>> D('a')
D(x='a')
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...
I managed to find type hints affecting runtime behavior in functools.register. 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.
Runtime behaviour determination: the stdlib [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.