python / python/mypy

(🐞) unreachable `isinstance` only works with variables

Open
#16,769 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

# mypy: warn-unreachable
def foo() -> int: return 1
class A:
     i: int
     foo() -> int: return 1
assert isinstance(1, str)
assert isinstance(1 + 1, str)
assert isinstance(foo(), str)
assert isinstance(A.foo(), str)
assert isinstance(A().foo(), str)
assert isinstance(A().i, str)
a: A
assert isinstance(a.foo(), str)

i = 1    
assert isinstance(i, str)  # error: Subclass of "int" and "str" cannot exist: would have incompatible method signatures  [unreachable]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided mypy snippet with warn-unreachable enabled and compare the isinstance expressions that do and do not produce diagnostics. Trace the type-checking path for expressions versus variables; done should mean the intended equivalent expressions receive consistent unreachable checks.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.