isinstance wrongly narrows away Any
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
Mypy falsely narrows a type when using isinstance with a tuple containing a variable of type Any.
This only happens when there is at least one other element in the tuple.
Note that a class might have type Any if it is imported from an untyped module.
To Reproduce
from typing import Any
class A:
pass
B: Any # (Or from untyped_lib import B)
m: Any
if isinstance(m, (A, B)):
reveal_type(m)
else:
reveal_type(m)
Expected Behavior
Mypy reports no error. m is of type Any in both branches
Actual Behavior
The second branch is reported as unreachable.
Your Environment
- Mypy version used: 0.931
- Mypy command-line flags: --warn-unreachable
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.10
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 by running the supplied reproduction with mypy 0.931, Python 3.10, and --warn-unreachable, then trace the isinstance narrowing logic involved when a tuple contains Any. Done means the example reports m as Any in both branches and no longer marks the else branch unreachable.
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
- 45/100