'Subclass of "A" and "B" cannot exist: would have incompatible method signatures' is usually wrong
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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.)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the callable and attribute examples in the linked mypy-play playgrounds, then trace the type narrowing and incompatible-signature checks that produce the diagnostic. Determine whether valid common subclasses should suppress the error and unreachable warning, and add regression coverage demonstrating the intended behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100