GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Generic and __new__ with returns to a subclass produces incorrect Union inference · Issue #14502 · python/mypy

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

New issue
Copy link
New issue
Copy link
Closed
#21441
Closed
Generic and __new__ with returns to a subclass produces incorrect Union inference#14502
#21441
Copy link
Labels
bugmypy got something wrong

Description

@Dr-Irv
Dr-Irv
opened on Jan 22, 2023
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

Activity

  1. Dr-Irv
    added
    bugmypy got something wrong
    on Jan 22, 2023
  2. Dr-Irv commented on Jan 3, 2025

    @Dr-Irv
    Dr-Irv
    on Jan 3, 2025
    Author
    More actions

    Still an issue with mypy 1.14.1

  3. ilevkivskyi
    closed this as completedin #21441on May 27, 2026
  4. pull
    added a commit that references this issue on May 27, 2026

    Respect explicit return type of __new__() (python#21441)

    938dbe2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.