python / python/mypy

[stubtest] False positive involving overloaded function with default argument for parameter with type variable

Open
#21,597 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-overloads topic-stubtest topic-type-variables
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Given the runtime function:

def foo(x=0, ret=1):
    return ret

stubtest 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: ...  # added

with 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

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 by reproducing the shown foo runtime and stub combinations with stubtest, then trace the overload and default-argument consistency check. Done means the third-overload case no longer reports a false positive while genuine incompatible runtime defaults remain detected.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.