Hi folks, I’d like clarification on how a type annotation consisting of the bare name of a generic class whose type parameters have default values should be interpreted. For example:
T = TypeVar("T", default=int)
class Foo(Generic[T]):
...
def func() -> Foo:
...
How is Foo
in the return type of func
interpreted? I can’t find an explicit answer to this question in the typing spec, although there are various places which suggest the answer should be Foo[int]
. That interpretation seems reasonable, although it’s a little surprising to me because of the following consequences:
Foo[str]
would not be a subtype ofFoo
(although it would be ofFoo[Any]
)- Adding a default value to a type variable could cause some previously-valid code to no longer type check, e.g. an implementation of
func
returning aFoo[str]
. (I wouldn’t have expected this since there’s no discussion in PEP 696 of potential backwards-compatibility issues like having to change annotations ofGenerator
toGenerator[Any, Any, Any]
. This post was prompted by typeshed recently adding default values toGenerator
, which if I understand correctly changed the meaning of a bareGenerator
annotation.)
I’d appreciate any thoughts on this, or pointers to where this case is specified.