--warn-unreachable and isinstance leads to false positive about method resolution order
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
A small V shaped class hierarchy, a variable annotated as an instance of the supertype, and a few assert isinstance to progressively narrow the type gets a false positive error message from mypy that no such subclass could exist. The code works fine at runtime, though.
To Reproduce
https://gist.github.com/mypy-play/a653520575a1e3279bfcec2f64c4a128
Requires --warn-unreachable:
https://mypy-play.net/?mypy=latest&python=3.11&flags=warn-unreachable&gist=a653520575a1e3279bfcec2f64c4a128
class A: pass
class B(A): pass
class C:pass
class D(B, C):pass
m: A = D()
assert isinstance(m, C) # remove this line, or move it to after the following one, and the error goes away.
assert isinstance(m, D) # error: Subclass of "A", "C", and "D" cannot exist: would have inconsistent method resolution order [unreachable]
Actual Behavior
$ mypy --warn-unreachable b.py
b.py:8: error: Subclass of "A", "C", and "D" cannot exist: would have inconsistent method resolution order [unreachable]
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.2.0 (compiled: yes)
- Mypy command-line flags: --warn-unreachable
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.11.0rc1
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
Start with the reproducer in the issue and run mypy with --warn-unreachable to confirm the diagnostic. Trace the isinstance narrowing and method-resolution-order analysis responsible for the unreachable result; done means this valid hierarchy no longer produces a false positive while genuine unreachable cases remain reported.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100