python / python/mypy

Mypy Gives `Incompatible Definitions` Error When Dynamically Typed

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

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

bug
主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 18時間
マージ済み PR(30日)
54

説明

Bug Report

Discussed in https://github.com/python/typing/discussions/1235

Originally posted by adam-grant-hendry August 4, 2022
Related to this SO question, I'm trying to understand why

but

I would have assumed since nothing is statically typed, that mypy would not produced any errors. In fact, the same code on the pyright playground shows no errors.

I though perhaps the error could be due to the potential for a keyword collision, which I gleaned from PEP 692. e.g. If I made the name of the 4th arguments the same, each could fail the same way if I tried to pass foo as a keyword argument:

>>> a = Foo()
>>> a.run(1, 2, 'baz', foo='bar')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: func() got multiple values for argument 'foo'

but if I keep the argument names different, then the above would work if I tried to call Bar.run(1, 2, 'baz', foo='bar'), but break for Foo.

However, I still would originally assume mypy would do nothing because nothing has been statically typed:

By default, mypy will not type check dynamically typed functions. This means that with a few exceptions, mypy will not report any errors with regular unannotated Python.

To Reproduce

Please see the mypy playground (and pyright playground) gists above.

Expected Behavior

mypy issues no errors.

Actual Behavior

mypy issues the error:

Definition of "run" in base class "Foo" is incompatible with definition in base class "Bar"

Your Environment

  • Mypy version used: mypy 0.971 (compiled: yes)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.8.10, x64-bit
  • Operating system and version: Windows 10, version 20H2

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

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

はじめの一歩

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

調査の方向性

リンクされている2つの mypy-play の再現例から始め、リンクされている Pyright playground と動作を比較してください。動的に型付けされたメソッド定義と追加された **kwargs に焦点を当てます。関連する typing の議論と、メソッド定義の互換性に関する既存の mypy テストを確認してください。動作が説明され、バグであることが確認された場合は回帰テストでカバーされていれば完了です。

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

評価

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

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

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