Skip to content

Commit

Permalink
fix casts
Browse files Browse the repository at this point in the history
  • Loading branch information
RonnyPfannschmidt committed Sep 3, 2024
1 parent 5c445a2 commit ff368da
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/pluggy/_callers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
Tuple[Generator[None, Result[object], None], HookImpl],
Generator[None, object, object],
]
else:

def cast(t, v):
return v

from ._hooks import HookImpl
from ._result import HookCallError
Expand Down Expand Up @@ -94,7 +98,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, hook_impl))
except StopIteration:
Expand All @@ -104,7 +108,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: 3 additions & 6 deletions src/pluggy/_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

from ._result import Result


_T = TypeVar("_T")
_F = TypeVar("_F", bound=Callable[..., object])
_Namespace = Union[ModuleType, type]
Expand All @@ -42,7 +41,6 @@
_HookImplFunction = Callable[..., Union[_T, Generator[None, Result[_T], None]]]
_CallHistory = List[Tuple[Mapping[str, object], Optional[Callable[[Any], None]]]]


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

Expand All @@ -58,7 +56,6 @@ class HookspecOpts(TypedDict):
#: .. versionadded:: 1.5
warn_on_impl_args: Mapping[str, Warning] | None


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

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

else:

def final(func: _F) -> _F:
return func
overload = final


overload = final


@final
Expand Down Expand Up @@ -384,12 +381,12 @@ def __init__(self) -> None:

def __getattr__(self, name: str) -> HookCaller: ...

_CallHistory = List[Tuple[Mapping[str, object], Optional[Callable[[Any], None]]]]

# Historical name (pluggy<=1.2), kept for backward compatibility.
_HookRelay = HookRelay


_CallHistory = List[Tuple[Mapping[str, object], Optional[Callable[[Any], None]]]]


class HookCaller:
Expand Down
11 changes: 8 additions & 3 deletions src/pluggy/_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

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]]
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 @@ -110,7 +115,7 @@ def get_result(self) -> ResultType:
exc = self._exception
tb = self._traceback
if exc is None:
return cast(ResultType, self._result)
return cast("ResultType", self._result)
else:
raise exc.with_traceback(tb)

Expand Down

0 comments on commit ff368da

Please sign in to comment.