Unsoundly accepts generic function with generic mutable default parameter

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

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

評価

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

調査の方向性

リンクされた mypy Playground の再現例から始め、関連する issue #3737、#11538、#11585 と比較します。ジェネリックなミュータブルのデフォルトパラメーターに対する期待される扱いを判断し、報告された互換性のないリストエイリアスが受け入れられないことを示すテストカバレッジを追加するとともに、非ジェネリックなデフォルト値に対する意図された動作を維持します。

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

説明

bug topic-type-variables

Mypy unsoundly accepts a generic function with a generic mutable default parameter value. This value is evaluated once at runtime, but can be referred to with multiple incompatible types, breaking type safety.

from typing import TypeVar

T = TypeVar("T")

def f(x: T, y: list[T] = []) -> list[T]:
    return y

y_strs: list[str] = f("a")
y_ints: list[int] = f(4)
# Uh oh, y_strs and y_ints refer to the same list with incompatible types
y_strs.append("b")
print(y_ints[0] + 6)  # oops

mypy Playground link

(This is a sort of converse to #3737, where the complaint is that mypy fails to accept a generic function with a non-generic default parameter value. It further supports my claim that default parameter values should not be generalized to the generic type of the parameter at function definition time.)

Related:

  • #11538
  • #11585
  • #3737
主要言語
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 を短くまとめたダイジェスト。