Type narrowing on all() call
Open
Nobody has claimed this yet.
feature
topic-type-narrowing
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
Mypy ignores types in a explicitly checked tuple.
To Reproduce
- Create a file containing:
def func(a: str, b: int) -> None:
pass
t: tuple[str | None, int | None] = ('a', None)
if not all(t):
raise ValueError
func(*t)
- Run mypy.
Expected Behavior
No errors.
Actual Behavior
mypy_tuple.py:8: error: Argument 1 to "func" has incompatible type "*Tuple[Optional[str], Optional[int]]"; expected "str"
mypy_tuple.py:8: error: Argument 1 to "func" has incompatible type "*Tuple[Optional[str], Optional[int]]"; expected "int"
Found 2 errors in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 0.961
- Mypy command-line flags:
- Mypy configuration options from
mypy.ini(and other config files): - Python version used: 3.10
- Operating system and version: Arch Linux
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 putting the reported example in a small file such as mypy_tuple.py and run mypy against it to confirm the two incompatible-type errors. Trace how mypy handles narrowing after all(t) for an explicitly typed tuple; done means the example passes without errors while existing narrowing behavior remains covered by tests.
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
- 35/100