python / python/typing

`isinstance` check with nested `Protocol`.

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

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

topic: feature
主要言語
Python
スター
1.8k
フォーク
302
平均マージ
23時間
マージ済み PR(30日)
8

説明

isinstance(obj, ProtocolSubclass) only checks the the existence of ProtocolSubclass's methods on obj and not the type signature. To provide deeper checks, maybe isinstance could check attributes/methods on ProtocolSubclass that are themselves a Protocol.

A small example showing the change in behavior:

@runtime_checkable
class FooLike(Protocol):
    attr: str

@runtime_checkable
class BarLike(Protocol):
    attr: FooLike

@dataclass
class Foo:
    attr: str = "test"

@dataclass
class Bar:
    attr: Foo

foo = Foo()
bar = Bar(foo)

# No change in current behavior
assert isinstance(foo, FooLike)  # passes
assert isinstance(bar, BarLike)  # passes
assert isinstance(bar, FooLike)  # passes

# Change in behavior
assert not isinstance(foo, BarLike)  # NOT because `BarLike.attr` should be `FooLike` and `foo.attr` is not

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

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

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

調査の方向性

レポート内の isinstance(obj, ProtocolSubclass) の実行時動作と @runtime_checkable Protocol の例から始めてください。現在の浅い属性存在チェックと、要求されているネストされた Protocol の動作を比較し、その後、例の assertions を使って完了条件を定義してください。

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

評価

技術スタック
python
領域
tooling
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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