News for hackers. Stuff that matters.
Re:The different uses of Python type hints by BeefySwain on Saturday April 15, 2023 @04:52PM (#35584410)
A few other examples for the sections given:

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.

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.
Re:The different uses of Python type hints by eirki on Sunday April 16, 2023 @05:20AM (#35588463)
FastApi very recently added support for Annotated, and now recommends using that over default arguments.

https://fastapi.tiangolo.com/release-notes/#0950

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.
Re:The different uses of Python type hints by eirki on Sunday April 16, 2023 @05:24AM (#35588474)
Re:The different uses of Python type hints by eesmith on Sunday April 16, 2023 @04:12AM (#35588176)
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.