Add type checking for hook specifications
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
Research direction
Start by inspecting pluggy.HookspecMarker and PluginManager, especially hook registration and the pm.hook attribute. Use the issue's MySpec example and mypy checks to understand the desired annotation behavior. Done should provide type checking for hook specifications without requiring the demonstrated manual casts, with coverage for the relevant APIs.
Written by the indexing model from the issue text.
Description
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
- Dominant language
- Python
- Stars
- 1.7k
- Forks
- 170
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 16
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pytest-dev/pluggy
-
[Downstream] devpi broken - trying to register `None`Possibly taken @bluetech claimed this 3 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 58/100
pytest-dev/pluggy#749 · 4 comments ·
Maintainers usually reply within 1 day
-
How to handle keyword-only argumentsPossibly taken @bluetech claimed this 8 days ago. Open
pytest-dev/pluggy#742 · 1 comment · 1 assignee ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
pytest-dev/pluggy#705 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 20/100
pytest-dev/pluggy#628 · 2 comments ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
pytest-dev/pluggy#588 · 1 reaction ·
Maintainers usually reply within 1 day
All issues in pytest-dev/pluggy
Similar issues
-
[Bug]: Non-vision image fallback calls vision_analyze with an empty source for oversized inline images and tells the model the image is corruptPossibly taken @liuhao1024 claimed this today. Opencomp/agent P2 tool/vision type/bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
NousResearch/hermes-agent#132605 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
pymc-labs/pymc-marketing#3102 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
vllm-project/compressed-tensors#920 ·
Maintainers usually reply within 1 day
-
documentation good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
accepted bug wg/router-models-inference-runtime
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
vllm-project/semantic-router#4524 · 1 comment ·
Maintainers usually reply within 1 day