python / python/mypy

About `TypeVar`, passing a `name` keyword argument to `name` keyword and positional parameter gets error

Open
#20,468 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-type-variables
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

*Memo:

  • mypy test.py
  • python test.py
  • pyright test.py # Strict mode
  • mypy 1.19.1
  • Python 3.14.0
  • Windows 11

The doc of TypeVar indicates name parameter is both a keyword and positional parameter as shown below:

class typing.TypeVar(name, *constraints, bound=None, covariant=False, contravariant=False, infer_variance=False, default=typing.NoDefault)

But with mypy, passing a name keyword argument to name keyword and positional parameter gets the error as shown below:

*Memo:

  • No error occurs at runtime.
  • pyright gives no error even with strict mode.
from typing import TypeVar

          # ↓↓↓↓↓
T = TypeVar(name='T')
# Error

error: TypeVar() expects a string literal as first argument

So, mypy should accept a name keyword argument.

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 by running the reported mypy test.py example with mypy 1.19.1, then compare its result with Python runtime behavior and pyright in strict mode. Trace the TypeVar call handling and add a regression test for TypeVar(name='T'); done means mypy accepts the keyword form without regressing positional handling.

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
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.