python / python/mypy

overload with wrong return type is ignored

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

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

feature topic-overloads
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Bug Report

When one of the alternatives of an overload has a return type T which is a subtype of the return type of the annotation U but that the body of the function returns a type V which is a subtype of U but not T, no error is reported

To Reproduce

from typing import overload

@overload
def f(a: int) -> str:
    ...

# this is completely wrong
@overload
def f(a: str) -> bool:
    ...


def f(a: str | int) -> str | bool:
    return "Hello"

reveal_type(f("some string"))

https://mypy-play.net/?mypy=latest&python=3.12&flags=strict&gist=83c5a353e087d8bbe03d542e7340b4b5

Expected Behavior

mypy should complain that the body of the function returns an str which is incompatible with type bool (overload 2)

More generally, for each overload signature, mypy should check that the body of the function typechecks with the overload signature.

Actual Behavior

mypy reports no error

main.py:16: note: Revealed type is "builtins.bool"
Success: no issues found in 1 source file

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

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

はじめの一歩

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

調査の方向性

main.py に示されている Python の再現コードから始め、リンクされた mypy-play の strict 設定で実行します。実装の戻り値の型に対して overload のシグネチャがどのように検証されるかを追跡します。mypy が overload 2 に対する互換性のない str の戻り値を報告し、その回帰がテストでカバーされれば完了です。

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

評価

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

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

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