#3737·mypy

Unable to specify a default value for a generic parameter

Author: rowilliaCreated Jul 19, 2017Updated Sep 11, 2026
Labelsfeaturetopic-type-variablespriority-0-high

Simplified Example:

python
from typing import TypeVar

_T = TypeVar('_T')
def foo(a: _T = 42) -> _T:  # E: Incompatible types in assignment (expression has type "int", variable has type "_T")
    return a

Real World example is something closer to:

python
_T = TypeVar('_T')
def noop_parser(x: str) -> str:
    return x

def foo(value: str, parser: Callable[[str], _T] = noop_parser) -> _T:
    return parser(value)