-
Notifications
You must be signed in to change notification settings - Fork 123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add type checking for hook specifications #191
Comments
@rbdixon woo I actually really like this. Would you mind making a PR and some tests. |
We also can consider just waiting a bit regarding py2; pytest plans to drop support in 5.0 mid-year, and we are almost May. We have to see the timeline for the other projects though ( |
The policy for devpi is pinning dependencies when necessary and dropping Python 2.x and 3.4 support when the workarounds become too cumbersome. We can't force others to hold back too much. |
tox plan is also mid-year but might go into autumn... not in a hurry yet 👍 |
any update on this? Mypy returns an error because of
|
Thanks @youtux for the ping. So far no updates, but now that we dropped Python 2, should be simpler. If anybody wants to work on that, we would be glad to review and merge a PR! 👍 |
Actually, it seems that the codebase it's already annotated since this PR: #340. Was a version ever released since then? |
The codebase is indeed type annotated, but the type checking added by #340 is for internal use only: This issue however is about this code in particular being type checked: # this will now be caught by mypy
results = pm.hook.myhook(arg1=1, arg2="1") Which we don't support currently (@bluetech can correct me if I'm wrong).
No, but mostly because those were internal changes so far not warranting a new release... we have #364, but the official 3.11 support was mostly adding it to CI as it was working with 3.11 already. |
I see. I have a different use case though, so having just a release of pluggy with the |
(probably I should have opened a different issue rather than posting in this one) |
Related: pytest-dev#191 Related: https://peps.python.org/pep-0561/
I just found the OP's StackOverflow post and used it as a guide to implement type annotations for my hookspecs; the content in the first post of this issue is not actually quite right, as the I would be happy to write up a section for the docs giving users some guidance on how to implement the proper types in their usage of pluggy; part of what I've done will be obsoleted by the type hints that were just added to the typeshed (and then later when |
Ok I think I implemented basically ideal static type hinting & runtime signature copying for Pluggy Overview
Methodology for compile-time hintsI defined some extra Methodology for runtime signaturesI copy over the from pluggy import HookspecMarker, HookimplMarker, PluginManager, HookimplOpts, HookCaller, HookspecOpts
ParamsT = ParamSpec('ParamsT')
ReturnT = TypeVar('ReturnT')
class HookSpecDecoratorThatReturnsFirstResult(Protocol):
def __call__(self, func: Callable[ParamsT, ReturnT]) -> Callable[ParamsT, ReturnT]: ...
class HookSpecDecoratorThatReturnsListResults(Protocol):
def __call__(self, func: Callable[ParamsT, ReturnT]) -> Callable[ParamsT, List[ReturnT]]: ...
class TypedHookspecMarker(HookspecMarker):
"""Improved version of pluggy.HookspecMarker that supports type inference of hookspecs with firstresult=True|False correctly"""
# handle @hookspec(firstresult=False) -> List[ReturnT] (test_firstresult_False_hookspec)
@overload
def __call__(
self,
function: None = ...,
firstresult: Literal[False] = ...,
historic: bool = ...,
warn_on_impl: Warning | None = ...,
warn_on_impl_args: Mapping[str, Warning] | None = ...,
) -> HookSpecDecoratorThatReturnsListResults: ...
# handle @hookspec(firstresult=True) -> ReturnT (test_firstresult_True_hookspec)
@overload
def __call__(
self,
function: None = ...,
firstresult: Literal[True] = ...,
historic: bool = ...,
warn_on_impl: Warning | None = ...,
warn_on_impl_args: Mapping[str, Warning] | None = ...,
) -> HookSpecDecoratorThatReturnsFirstResult: ...
# handle @hookspec -> List[ReturnT] (test_normal_hookspec)
# @overload order matters!!! this one must come last
@overload
def __call__(
self,
function: Callable[ParamsT, ReturnT] = ...,
firstresult: Literal[False] = ...,
historic: bool = ...,
warn_on_impl: Warning | None = ...,
warn_on_impl_args: Mapping[str, Warning] | None = ...,
) -> Callable[ParamsT, List[ReturnT]]: ...
def __call__(
self,
function: Callable[ParamsT, ReturnT] | None = None,
firstresult: bool = False,
historic: bool = False,
warn_on_impl: Warning | None = None,
warn_on_impl_args: Mapping[str, Warning] | None = None,
) -> Callable[ParamsT, List[ReturnT]] | HookSpecDecoratorThatReturnsFirstResult | HookSpecDecoratorThatReturnsListResults:
return super().__call__(function=function, firstresult=firstresult, historic=historic, warn_on_impl=warn_on_impl, warn_on_impl_args=warn_on_impl_args)
PluginSpec = TypeVar("PluginSpec")
class TypedPluginManager(PluginManager, Generic[PluginSpec]):
"""
Improved version of pluggy.PluginManager that allows static type inference of HookCaller calls based on underlying hookspec.
"""
# enable static type checking of pm.hook.call() calls
# https://stackoverflow.com/a/62871889/2156113
# https://github.com/pytest-dev/pluggy/issues/191
hook: PluginSpec
def create_typed_hookcaller(self, name: str, module_or_class: Type[PluginSpec], spec_opts: Dict[str, Any]) -> HookCaller:
"""
create a new HookCaller subclass with a modified __signature__
so that the return type is correct and args are converted to kwargs
"""
TypedHookCaller = type('TypedHookCaller', (HookCaller,), {})
hookspec_signature = inspect.signature(getattr(module_or_class, name))
hookspec_return_type = hookspec_signature.return_annotation
# replace return type with list if firstresult=False
hookcall_return_type = hookspec_return_type if spec_opts['firstresult'] else List[hookspec_return_type]
# replace each arg with kwarg equivalent (pm.hook.call() only accepts kwargs)
args_as_kwargs = [
param.replace(kind=inspect.Parameter.KEYWORD_ONLY) if param.name != 'self' else param
for param in hookspec_signature.parameters.values()
]
TypedHookCaller.__signature__ = hookspec_signature.replace(parameters=args_as_kwargs, return_annotation=hookcall_return_type)
TypedHookCaller.__name__ = f'{name}_HookCaller'
return TypedHookCaller(name, self._hookexec, module_or_class, spec_opts)
def add_hookspecs(self, module_or_class: Type[PluginSpec]) -> None:
"""Add HookSpecs from the given class, (generic type allows us to enforce types of pm.hook.call() statically)"""
names = []
for name in dir(module_or_class):
spec_opts = self.parse_hookspec_opts(module_or_class, name)
if spec_opts is not None:
hc: HookCaller | None = getattr(self.hook, name, None)
if hc is None:
hc = self.create_typed_hookcaller(name, module_or_class, spec_opts)
setattr(self.hook, name, hc)
else:
# Plugins registered this hook without knowing the spec.
hc.set_specification(module_or_class, spec_opts)
for hookfunction in hc.get_hookimpls():
self._verify_hook(hc, hookfunction)
names.append(name)
if not names:
raise ValueError(
f"did not find any {self.project_name!r} hooks in {module_or_class!r}"
) Usagehookspec = TypedHookspecMarker("test")
class TestSpec:
@hookspec
def test_normal_hookspec(self, abc1: int) -> int:
...
@hookspec(firstresult=False)
def test_firstresult_False_hookspec(self, abc1: int) -> int:
...
@hookspec(firstresult=True)
def test_firstresult_True_hookspec(self, abc1: int) -> int:
...
TestPluginManager = TypedPluginManager[TestSpec]
pm = TestPluginManager("test")
pm.add_hookspecs(TestSpec)
# note this does not limit to a single PluginSpec, you can use multiple like so:
#
# class CombinedPluginSpec(Spec1, Spec2, Spec3):
# pass
#
# PluginManager = TypedPluginManager[CombinedPluginSpec]
# pm = PluginManager("test")
# pm.add_hookspecs(Spec1)
# pm.add_hookspecs(Spec2)
# pm.add_hookspecs(Spec3) Results
Can I submit a PR to pluggy with my changes? |
I think it's a helpful addition We ought to figure if there's a reasonable way to provide a mypy plugin that handles modules as spec and or unions of specs like pytest plugins that add new hooks |
For what it's worth, my solution was much simpler but probably not as comprehensive. |
@RonnyPfannschmidt yeah I had the same concern. It's not impossible to add module support to my implementation, but it is hard to provide a straighforward, consistent syntax for making a spec union of a mix of modules and classes together. I think it wouldn't be unreasonable to ask people to do this in order to get pluggy static type hinting, it's not too hard to make everything into a class manually, and that makes it more explicit / is less "magic" than if we try to invent a new union syntax: class FirstHookspec:
@hookspec
def some_hookspec(self) -> int: ...
import second_hookspec
SecondHookspecAsClass = type('SecondHookspecAsClass', (), second_hookspec.__dict__)
# Combining Two hookspecs
class CombinedHookspec(FirstHookspec, SecondHookspecAsClass):
pass
PluginManager = TypedPluginManager[CombinedHookspec]
pm = PluginManager("test")
pm.add_hookspecs(CombinedHookspec)
# OR you can register them the normal way, either way works:
pm.add_hookspecs(FirstHookspec)
pm.add_hookspecs(second_hookspec) I don't think something like this is possible with the python type system anyway: PluginManager = TypedPluginManager[FirstHookspec | second_hookspec] Even with the new |
That's why I mentioned mypy plugins,, it's simply impossible natively |
However having a starting point is key |
I'm not sure extending mypy & pyright is worth it, imo it's too "magic" to merge modules and classes at the type level without forcing the user to do a little bit of work to understanad what's happening. I think you'd eventually run into weird issues where tooling trying to introspect |
One certainly wouldn't want to merge the types themselves But being able to do valiated casts and/or enumerations of the hooks might be enough It's certainly not going to be easy |
Modules are accepted in place of a Protocol in some places, which could help with this:
It seems like
Interestingly I am able to get combined type hinting for a simple
def test_func_from_module(abc1: int) -> int:
return 123
class SpecFromClass:
def test_func_from_class(self, abc1: int) -> int:
return 456
import inspect
from typing import TypeVar, Union, TypeVar, Type, Protocol, cast, List, Tuple, reveal_type
from types import ModuleType
import spec_from_module
import spec_from_class
ModuleT = TypeVar('ModuleT', bound=ModuleType)
ClassT = TypeVar('ClassT', bound=Type)
def combined_spec(*namespaces: ModuleT | ClassT]) -> ModuleT | ClassT:
return type('CombinedSpec', tuple(
namespace if inspect.isclass(namespace) else type(namespace.__name__, (), namespace.__dict__)
for namespace in namespaces
), {})
CombinedSpec = combined_spec(spec_from_module, spec_from_class.SpecFromClass)
print(inspect.signature(CombinedSpec.test_func_from_class))
# (self, abc1: int) -> int
print(inspect.signature(CombinedSpec.test_func_from_module))
# (abc1: int) -> int
reveal_type(CombinedSpec)
# Module("spec_from_module") | SpecFromClass
print(CombinedSpec.test_func_from_module(abc1=123))
# 123
print(CombinedSpec().test_func_from_class(abc1=123))
# 456 |
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.HookspecMarker
must be modified with a type hint so that the decorator does not obscure the type hints added to the specification..hook
attribute of thepluggy.manager.PluginManager
instance myst be cast so that mypy can connect the specification to the registered hooks.Here is a full example:
Output when checking with mypy:
My original StackOverflow question and answer: https://stackoverflow.com/questions/54674679/how-can-i-annotate-types-for-a-pluggy-hook-specification
The text was updated successfully, but these errors were encountered: