python / python/mypy

Type narrowing on all() call

Open
#13,069 1 comment 4 reactions 0 assignees View on GitHub

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

  1. 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)
  1. 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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.