(inter-)dependent variable types
Open
Nobody has claimed this yet.
feature
needs discussion
priority-2-low
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
I'm not sure how to entitle this, but I think it's a dependent type.
- Are you reporting a bug, or opening a feature request? yes
- Please insert below the code you are checking with mypy,
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
from typing import Tuple, Union
def x() -> Union[Tuple[int, int], Tuple[str, str]]:
...
a, b = x()
if isinstance(b, int):
reveal_type(a) # can only be int but is reported as Union[int, str]
- What is the actual behavior/output?
type is reported asUnion[int, str] - What is the behavior/output you expect?
type ofacan only beintgiven the return type signature - What are the versions of mypy and Python you are using? 7.20
Do you see the same issue after installing mypy from Git master? yes - What are the mypy flags you are using? (For example --strict-optional)
python_version=3.7
I'm assuming this would be a difficult change, but I figured I'd open an issue for the sake of having a record, and in the meantime I can use assert isinstance()
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 with the minimal Python reproduction in the issue and verify the current reveal_type output under the stated Python 3.7 configuration. The change would be complete when narrowing on b as int also narrows a to int, while preserving the corresponding str case and existing behavior for unrelated unions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 30/100