Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link

Description

@Dr-Irv
Issue body actions

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
Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.