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

オープン
#21,597 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
静か
技術スタック
python

調査の方向性

まず、示されている foo の runtime と stub の組み合わせを stubtest で再現し、次に overload とデフォルト引数の整合性チェックを追跡します。3 番目の overload のケースで誤検知が報告されなくなり、実際に互換性のない runtime のデフォルト値は引き続き検出されれば完了です。

索引モデルが issue の本文から書いたものです。

説明

bug topic-overloads topic-stubtest topic-type-variables

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
主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 18時間
マージ済み PR(30日)
54

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python/mypy のほかの issue

python/mypy の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。