python / python/mypy

(inter-)dependent variable types

Open
#7,422 2 comments 0 reactions 0 assignees View on GitHub

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 as Union[int, str]
  • What is the behavior/output you expect?
    type of a can only be int given 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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.