KotlinIsland / basedtypeshed

Collection of library stubs for Python, with static types
Other
3 stars 0 forks source link

type the special forms (typing, type machinery) #14

Open DetachHead opened 2 years ago

DetachHead commented 2 years ago
a = Union[int, str]

What is this value? It can be extremely confusing that it is an instance of _SpecialForm.

Union is an instance of _SpecialForm. _SpecialForm.__getitem__ returns a _SpecialForm, but each instance is specialized in this method.

Unions __getitem__ returns an instance of _UnionGenericAlias which is a proxy object to Union that stores the type parameters.

I would both be useful and help with understanding if these objects were typed in a nice way.

functional benefit:

def f(u: TypeForm[Union[T]) -> tuple[T]: ...

a = f(Union[str, int]) # a is tuple[T]

understanding benefit:

reveal_type(Union)  # TypeForm[Union[str, int]]
reveal_type(Union[str, int])  # TypeForm[Union[str, int]]

There needs to be discussion around the exact impl, as I am unsure.

Also these are tightly special cased and would probably need fixing in mypy.

KotlinIsland commented 2 years ago

KotlinIsland/basedmypy/issues/24