python / python/mypy

Tuple of iterables are not narrowed after converting it to tuple of tuple

Open
#17,194 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

If there is a function that takes *args: Iterable[float] as an argument, mypy still thinks it is a tuple of iterates after converting it to tuple of tuple via either map or tuple comprehension.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.11&gist=f70e818d8b82887c5d7f0b19cf51b504

from typing import Iterable


def foo(*args: Iterable[float]):
    x = tuple(map(tuple, args))
    reveal_type(x)  # Revealed type is "builtins.tuple[builtins.tuple[builtins.float, ...], ...]"

    args = tuple(map(tuple, args))
    reveal_type(args)  # Revealed type is "builtins.tuple[typing.Iterable[builtins.float], ...]"

Expected Behavior

args should have the same type as x.

Actual Behavior

args's type is not narrowed down after conversion.

Your Environment

  • Mypy version used: 1.10
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.11

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

No repository file or test path is named. Start by reproducing the example in the linked mypy-play case and locate the type-narrowing logic and related tests for tuple assignments; done means reassignment through map or tuple comprehension reveals the same narrowed tuple type as x.

Written by the indexing model from the issue text.

Assessment

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