python / python/mypy

mypy incorrectly reports incompatible return types for overloaded methods

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

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

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

説明

Bug Report

@overload
def foo(cls: type[dict], mapping: SupportsKeysAndGetItem[K, V], /) -> dict[K, V]: ...
@overload
def foo(cls: type[dict], mapping: SupportsKeysAndGetItem[str, V], /, **kwargs: V) -> dict[str, V]: ...

Return types are compatible.

class FrozendictBase(Generic[K, V]):
    @overload
    def __new__(
            cls,
            mapping: SupportsKeysAndGetItem[K, V],
            /,
            ) -> 'FrozendictBase[K, V]': ...

    @overload
    def __new__(
            cls,
            mapping: SupportsKeysAndGetItem[str, V],
            /,
            **kwargs: V,
            ) -> 'FrozendictBase[str, V]': ...

Return types are also compatible, but mypy reports an error.

To Reproduce

  1. Download file.txt and save as sandbox.py.
  2. Run mypy sandbox.py.

Expected Behavior

sandbox.py:8: error: Invariant type variable "V" used in protocol where covariant one is expected

This is due to #5775.

Actual Behavior

sandbox.py:8: error: Invariant type variable "V" used in protocol where covariant one is expected
sandbox.py:39: error: Overloaded function signatures 3 and 4 overlap with incompatible return types
sandbox.py:54: error: Overloaded function signatures 5 and 6 overlap with incompatible return types

Your Environment

  • Mypy version used: mypy 0.950
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: Python 3.10.4
  • Operating system and version: Windows 10

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

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

はじめの一歩

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

調査の方向性

リンク先のファイルをダウンロードして sandbox.py として保存し、その後 mypy sandbox.py を実行してオーバーロードの診断を再現します。オーバーロードされたシグネチャ 3/4 および 5/6 に対する互換性のない戻り値型の報告を調査します。期待される不変な型変数のエラーが残ったまま、それらの報告が表示されなくなれば完了です。

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

評価

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

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

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