Add type checking for hook specifications
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 35/100
Direzione di ricerca
Inizia esaminando pluggy.HookspecMarker e PluginManager, in particolare la registrazione degli hook e l’attributo pm.hook. Usa l’esempio MySpec dell’issue e i controlli di mypy per comprendere il comportamento desiderato delle annotazioni. Done dovrebbe fornire il controllo dei tipi per le specifiche degli hook senza richiedere i cast manuali mostrati, con copertura delle API pertinenti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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
- Lingua principale
- Python
- Stelle
- 1.7k
- Fork
- 170
- Merge medio
- 1g 23h
- PR unite (30g)
- 16
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di pytest-dev/pluggy
-
[Downstream] devpi broken - trying to register `None`Forse già presa @bluetech l’ha presa 4 giorni fa. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 58/100
pytest-dev/pluggy#749 · 4 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
How to handle keyword-only argumentsForse già presa @bluetech l’ha presa 9 giorni fa. Aperta
pytest-dev/pluggy#742 · 1 commento · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
pytest-dev/pluggy#705 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 20/100
pytest-dev/pluggy#628 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
pytest-dev/pluggy#588 · 1 reazione ·
I maintainer di solito rispondono entro 1 giorno