python / python/mypy

Subclass method parameter names are not checked against superclass

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

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

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

説明

  • Are you reporting a bug, or opening a feature request?

Bug

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.

When a subclass renames the parameters of a method, this results in runtime errors; but this seems like something that should be statically detectable.

The below code provides a superclass A, and two similar subclasses B and C that differ in that C renames the parameter to the method f, but B does not.

mypy vs runtime behaviour for variables c and c2 seems inconsistent, while for c3 and c4 it seems ok.

I was expecting some mypy error to be raised when defining f in class C.

class A:
    def f(self, x: int) -> None:
      print("A: ", x)

class B(A):
    def f(self, x: int) -> None:
      print("B: ", x)

class C(A):
    def f(self, n: int) -> None:
      print("C: ", n)

a = A()
a.f(x=3)

b = B()  # type: A
b.f(x=3)

c = C()  # type: A
c.f(x=3)  # mypy passes, but gives runtime error: TypeError: f() got an unexpected keyword argument 'x'

c2 = C()  # type: A
c2.f(n=3) # mypy gives an error:  error: Unexpected keyword argument "n" for "f" of "A"   but runtime works.

c3 = C()  # type: C
c3.f(x=3)    # mypy give type error: error: Unexpected keyword argument "x" for "f" of "C", and runtime error: mypy-issue.py:25: error: Unexpected keyword argument "x" for "f" of "C"

c4 = C()  # type: C
c4.f(n=3)  # no errors in mypy or at runtime
  • What are the versions of mypy and Python you are using?

$ mypy --version
mypy 0.670
$ python --version
Python 3.7.2+

  • Do you see the same issue after installing mypy from Git master?

yes - mypy 0.710+dev.f10528a5a8d9e98939ceb84d40e614d1f159d9a0

  • What are the mypy flags you are using? (For example --strict-optional)

With no flags, and with --strict.

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

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

はじめの一歩

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

調査の方向性

issue にある最小限のサブクラス例から始め、--strict ありとなしで mypy を実行して、一貫性のないキーワード引数チェックを再現します。クラス C に使用されているオーバーライドとメソッドシグネチャの処理を追跡します。完了条件は、サブクラスの定義時に mypy が互換性のないパラメーター名の変更を報告し、互換性のあるオーバーライドに対する有効な呼び出しを維持することです。

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

評価

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

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

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