Add type checking for hook specifications
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
まず pluggy.HookspecMarker と PluginManager を調べ、特にフックの登録と pm.hook 属性を確認します。Issue の MySpec の例と mypy のチェックを使って、望ましいアノテーションの動作を理解します。Done では、示されている手動のキャストを必要とせずにフック仕様の型チェックを提供し、関連する API をカバーできるようにします。
索引モデルが issue の本文から書いたものです。
説明
I think it would be helpful to support type annotations in hook specifications.
It isn't hard to add the necessary annotations to a hook specification but I couldn't work out how to integrate this with pluggy. I spent some time on this and worked out the specifics:
pluggy.HookspecMarkermust be modified with a type hint so that the decorator does not obscure the type hints added to the specification.- When a hook is registered the
.hookattribute of thepluggy.manager.PluginManagerinstance myst be cast so that mypy can connect the specification to the registered hooks.
Here is a full example:
import pluggy # type: ignore
from typing import TypeVar, Callable, Any, cast
# Improvement suggested by @oremanj on python/typing gitter
F = TypeVar("F", bound=Callable[..., Any])
hookspec = cast(Callable[[F], F], pluggy.HookspecMarker("myproject"))
hookimpl = pluggy.HookimplMarker("myproject")
class MySpec(object):
"""A hook specification namespace."""
@hookspec
def myhook(self, arg1: int, arg2: int) -> int:
"""My special little hook that you can customize."""
class Plugin_1(object):
"""A hook implementation namespace."""
@hookimpl
def myhook(self, arg1: int, arg2: int) -> int:
print("inside Plugin_1.myhook()")
return arg1 + arg2 + 'a'
# create a manager and add the spec
pm = pluggy.PluginManager("myproject")
pm.add_hookspecs(MySpec)
# register plugins
pm.register(Plugin_1())
# Add cast so that mypy knows that pm.hook
# is actually a MySpec instance. Without this
# hint there really is no way for mypy to know
# this.
pm.hook = cast(MySpec, pm.hook)
# Uncomment these when running through mypy to see
# how mypy regards the type
# reveal_type(pm.hook)
# reveal_type(pm.hook.myhook)
# reveal_type(MySpec.myhook)
# this will now be caught by mypy
results = pm.hook.myhook(arg1=1, arg2="1")
print(results)
Output when checking with mypy:
$ mypy plug.py
plug.py:24: error: Unsupported operand types for + ("int" and "str")
plug.py:47: error: Argument "arg2" to "myhook" of "MySpec" has incompatible type "str"; expec
ted "int"
My original StackOverflow question and answer: https://stackoverflow.com/questions/54674679/how-can-i-annotate-types-for-a-pluggy-hook-specification
- 主要言語
- Python
- スター
- 1.7k
- フォーク
- 170
- 平均マージ
- 1日 23時間
- マージ済み PR(30日)
- 16
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
pytest-dev/pluggy のほかの issue
-
[Downstream] devpi broken - trying to register `None`対応中かも @bluetech が 5 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 58/100
pytest-dev/pluggy#749 · コメント 4 件 ·
メンテナーはふだん 1 日以内に返信
-
How to handle keyword-only arguments対応中かも @bluetech が 10 日前に担当しました。 オープン
pytest-dev/pluggy#742 · コメント 1 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
pytest-dev/pluggy#705 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 20/100
pytest-dev/pluggy#628 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
pytest-dev/pluggy#588 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
pytest-dev/pluggy の issue をすべて見る
似ている issue
-
[Bug]: Bedrock request metadata forwarding does not work for /embeddings対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープンbug llm translation
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
Arkansas 2025 tax is $1.70 high above $100,000 net taxable income ($3,809 + 3.9% rule)対応中かも @PavelMakarchuk が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
PolicyEngine/policyengine-us#9828 ·
メンテナーはふだん 2 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
jellyfin/jellyfin-mpv-shim#800 ·
メンテナーはふだん 1 日以内に返信
-
skillfs: one malformed chat-log line aborts the entire skill-usage analysis (skill_usage_from_chat_logs.py)対応中かも @zjncs が今日担当しました。 オープンcomponent:skillfs
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
agentic-os-org/ANOLISA#6116 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
P4: low query
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
jeffknupp/association#336 ·