python / python/mypy

mypy does not recognize tuple unpacking syntax for TypeVars.

Open
#11,655 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature priority-2-low topic-runtime-semantics topic-type-variables
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

This is really a minor issue, but if you declare multiple TypeVar variables in one line, mypy does not seem to recognize them as type variables. For example,

from typing import TypeVar, Generic
T1, T2 = TypeVar('T1'), TypeVar('T2')

class Container(Generic[T1]):
    class Item(Generic[T2]):
        pass

To Reproduce

  1. Declare more than one TypeVars in one line. (See the example code above.)
  2. Run mypy check on this script.

Expected Behavior

The T1 and T2 in the example above should be recognized as type variables.
They should pass mypy check.

Actual Behavior

Errors:

$ mypy typevar.py 
typevar.py:5: error: Free type variable expected in Generic[...]
typevar.py:6: error: Free type variable expected in Generic[...]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10.0
  • Operating system and version: Ubuntu 20.04

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

Reproduce the issue with the provided Python snippet and the mypy typevar.py command, then trace how tuple-assigned TypeVars are recognized before checking Generic[...]. Done means both T1 and T2 are accepted as type variables and the example passes mypy without errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.