Skip to content

Commit

Permalink
fix casts
Browse files Browse the repository at this point in the history
  • Loading branch information
RonnyPfannschmidt committed Sep 20, 2023
1 parent a3d2066 commit 90c4a19
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
12 changes: 9 additions & 3 deletions src/pluggy/_callers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
Call loop machinery
"""
from __future__ import annotations

from ._hooks import HookImpl
from ._result import _raise_wrapfail
from ._result import HookCallError
from ._result import Result

TYPE_CHECKING = False
if TYPE_CHECKING:
from typing import cast
Expand All @@ -17,10 +19,14 @@

# Need to distinguish between old- and new-style hook wrappers.
# Wrapping one a singleton tuple is the fastest type-safe way I found to do it.

Check warning on line 21 in src/pluggy/_callers.py

View check run for this annotation

Codecov / codecov/patch

src/pluggy/_callers.py#L21

Added line #L21 was not covered by tests
Teardown = Union[
Teardown = Union[
Tuple[Generator[None, Result[object], None]],
Generator[None, object, object],
]
else:

def cast(t, v):
return v


def _multicall(
Expand Down Expand Up @@ -57,7 +63,7 @@ def _multicall(
# If this cast is not valid, a type error is raised below,
# which is the desired response.
res = hook_impl.function(*args)
wrapper_gen = cast(Generator[None, Result[object], None], res)
wrapper_gen = cast("Generator[None, Result[object], None]", res)
next(wrapper_gen) # first yield
teardowns.append((wrapper_gen,))
except StopIteration:
Expand All @@ -67,7 +73,7 @@ def _multicall(
# If this cast is not valid, a type error is raised below,
# which is the desired response.
res = hook_impl.function(*args)
function_gen = cast(Generator[None, object, object], res)
function_gen = cast("Generator[None, object, object]", res)
next(function_gen) # first yield
teardowns.append(function_gen)
except StopIteration:
Expand Down
9 changes: 2 additions & 7 deletions src/pluggy/_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

from ._result import Result


_T = TypeVar("_T")
_F = TypeVar("_F", bound=Callable[..., object])

Check warning on line 34 in src/pluggy/_hooks.py

View check run for this annotation

Codecov / codecov/patch

src/pluggy/_hooks.py#L30-L34

Added lines #L30 - L34 were not covered by tests
_Namespace = Union[ModuleType, type]
Expand All @@ -41,7 +40,6 @@
_HookImplFunction = Callable[..., Union[_T, Generator[None, Result[_T], None]]]
_CallHistory = List[Tuple[Mapping[str, object], Optional[Callable[[Any], None]]]]

Check warning on line 41 in src/pluggy/_hooks.py

View check run for this annotation

Codecov / codecov/patch

src/pluggy/_hooks.py#L41

Added line #L41 was not covered by tests


class HookspecOpts(TypedDict):
"""Options for a hook specification."""

Check warning on line 45 in src/pluggy/_hooks.py

View check run for this annotation

Codecov / codecov/patch

src/pluggy/_hooks.py#L45

Added line #L45 was not covered by tests
Expand All @@ -52,7 +50,6 @@ class HookspecOpts(TypedDict):
#: Whether the hook :ref:`warns when implemented <warn_on_impl>`.
warn_on_impl: Warning | None

Check warning on line 52 in src/pluggy/_hooks.py

View check run for this annotation

Codecov / codecov/patch

src/pluggy/_hooks.py#L52

Added line #L52 was not covered by tests

class HookimplOpts(TypedDict):
"""Options for a hook implementation."""

Expand All @@ -74,11 +71,11 @@ class HookimplOpts(TypedDict):
specname: str | None

else:

Check warning on line 74 in src/pluggy/_hooks.py

View check run for this annotation

Codecov / codecov/patch

src/pluggy/_hooks.py#L74

Added line #L74 was not covered by tests
def final(func: _F) -> _F:
return func
overload = final


overload = final


@final
Expand Down Expand Up @@ -377,8 +374,6 @@ def __getattr__(self, name: str) -> HookCaller:
_HookRelay = HookRelay




class HookCaller:
"""A caller of all registered implementations of a hook specification."""

Expand Down
11 changes: 8 additions & 3 deletions src/pluggy/_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Hook wrapper "result" utilities.
"""
from __future__ import annotations

TYPE_CHECKING = False
if TYPE_CHECKING:
from types import TracebackType
Expand All @@ -16,16 +17,20 @@
from typing import Type
from typing import TypeVar


_ExcInfo = Tuple[Type[BaseException], BaseException, Optional[TracebackType]]

Check warning on line 20 in src/pluggy/_result.py

View check run for this annotation

Codecov / codecov/patch

src/pluggy/_result.py#L20

Added line #L20 was not covered by tests
ResultType = TypeVar("ResultType")
else:
from ._hooks import final

def cast(v, t):
return t

class Generic:
"""fake generic"""
def __class_getitem__(cls, key)-> type[object]:

def __class_getitem__(cls, key) -> type[object]:
return object

ResultType = "ResultType"


Expand Down Expand Up @@ -118,7 +123,7 @@ def get_result(self) -> ResultType:
__tracebackhide__ = True
exc = self._exception
if exc is None:
return cast(ResultType, self._result)
return cast("ResultType", self._result)
else:
raise exc.with_traceback(exc.__traceback__)

Expand Down

0 comments on commit 90c4a19

Please sign in to comment.