Idea: Simpler and More Expressive Type Annotations

I am going to reiterate something I said before: I think general macros ala PEP 638 are a better idea long term.

E.g. we could have a type!(<ast>) macro provided somewhere that can then be used anywhere:

class Child(Base[type!(1 | 2)]):
    pass

OldStyleAlias = type!(3 | 4)

def foo[T](a: T, b: T) -> type!((T, T)):
    ...

This doesn’t always look great - but it requires far less special casing of annotations and makes evolution of this aspect of the language somewhat independent of CPython releases. Additionally, it removes all additional runtime performance cost - the only cost would be on first import, afterwards it’s cached with the exact same strategies as current annotations.

Once a version of type! has been tested for a bit, the language can be extended to implicitly apply type! to annotations, giving us the same end state as more direct proposals but in a more controlled speed.

4 Likes