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
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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