Skip to content
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 typing information to all of pluggy #326

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Initial type annotations
  • Loading branch information
RonnyPfannschmidt committed Aug 18, 2021
commit 9330ac746062efff4359dcc7f10cd9a71a441d3a
12 changes: 12 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[mypy]
#python_version = 3.6
mypy_path = $MYPY_CONFIG_FILE_DIR/src

warn_return_any = True
warn_unused_configs = True

[mypy-pluggy.*]

disallow_untyped_defs = True
warn_return_any = True
disallow_any_expr = True
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dev =
testing =
pytest
pytest-benchmark

mypy
importlib-metadata>=0.12 # keep in sync
[devpi:upload]
formats=sdist.tgz,bdist_wheel
51 changes: 46 additions & 5 deletions src/pluggy/_callers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,61 @@
"""
import sys

from ._result import HookCallError, _Result, _raise_wrapfail
from ._result import (
HookCallError,
_Result,
WrapResult,
_raise_wrapfail,
EXCINFO,
SomeResult,
)
from typing import List, TYPE_CHECKING, Dict, cast, overload, Union, Callable
from typing_extensions import Literal

if TYPE_CHECKING:
from ._hooks import HookImpl

def _multicall(hook_name, hook_impls, caller_kwargs, firstresult):
HookImpls = List["HookImpl"]
HookArgs = Dict[str, object]
HookResultCallback = Callable[[SomeResult], SomeResult]


@overload
def _multicall(
hook_name: str,
hook_impls: HookImpls,
caller_kwargs: HookArgs,
firstresult: Literal[False],
) -> List[SomeResult]:
pass


@overload
def _multicall(
hook_name: str,
hook_impls: HookImpls,
caller_kwargs: HookArgs,
firstresult: Literal[True],
) -> SomeResult:
pass


def _multicall(
hook_name: str,
hook_impls: HookImpls,
caller_kwargs: HookArgs,
firstresult: bool,
) -> Union[List[SomeResult], SomeResult]:
"""Execute a call into multiple python functions/methods and return the
result(s).

``caller_kwargs`` comes from _HookCaller.__call__().
"""
__tracebackhide__ = True
results = []
excinfo = None
excinfo: EXCINFO = None, None, None
try: # run impl and wrapper setup functions in a loop
teardowns = []
teardowns: List[WrapResult] = []
try:
for hook_impl in reversed(hook_impls):
try:
Expand All @@ -30,7 +71,7 @@ def _multicall(hook_name, hook_impls, caller_kwargs, firstresult):

if hook_impl.hookwrapper:
try:
gen = hook_impl.function(*args)
gen = cast(WrapResult, hook_impl.function(*args))
next(gen) # first yield
teardowns.append(gen)
except StopIteration:
Expand Down
Loading