python / python/mypy

Can't convert from Tuple of optional to Tuple of non-optional

Open
#8,622 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

false-positive feature priority-2-low
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Code:

from typing import Tuple, Optional

a: Tuple[Optional[int], Optional[int]] = (1, 1)
b: Tuple[int, int]
if a[0] is not None and a[1] is not None:
    b = a

After that has error:

script.py:6: error: Incompatible types in assignment (expression has type "Tuple[Optional[int], Optional[int]]", variable has type "Tuple[int, int]")

Expected no error.
I have: mypy-0.770 and Python 3.7.7

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

Reproduce the reported error from script.py using mypy with the shown Tuple and Optional annotations. Investigate how mypy narrows tuple elements after the two None checks, and consider the work complete when the assignment to b is accepted without an incompatible-types error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
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.