python / python/mypy

False positive: "overloaded signature will never be matched" with Self

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

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

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

説明

Bug Report

When combining Self and base class overloads, mypy incorrectly warns about the former being "the same or broader" as the latter. However, those overloads are both actually used.

I do know that this can be solved with a single TypeVar - the case is extracted from a more complicated code where similar TypeVar solution doesn't seem to apply.

To Reproduce

playground

from __future__ import annotations

from typing import Self, overload

class Base:
    @overload
    def fn(self, other: Self) -> Self: ...
    @overload
    def fn(self, other: Base) -> Base: ...
    def fn(self, other: Base) -> Base:
        return other


class X(Base): ...
class Y(Base): ...

reveal_type(X().fn(X()))
reveal_type(X().fn(Y()))
reveal_type(Y().fn(X()))
reveal_type(Y().fn(Y()))

Expected Behavior

No errors reported

Actual Behavior

main.py:10: error: Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader  [misc]
main.py:18: note: Revealed type is "__main__.X"
main.py:19: note: Revealed type is "__main__.Base"
main.py:20: note: Revealed type is "__main__.Base"
main.py:21: note: Revealed type is "__main__.Y"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.11.1 and master (playground)
  • Mypy command-line flags: none and --strict - both reproduce
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11, 3.12 - does not affect

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

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

はじめの一歩

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

調査の方向性

リンクされている mypy-play の再現例と issue の overload の例から始め、Self と基底クラスが関係する重複した overload シグネチャを mypy がどのようにチェックするかを追跡します。完了条件は、例が誤検知エラーを報告せず、レポートに示されている4つの revealed type を維持することです。

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

評価

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

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

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