When mypy is run on the following code:
from typing import Any, Generic, TypeVar
T = TypeVar('T', bound=int)
class C(Generic[T]):
x: T
c: C[Any] = C()
c.x = 123
c.x = 'abc'
It will not report any errors. However, since T has int as a bound, assigning a string to x should be considered invalid even if we don't know the exact type of x.
It seems that when Any is substituted for the type argument of C, the knowledge of the bound is lost. Perhaps AnyType could be given a bound, object by default, to restrict which types Any can represent in a particular context.
When mypy is run on the following code:
It will not report any errors. However, since
Thasintas a bound, assigning a string toxshould be considered invalid even if we don't know the exact type ofx.It seems that when
Anyis substituted for the type argument ofC, the knowledge of the bound is lost. PerhapsAnyTypecould be given a bound,objectby default, to restrict which typesAnycan represent in a particular context.