Open
Labels
bugmypy got something wrongmypy got something wrongtopic-overloadstopic-stubtesttopic-type-variables
Description
Bug Report
Given the runtime function:
def foo(x=0, ret=1):
return retstubtest is happy with these stubs:
from typing import TypeVar, overload
_T = TypeVar("_T")
@overload
def foo(x: int = 0) -> int: ...
@overload
def foo(x: int, ret: _T) -> _T: ...but complains about these stubs (with a third overload added):
from typing import TypeVar, overload
_T = TypeVar("_T")
@overload
def foo(x: int = 0) -> int: ...
@overload
def foo(x: int, ret: _T) -> _T: ...
@overload
def foo(x: int = 0, *, ret: _T) -> _T: ... # addedwith the error:
error: main.foo is inconsistent, runtime parameter "ret" has a default value of type Literal[1], which is incompatible with stub parameter type _T | _T. This is often caused by overloads failing to account for explicitly passing in the default value.
Stub: in file main.pyi:5
Overload(def (x: int =) -> int, def [_T] (x: int, ret: _T) -> _T, def [_T] (x: int =, *, ret: _T) -> _T)
Inferred signature: def (x: int = ..., ret: _T | _T = ...)
Runtime: in file main.py:1
def (x=0, ret=1)
Found 1 error (checked 1 module)
Your Environment
- Mypy version used: master,
mypy 2.2.0+dev.e15a6d58d - Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.13.13
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrongtopic-overloadstopic-stubtesttopic-type-variables
Activity
beanscg commentedon Jun 9, 2026
I checked the stubtest overload-merge path.
Signature.from_overloadedfuncdefcombines overload items into a single signature (mypy/stubtest.py:1010-1105). Default compatibility is then checked in_verify_arg_default_value(831-867), and overloaded-stub default errors are annotated at1422-1433.The issue's third overload adds a keyword-only
ret: _T, after which stubtest reports the runtime defaultLiteral[1]as incompatible with_T | _T. That looks like the merged signature is losing the fact that an earlier overload already accounts for the defaultedret=1path.A focused regression can use the issue's exact
foo(x=0, ret=1)runtime and two stub variants: the first two overloads accepted, then the third keyword-only overload added. Adding the third overload should not make the runtime default fail if the default is still covered by an overload.