python / python/mypy

With a new syntax generic function, `T` cannot be used to set a default value to a parameter with `cast()` while `T` can be used for a parameter and return type

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

Nobody has claimed this yet.

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

Description

*Memo:
mypy --strict test.py
mypy 1.19.0
Python 3.14.0
Windows 11

With a new syntax generic function, T cannot be used to set a default value to a parameter with cast() while T can be used for a parameter and return type as shown below:

from typing import cast

                      # ↓ Error
def func[T](x: T = cast(T, 100)) -> T:
    return x # ↑ No error         # ↑ No error

error: Name "T" is not defined

If I don't use T with cast(), other error occurs as shown below:

                 # ↓↓↓ Error
def func[T](x: T = 100) -> T:
    return x

error: Incompatible default for argument "x" (default has type "int", argument has type "T")

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 test.py reproducer and run it with mypy --strict using the reported mypy 1.19.0 and Python 3.14.0 versions. Trace how the new generic-function syntax handles T in the default expression passed to cast(). Done means the example accepts the cast-based default while preserving the existing parameter and return type behavior.

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.