python / python/mypy

Parameterized protocol structure ignored as argument

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

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

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

説明

Bug Report

Parameterized protocols seem to be largely or totally ignored when specified as argument types. Simple example:

from typing import Protocol, TypeVar

T = TypeVar("T")
T_co = TypeVar("T_co", covariant=True)


class Proto(Protocol[T_co]):
    def __call__(self, *, a: str) -> T_co:
        ...


class InvalidClass:
    def __init__(self, *, a: int) -> None:
        ...


def test(b: Proto[T]) -> T:
    return b(a="0")
    

test(InvalidClass)  # <-- should fail type checking

InvalidClass is accepted here even though it does not conform to the protocol. Here's a more detailed playground gist.

As demonstrated in the gist, the issue does not reproduce when a function is provided, only with a class. It also does not manifest with assignment, only with arguments. However, assigning with the parameter set to Any also produces an unexpected result, which might somehow be related.

  • Mypy version used: 1.0
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.11

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

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

はじめの一歩

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

調査の方向性

まず、Python 3.11 上で mypy 1.0 を使って最小限の例を再現し、次にリンクされた playground の gist とその strict flags と比較します。Proto に準拠していないため mypy が test(InvalidClass) を拒否し、同時に関数と代入について報告された動作が維持されれば、修正は完了です。

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

評価

技術スタック
python
領域
tooling
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
45/100

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

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