Skip to content

Inconsistent MRO error when __new__ returns a subclass and --warn-unreachable is turned on #13824

Description

@tmke8

Bug Report

The problem appeared in the pandas stubs in this issue: pandas-dev/pandas-stubs#269

There, I was told to report it here.

To Reproduce
I reduced the problem to a standalone example, but it might be possible to reduce it further:

Playground URL: https://mypy-play.net/?mypy=master&python=3.10&flags=warn-unreachable&gist=18e35c8047f69e2418498f85cc8f02d0

from __future__ import annotations
import datetime
from typing import Any, Generic, TypeVar, Union, overload

S1 = TypeVar("S1", int, datetime.datetime)

class Series(Generic[S1]):
    @overload
    def __new__(cls, data: datetime.datetime) -> TimestampSeries: ...

    @overload
    def __new__(cls, data: dict[int, S1]) -> Series[S1]: ...

    def __new__(cls, data: Union[datetime.datetime, dict[int, S1]]) -> Any:
        return

class TimestampSeries(Series[datetime.datetime]): ...

x = Series({0: 0, 1: 1})
reveal_type(x)
assert isinstance(x, Series)

Note that the error only appears with --warn-unreachable.

Expected Behavior

Given that the reveal_type() shows that the type is Series[int], I would expect the subsequent isinstance() call to succeed.

Actual Behavior

main.py:20: note: Revealed type is "__main__.Series[builtins.int]"
main.py:21: error: Subclass of "Series[int]" and "TimestampSeries" cannot exist: would have inconsistent method resolution order  [unreachable]
Found 1 error in 1 file (checked 1 source file)

Your Environment

See the mypy playground link.

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