Bug Report
There seems to be an odd interaction between Union, __new__() and Generic causing incorrect inferences to occur.
Came up in a reported issue in pandas-stubs: pandas-dev/pandas-stubs#516
Might be related to both #14009 and #14061
from __future__ importannotationsfromtypingimportTypeVar, Generic, Union, overloadT=TypeVar("T", int, str)
classSeries(Generic[T]):
@overloaddef__new__(cls, dtype: int) ->IntSeries:
...
@overloaddef__new__(cls, dtype: type[T]) ->Series[T]:
...
@overloaddef__new__(cls, dtype=...) ->Series:
...
def__new__(cls, dtype=...) ->Series:
cls.dtype=dtypereturnobject.__new__(cls)
...
classIntSeries(Series[int]):
...
classIndex:
@overloaddef__new__(cls, dtype: int) ->IntIndex:
...
@overloaddef__new__(cls, dtype=...) ->Index:
...
def__new__(cls, dtype=...) ->Index:
cls.dtype=dtypereturnobject.__new__(cls)
classIntIndex(Index):
...
deffoo(x: Union[Series, int, Index]):
ifisinstance(x, (Series, Index)):
reveal_type(x)
else:
reveal_type(x)Expected Behavior
genunion.py:52: note: Revealed type is "Union[genunion.Series[Any], genunion.Index]"
genunion.py:54: note: Revealed type is "int"
pyright handles this correctly
Actual Behavior
genunion.py:52: note: Revealed type is "Union[genunion.IntSeries, genunion.IntIndex]"
genunion.py:54: note: Revealed type is "Union[genunion.Series[Any], builtins.int, genunion.Index]"
Your Environment
- Mypy version used: 0.991
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini (and other config files): none - Python version used: 3.9
Bug Report
There seems to be an odd interaction between
Union,__new__()andGenericcausing incorrect inferences to occur.Came up in a reported issue in pandas-stubs: pandas-dev/pandas-stubs#516
Might be related to both #14009 and #14061
Expected Behavior
pyright handles this correctly
Actual Behavior
Your Environment
mypy.ini(and other config files): none