python / python/mypy

Erroneous arg-type error with Mapping and reversed Mapping

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

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

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

説明

I get an arg-type error in MyPy 1.14.1 on the following code using a Union of inverted collections.abc.Mapping:

from collections.abc import Mapping


def fun(x: Mapping[str, int | None] | Mapping[int | None, str]):
    pass


fun({"a": 1, "b": 2})

fun({"a": 1, "b": 2, "c": None})

fun({1: "a", 2: "b"})

fun({1: "a", 2: "b", None: "c"})

The error is:

mypy_type_error.py:12: error: Argument 1 to "fun" has incompatible type "dict[int, str]"; expected "Mapping[str, int | None] | Mapping[int | None, str]" [arg-type]

That is, MyPy takes issue with the parameters in the third function call. The last call including the None key does not produce an error, though. I'd expect the third call to not show an error, either.

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

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

はじめの一歩

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

調査の方向性

まず、報告されたスニペットを MyPy 1.14.1 で実行し、4 つの呼び出しを比較します。次に、引数型のチェックと union-of-Mapping の処理を追跡して、関連する回帰テストの領域を見つけます。完了条件は、他の例を後退させることなく、3 番目の呼び出しが 4 番目と一貫して受け入れられることです。

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

評価

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

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

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