Skip to content

'Subclass of "A" and "B" cannot exist: would have incompatible method signatures' is usually wrong #19377

Open
@JelleZijlstra

Description

@JelleZijlstra

Mypy reports Subclass of "A" and "B" cannot exist: would have incompatible method signatures in many cases if given x: A, you run isinstance(x, B). However, this is often (always?) incorrect, as mypy does not account for subclasses that use method signatures that are subtypes of both classes.

Example (playground):

from typing import Never

class A:
    def f(self) -> int: return 0
class B:
    def f(self) -> str: return ""

class X(A, B):
    def f(self) -> Never: raise Exception

x: A = X()
if isinstance(x, B):  # E: Subclass of "A" and "B" cannot exist: would have incompatible method signatures
    reveal_type(x)    # E: Statement is unreachable
else:
    reveal_type(x)    # N: Revealed type is "__main__.A"

Here, mypy accepts the definition of a class X that is a subclass of A and B, yet also says that such a subclass cannot exist.

Any callable type is a subclass of (*args: object, **kwargs: object) -> Never, so incompatible method signatures cannot be a reason that a common subclass cannot exist.

It gets a little trickier with attributes. However, mypy accepts this class and then again claims it cannot exist (playground):

from typing import Never

class A:
    x: int
class B:
    x: str

class X(A, B):
    @property
    def x(self) -> Never: raise Exception
    @x.setter
    def x(self, x: int | str) -> None: pass

x: A = X()
if isinstance(x, B):  # E: Subclass of "A" and "B" cannot exist: would have incompatible method signatures
    reveal_type(x)    # E: Statement is unreachable
else:
    reveal_type(x)    # N: Revealed type is "__main__.A"

(Note though that pyright rejects this form of inheritance; possibly mypy should reject this too in the future if we revise how we treat inheritance of attributes.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-inheritanceInheritance and incompatible overridestopic-reachabilityDetecting unreachable code

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions