Skip to content

Generic and __new__ with returns to a subclass produces incorrect Union inference #14502

Description

@Dr-Irv

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__ import annotations

from typing import TypeVar, Generic, Union, overload

T = TypeVar("T", int, str)


class Series(Generic[T]):
    @overload
    def __new__(cls, dtype: int) -> IntSeries:
        ...

    @overload
    def __new__(cls, dtype: type[T]) -> Series[T]:
        ...

    @overload
    def __new__(cls, dtype=...) -> Series:
        ...

    def __new__(cls, dtype=...) -> Series:
        cls.dtype = dtype
        return object.__new__(cls)

    ...


class IntSeries(Series[int]):
    ...


class Index:
    @overload
    def __new__(cls, dtype: int) -> IntIndex:
        ...

    @overload
    def __new__(cls, dtype=...) -> Index:
        ...

    def __new__(cls, dtype=...) -> Index:
        cls.dtype = dtype
        return object.__new__(cls)


class IntIndex(Index):
    ...


def foo(x: Union[Series, int, Index]):
    if isinstance(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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions