python / python/mypy

stubtest false positive: default (list,) incompatible with tuple[type[list[_T]]]

Open
#19,852 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

Mypy stubtests report a false positive for a signature of tuple[type[list[_T]]] and a default value of (list,).
Removing either the wrapping tuple or the TypeVar is accepted.

To Reproduce

def foo1(x = (list,)): ...
def foo2(x = (list,)): ...
def foo3(x = list): ...

from typing import TypeVar

_T = TypeVar('_T')

def foo1(x: tuple[type[list[_T]]] = ...): ...
def foo2(x: tuple[type[list]] = ...): ...
def foo3(x: type[list[_T]] = ...): ...

Expected Behavior

Stubtests accept this

Actual Behavior

$ python -m mypy.stubtest lib
error: lib.foo1 is inconsistent, runtime parameter "x" has a default value of type tuple[Overload(def [_T] () -> builtins.list[_T`1], def [_T] (typing.Iterable[_T`1]) -> builtins.list[_T`1])], which is incompatible with stub parameter type tuple[type[builtins.list[_T`-1]]]
Stub: in file D:\Programming\Projects\Testing\lib.pyi:5
def [_T] (x: tuple[type[builtins.list[_T`-1]]] =) -> Any
Runtime: in file D:\Programming\Projects\Testing\lib.py:1
def (x=(<class 'list'>,))

Found 1 error (checked 1 module)


Your Environment

Command: python -m mypy.stubtest lib
Python Version: Python 3.11.3
Mypy Version: mypy 1.18.1 (compiled: yes)

From: https://github.com/python/typeshed/issues/14718#issuecomment-3290882846

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 report with Python 3.11.3 using python -m mypy.stubtest lib, comparing the signatures in lib.py and lib.pyi. Trace how stubtest evaluates the runtime default (list,) against tuple[type[list[_T]]]; done means the reported foo1 mismatch is accepted while the existing foo2 and foo3 cases remain covered.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.