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
When an overloaded function implementation returns a Union, mypy becomes confused when that Union contains two TypeVars.
The following code works as expected (i.e., there are no errors):
fromtypingimport (
Generic,
Literal,
Optional,
overload,
TypeVar,
Union,
)
T1=TypeVar('T1')
T2=TypeVar('T2')
# first example, works as expected:U1=Union[None, T1]
@overloaddefworks1(number: Literal[1]) ->T1: ...
@overloaddefworks1(number: Literal[2]) ->Optional[T1]: ...
defworks1(number: int) ->U1:
pass# second example, also works as expected:U2=Union[None, int, T2]
@overloaddefworks2(number: Literal[1]) ->int: ...
@overloaddefworks2(number: Literal[2]) ->Optional[int]: ...
defworks2(number: int) ->U2:
pass
Adding T2 to U1 in the first example, or changing int to T1 in the second example, produces this third example, which exhibits the problem in question:
fromtypingimport (
Generic,
Literal,
Optional,
overload,
TypeVar,
Union,
)
T1=TypeVar('T1')
T2=TypeVar('T2')
# third example, does not work as expected:U3=Union[None, T1, T2]
@overloaddeffunction(number: Literal[1]) ->T1: ...
@overloaddeffunction(number: Literal[2]) ->Optional[T1]: ...
deffunction(number: int) ->U3:
pass# error: Overloaded function implementation cannot produce return type of signature 1 [misc]# error: Overloaded function implementation cannot produce return type of signature 2 [misc]
To Reproduce
Run the "third example" code through mypy.
Expected Behavior
There should be no errors when processing the third example.
Actual Behavior
These errors are produced:
# error: Overloaded function implementation cannot produce return type of signature 1 [misc]
# error: Overloaded function implementation cannot produce return type of signature 2 [misc]
It may be helpful to know that the same problem still occurs if function is a method of a type which inherits from Generic[T2] (i.e., binding T2 doesn't help):
Uh oh!
There was an error while loading. Please reload this page.
Bug Report
When an overloaded function implementation returns a Union, mypy becomes confused when that Union contains two TypeVars.
The following code works as expected (i.e., there are no errors):
Adding
T2
toU1
in the first example, or changingint
toT1
in the second example, produces this third example, which exhibits the problem in question:To Reproduce
Run the "third example" code through mypy.
Expected Behavior
There should be no errors when processing the third example.
Actual Behavior
These errors are produced:
Further information
This may be related to #9023.
It may be helpful to know that the same problem still occurs if
function
is a method of a type which inherits fromGeneric[T2]
(i.e., binding T2 doesn't help):Your Environment
mypy.ini
(and other config files): (none necessary to reproduce the issue)The text was updated successfully, but these errors were encountered: