Incomplete inference of extracted values from union of tuples
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
I expect this is a feature request, though it's possible it indicates a bug.
from typing import Union, Tuple
def func() -> Union[Tuple[None, None], Tuple[int, int]]:
...
a, b = func()
if a:
print(a + b)
if a is not None:
print(a + b)
# Unsupported operand types for + ("int" and "None")
# Right operand is of type "Optional[int]"
Both if tests result in the same behaviour.
Ideally I'd like mypy to know that the test here for a is sufficient to also know that b cannot be None (due to the way that Union works).
Not sure if this is asking too much though.
Python 3.6, mypy 0.770, also master (at 2a3de7b7941f24b6bc53fe0cf8a2b7f8f7fd4e15)
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
Reproduce the Python example with mypy and compare the diagnostics for the two checks on a value inferred from the union of tuples. Investigate the type-narrowing entry points involved in tuple-union inference. Done means both checks establish that b is an int and no longer report an unsupported operand type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100