Type of list items not inferred when using `isinstance` inside `all`
Open
Nobody has claimed this yet.
feature
priority-1-normal
topic-type-narrowing
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
The type of items in a list is not inferred when using an isinstance check in an all call.
from typing import List
class A:
pass
class B(A):
pass
def test(arg: List[A]) -> None:
if all([isinstance(a, B) for a in arg]):
test2(arg)
def test2(arg: List[B]) -> None:
pass
- What is the actual behavior/output?
error: Argument 1 to "test_2" has incompatible type "List[A]"; expected "List[B]"
Found 1 error in 1 file (checked 1 source file)
- What is the behavior/output you expect?
I expect mypy to infer the type of the list items based on the if check.
- What are the versions of mypy and Python you are using?
mypy: v0.740
python: 3.6.7
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
No source file or test is named. First reproduce the example with mypy v0.740, then trace how the isinstance check inside all and the list comprehension are analyzed. Done means the example narrows List[A] to List[B] without the reported error and has regression coverage.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 34/100