You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Class factory with type[TypeVar] with upper bound not checked correctly (false positive) #14790
Bug Report
Class factory, that uses TypeVar with upper bound as a type, suffers from over-inference, thinking that type[SomeTypeVar] gives its upper bound on instantiation.
To Reproduce
fromtypingimportTypeVar, Type, UnionclassA:
def__init__(self, x: str) ->None: passclassB:
def__init__(self, x: int) ->None: passclassC:
def__init__(self, x: int) ->None: passT=TypeVar('T', bound=Union[A, B])
deff(x: Type[T]) ->T:
returnx(1) \
# E: Incompatible return value type (got "Union[A, B]", expected "T") \# E: Argument 1 to "A" has incompatible type "int"; expected "str"T2=TypeVar('T2', bound=Union[B, C])
deff2(x: Type[T2]) ->T2:
returnx(1) # E: Incompatible return value type (got "Union[B, C]", expected "T2")
I expect only 2nd error (invalid constructor arg) to be reported, without replacing a TypeVar with Union bound.
Actual Behavior
main.py:13: error: Incompatible return value type (got "Union[A, B]", expected "T") [return-value]
main.py:13: error: Argument 1 to "A" has incompatible type "int"; expected "str" [arg-type]
main.py:20: error: Incompatible return value type (got "Union[B, C]", expected "T2") [return-value]
Found 3 errors in 1 file (checked 1 source file)
Your Environment
Mypy version used: 1.0.1 and master, repro's on older versions too
Mypy command-line flags: none
Mypy configuration options from mypy.ini (and other config files): none
Activity
type(t)()#18478 as a duplicate of this issue