With PEP 649 impending, I wanted to float this idea.
Adding types to a Python code-base requires importing many names from the typing
module. After a while it’s natural to start pondering whether some of the more common ones couldn’t be made builtins. However, adding builtins is not something that can be done lightly, and I would expect to see exactly zero progress should that idea be seriously proposed.
Still, it seems a bit silly that the following program requires an import.
from typing import Final
a: Final = 1
So, what if we changed the lookup semantics for all lazy type hints, such that when a NameError
would have usually been raised, the lookup function first checks if the name exists in the typing
module, and uses that?
Combined with the generic type alias syntax introduced in PEP 695, I imagine this would allow the following program to be valid, without adding any imports.
type MyType = Literal[1, 2]
a: Final[MyType] = 1
Is this something that could be a viable idea?