Skip to content

WIP: experiment with mypyc #591

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
requires = [
"setuptools>=65.0",
"setuptools-scm[toml]>=8.0",
"mypy>=1.15",
]
build-backend = "setuptools.build_meta"

Expand Down
2 changes: 0 additions & 2 deletions src/pluggy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"HookspecOpts",
"HookimplOpts",
"HookImpl",
"HookRelay",
"HookspecMarker",
"HookimplMarker",
"Result",
Expand All @@ -18,7 +17,6 @@
from ._hooks import HookImpl
from ._hooks import HookimplMarker
from ._hooks import HookimplOpts
from ._hooks import HookRelay
from ._hooks import HookspecMarker
from ._hooks import HookspecOpts
from ._manager import PluginManager
Expand Down
27 changes: 27 additions & 0 deletions src/pluggy/_hookrelay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
separate module for only hookrelay as mypyc doesnt support dynamic attributes/getattr
"""

from __future__ import annotations

from typing import final
from typing import TYPE_CHECKING


if TYPE_CHECKING:
from pluggy import HookCaller


@final
class HookRelay:
"""Hook holder object for performing 1:N hook calls where N is the number
of registered plugins."""

__slots__ = ("__dict__",)

def __init__(self) -> None:
""":meta private:"""

if TYPE_CHECKING:

def __getattr__(self, name: str) -> HookCaller: ...
30 changes: 9 additions & 21 deletions src/pluggy/_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,25 @@
from typing import final
from typing import Optional
from typing import overload
from typing import TYPE_CHECKING
from typing import TypedDict
from typing import TypeVar
from typing import Union
import warnings

from ._hookrelay import HookRelay
from ._result import Result


_T = TypeVar("_T")
_F = TypeVar("_F", bound=Callable[..., object])
_Namespace = Union[ModuleType, type]
_Plugin = object
_HookExec = Callable[
[str, Sequence["HookImpl"], Mapping[str, object], bool],
Union[object, list[object]],

type _HookExec = Callable[
[str, Sequence[HookImpl], Mapping[str, object], bool], Union[object, list[object]]
]
_HookImplFunction = Callable[..., Union[_T, Generator[None, Result[_T], None]]]

type _HookImplFunction[T] = Callable[..., Union[T, Generator[None, Result[T], None]]]


class HookspecOpts(TypedDict):
Expand Down Expand Up @@ -355,21 +356,6 @@ def varnames(func: object) -> tuple[tuple[str, ...], tuple[str, ...]]:
return args, kwargs


@final
class HookRelay:
"""Hook holder object for performing 1:N hook calls where N is the number
of registered plugins."""

__slots__ = ("__dict__",)

def __init__(self) -> None:
""":meta private:"""

if TYPE_CHECKING:

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


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

Expand All @@ -388,6 +374,8 @@ class HookCaller:
"_call_history",
)

_call_history: _CallHistory | None

def __init__(
self,
name: str,
Expand All @@ -407,7 +395,7 @@ def __init__(
# 5. wrappers
# 6. tryfirst wrappers
self._hookimpls: Final[list[HookImpl]] = []
self._call_history: _CallHistory | None = None
self._call_history = None
# TODO: Document, or make private.
self.spec: HookSpec | None = None
if specmodule_or_class is not None:
Expand Down
9 changes: 6 additions & 3 deletions src/pluggy/_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
from typing import cast
from typing import Final
from typing import TYPE_CHECKING
from typing import TypeAlias
import warnings

from . import _tracing
from ._callers import _multicall
from ._hookrelay import HookRelay
from ._hooks import _HookImplFunction
from ._hooks import _Namespace
from ._hooks import _Plugin
from ._hooks import _SubsetHookCaller
from ._hooks import HookCaller
from ._hooks import HookImpl
from ._hooks import HookimplOpts
from ._hooks import HookRelay
from ._hooks import HookspecOpts
from ._hooks import normalize_hookimpl_opts
from ._result import Result
Expand All @@ -32,8 +33,10 @@
import importlib.metadata


_BeforeTrace = Callable[[str, Sequence[HookImpl], Mapping[str, Any]], None]
_AfterTrace = Callable[[Result[Any], str, Sequence[HookImpl], Mapping[str, Any]], None]
_BeforeTrace: TypeAlias = Callable[[str, Sequence[HookImpl], Mapping[str, Any]], None]
_AfterTrace: TypeAlias = Callable[
[Result[Any], str, Sequence[HookImpl], Mapping[str, Any]], None
]


def _warn_for_function(warning: Warning, function: Callable[..., object]) -> None:
Expand Down
6 changes: 3 additions & 3 deletions src/pluggy/_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ def exception(self) -> BaseException | None:
def from_call(cls, func: Callable[[], ResultType]) -> Result[ResultType]:
""":meta private:"""
__tracebackhide__ = True
result = exception = None
try:
result = func()
except BaseException as exc:
exception = exc
return cls(result, exception)
return cls(None, exc)
else:
return cls(result, None)

def force_result(self, result: ResultType) -> None:
"""Force the result(s) to ``result``.
Expand Down
Loading