Skip to content

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 #20399

@hyperkai

Description

@hyperkai

*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")

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongpendingIssues that may be closed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions