python / python/mypy

`typing.Protocol` is explicitly treated as not a `type`

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

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

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

説明

Bug Report

At runtime, isinstance(typing.Protocol, type) is True. When type checking, mypy appears to special-case Protocol:

  • mypy: <typing special form>
  • Pylance/Pyright: (class) Protocol

To Reproduce

from typing import Protocol

class A(Protocol): pass
    
assert Protocol in A.__bases__
bases: tuple[type, ...] = (Protocol, *A.__bases__)

playground (gist)

Expected Behavior

Same as Pyright, Protocol is a class (type), no errors.

Actual Behavior

main.py:5: error: Non-overlapping container check (element type: "<typing special form>", container item type: "type")  [comparison-overlap]
main.py:6: error: Argument 1 to <tuple> has incompatible type "<typing special form>"; expected "type"  [arg-type]

Your Environment

  • Mypy version used: 1.0.0, 1.10.1, 1.20.2, 2.3.1
  • Mypy command-line flags: --strict
  • Python version used: 3.6 - 3.14

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

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

はじめの一歩

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

調査の方向性

提供された Python 再現コードを mypy --strict で実行することから始め、診断結果を記載されている Pyright の動作と比較します。mypy における typing.Protocol の特別な扱いを特定し、Protocol が型として受け入れられ、再現コードでエラーが発生しないように、テストを追加するかカバレッジを更新します。

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

評価

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

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

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