python / python/mypy

Default value of keyword parameter in custom Protocol is not used

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

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

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

説明

Bug Report

Consider the code below:

from typing import Protocol


class P(Protocol):
    def foo(self, n: int = 666) -> None: ...


class F:
    def foo(self, n: int = 666) -> None: ...


class S:
    def foo(self, n: int = 999) -> None: ...


def bar(x: P) -> None: ...


bar(F())
bar(S())

Expected Behavior

Warning in the last code line about incompatibility of class S and protocol P, because the default value of parameter n in P.foo is not ellipsis and the numerical value of n in S.foo is different.

Actual Behavior

Mypy does not generate any warnings.

Your Environment

  • Mypy version used:

8.12
0.820+dev.8642b351cff94c471333256f9cd6867807384f01

  • Mypy command-line flags:

none

  • Mypy configuration options from mypy.ini (and other config files):
[mypy]

pretty = True
show_error_codes = True
strict = True
disallow_any_unimported = True
disallow_any_expr = True
disallow_any_decorated = True
disallow_any_explicit = True
no_warn_no_return = True
warn_unreachable = True
  • Python version used:

3.8.10 (64-bit), 3.9.5 (64-bit), 3.10 beta (64-bit)

  • Operating system and version:

Windows 10 64-bit

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

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

はじめの一歩

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

調査の方向性

まず、issue の reproducer に対して mypy を実行し、foo の n パラメーターのデフォルト値に対するプロトコル互換性チェックを追跡してください。完了の条件は、mypy が S と P の非互換性を報告しつつ、F を互換性があるものとして引き続き受け入れることです。

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

評価

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

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

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