python / python/mypy

constrained TypeVar and star operator incompatible type

Open
#17,698 0 comments 0 reactions 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
We can't use TypeVar with star operator, maybe I wrongly used TypeVar.
Thanks

To Reproduce

T = TypeVar("T", str, int)


def test(a: Sequence[T]) -> list[T]:
    return [*a]

Expected Behavior
No error

Actual Behavior

cactus.py: note: In function "test":
cactus.py:57:14: error: List item 0 has incompatible type "Sequence[T]"; expected "str"  [list-item]
        return [*a]
                 ^
cactus.py:57:14: error: List item 0 has incompatible type "Sequence[T]"; expected "int"  [list-item]
        return [*a]
                 ^
cactus.py:57:14: note: See https://mypy.rtfd.io/en/stable/_refs.html#code-list-item for more info

Your Environment

  • Mypy version used: 1.11.1
  • Python version used: 3.12.1

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 Python reproducer in the issue using mypy 1.11.1 and Python 3.12.1, confirming the incompatible list-item errors for the constrained TypeVar and star expression. Trace the type-checking path for this expression and make the example pass without errors, then rerun the reproducer to verify the expected behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.