Skip to content

Commit d3e72b0

Browse files
committed
Rename _Result -> Result, export as pluggy.Result
For typing purposes, refs #428. The old name `pluggy._result._Result` is kept for backward compatibility, no reason to break the "offenders" who have imported it before.
1 parent 6f28953 commit d3e72b0

File tree

8 files changed

+37
-32
lines changed

8 files changed

+37
-32
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Features
5151

5252
- `#260 <https://github.com/pytest-dev/pluggy/issues/260>`_: Added "new-style" hook wrappers, a simpler but equally powerful alternative to the existing ``hookwrapper=True`` wrappers.
5353

54-
New-style wrappers are generator functions, similarly to ``hookwrapper``, but do away with the :class:`result <pluggy._result._Result>` object.
54+
New-style wrappers are generator functions, similarly to ``hookwrapper``, but do away with the :class:`result <pluggy.Result>` object.
5555
Instead, the return value is sent directly to the ``yield`` statement, or, if inner calls raised an exception, it is raised from the ``yield``.
5656
The wrapper is expected to return a value or raise an exception, which will become the result of the hook call.
5757

@@ -64,7 +64,7 @@ Features
6464
- `#364 <https://github.com/pytest-dev/pluggy/issues/364>`_: Python 3.11 and 3.12 are now officially supported.
6565

6666

67-
- `#394 <https://github.com/pytest-dev/pluggy/issues/394>`_: Added the :meth:`~pluggy._result._Result.force_exception` method to ``_Result``.
67+
- `#394 <https://github.com/pytest-dev/pluggy/issues/394>`_: Added the :meth:`~pluggy.Result.force_exception` method to ``_Result``.
6868

6969
``force_exception`` allows (old-style) hookwrappers to force an exception or override/adjust an existing exception of a hook invocation,
7070
in a properly behaving manner. Using ``force_exception`` is preferred over raising an exception from the hookwrapper,

docs/api_reference.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ API Reference
1414

1515
.. autodecorator:: pluggy.HookimplMarker
1616

17-
.. autoclass:: pluggy._result._Result()
17+
.. autoclass:: pluggy.Result()
1818
:show-inheritance:
1919
:members:
2020

docs/index.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -472,21 +472,21 @@ execution of all corresponding non-wrappper *hookimpls*.
472472
if config.use_defaults:
473473
outcome.force_result(defaults)
474474
475-
The generator is :py:meth:`sent <python:generator.send>` a :py:class:`pluggy._result._Result` object which can
475+
The generator is :py:meth:`sent <python:generator.send>` a :py:class:`pluggy.Result` object which can
476476
be assigned in the ``yield`` expression and used to inspect
477477
the final result(s) or exceptions returned back to the caller using the
478-
:py:meth:`~pluggy._result._Result.get_result` method, override the result
479-
using the :py:meth:`~pluggy._result._Result.force_result`, or override
480-
the exception using the :py:meth:`~pluggy._result._Result.force_exception`
478+
:py:meth:`~pluggy.Result.get_result` method, override the result
479+
using the :py:meth:`~pluggy.Result.force_result`, or override
480+
the exception using the :py:meth:`~pluggy.Result.force_exception`
481481
method.
482482

483483
.. note::
484484
Old-style hook wrappers can **not** return results; they can only modify
485-
them using the :py:meth:`~pluggy._result._Result.force_result` API.
485+
them using the :py:meth:`~pluggy.Result.force_result` API.
486486

487487
Old-style Hook wrappers should **not** raise exceptions; this will cause
488488
further hookwrappers to be skipped. They should use
489-
:py:meth:`~pluggy._result._Result.force_exception` to adjust the
489+
:py:meth:`~pluggy.Result.force_exception` to adjust the
490490
exception.
491491

492492
.. _specs:

src/pluggy/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
"HookCallError",
1212
"HookspecMarker",
1313
"HookimplMarker",
14+
"Result",
1415
]
1516

1617
from ._manager import PluginManager, PluginValidationError
17-
from ._result import HookCallError
18+
from ._result import HookCallError, Result
1819
from ._hooks import HookspecMarker, HookimplMarker

src/pluggy/_callers.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212

1313
from ._hooks import HookImpl
1414
from ._result import _raise_wrapfail
15-
from ._result import _Result
1615
from ._result import HookCallError
16+
from ._result import Result
1717

1818

1919
# Need to distinguish between old- and new-style hook wrappers.
2020
# Wrapping one a singleton tuple is the fastest type-safe way I found to do it.
2121
Teardown = Union[
22-
Tuple[Generator[None, _Result[object], None]],
22+
Tuple[Generator[None, Result[object], None]],
2323
Generator[None, object, object],
2424
]
2525

@@ -58,7 +58,7 @@ def _multicall(
5858
# If this cast is not valid, a type error is raised below,
5959
# which is the desired response.
6060
res = hook_impl.function(*args)
61-
wrapper_gen = cast(Generator[None, _Result[object], None], res)
61+
wrapper_gen = cast(Generator[None, Result[object], None], res)
6262
next(wrapper_gen) # first yield
6363
teardowns.append((wrapper_gen,))
6464
except StopIteration:
@@ -82,7 +82,7 @@ def _multicall(
8282
except BaseException as exc:
8383
exception = exc
8484
finally:
85-
# Fast path - only new-style wrappers, no _Result.
85+
# Fast path - only new-style wrappers, no Result.
8686
if only_new_style_wrappers:
8787
if firstresult: # first result hooks return a single value
8888
result = results[0] if results else None
@@ -117,11 +117,11 @@ def _multicall(
117117
# Slow path - need to support old-style wrappers.
118118
else:
119119
if firstresult: # first result hooks return a single value
120-
outcome: _Result[object | list[object]] = _Result(
120+
outcome: Result[object | list[object]] = Result(
121121
results[0] if results else None, exception
122122
)
123123
else:
124-
outcome = _Result(results, exception)
124+
outcome = Result(results, exception)
125125

126126
# run all wrapper post-yield blocks
127127
for teardown in reversed(teardowns):

src/pluggy/_hooks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from typing import TypeVar
2424
from typing import Union
2525

26-
from ._result import _Result
26+
from ._result import Result
2727

2828

2929
_T = TypeVar("_T")
@@ -34,7 +34,7 @@
3434
[str, Sequence["HookImpl"], Mapping[str, object], bool],
3535
Union[object, List[object]],
3636
]
37-
_HookImplFunction = Callable[..., Union[_T, Generator[None, _Result[_T], None]]]
37+
_HookImplFunction = Callable[..., Union[_T, Generator[None, Result[_T], None]]]
3838

3939

4040
class _HookSpecOpts(TypedDict):
@@ -232,7 +232,7 @@ def __call__( # noqa: F811
232232
needs to execute exactly one ``yield``. The code before the
233233
``yield`` is run early before any non-hook-wrapper function is run.
234234
The code after the ``yield`` is run after all non-hook-wrapper
235-
function have run The ``yield`` receives a :class:`_Result` object
235+
function have run The ``yield`` receives a :class:`Result` object
236236
representing the exception or result outcome of the inner calls
237237
(including earlier hook wrapper calls). This option is mutually
238238
exclusive with ``wrapper``. See :ref:`old_style_hookwrapper`.

src/pluggy/_manager.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
from ._hooks import HookImpl
2626
from ._hooks import HookSpec
2727
from ._hooks import normalize_hookimpl_opts
28-
from ._result import _Result
28+
from ._result import Result
2929

3030

3131
_BeforeTrace = Callable[[str, Sequence[HookImpl], Mapping[str, Any]], None]
32-
_AfterTrace = Callable[[_Result[Any], str, Sequence[HookImpl], Mapping[str, Any]], None]
32+
_AfterTrace = Callable[[Result[Any], str, Sequence[HookImpl], Mapping[str, Any]], None]
3333

3434

3535
def _warn_for_function(warning: Warning, function: Callable[..., object]) -> None:
@@ -439,7 +439,7 @@ def add_hookcall_monitoring(
439439
of HookImpl instances and the keyword arguments for the hook call.
440440
441441
``after(outcome, hook_name, hook_impls, kwargs)`` receives the
442-
same arguments as ``before`` but also a :class:`~pluggy._result._Result` object
442+
same arguments as ``before`` but also a :class:`~pluggy.Result` object
443443
which represents the result of the overall hook call.
444444
"""
445445
oldcall = self._inner_hookexec
@@ -451,7 +451,7 @@ def traced_hookexec(
451451
firstresult: bool,
452452
) -> object | list[object]:
453453
before(hook_name, hook_impls, caller_kwargs)
454-
outcome = _Result.from_call(
454+
outcome = Result.from_call(
455455
lambda: oldcall(hook_name, hook_impls, caller_kwargs, firstresult)
456456
)
457457
after(outcome, hook_name, hook_impls, caller_kwargs)
@@ -478,7 +478,7 @@ def before(
478478
hooktrace(hook_name, kwargs)
479479

480480
def after(
481-
outcome: _Result[object],
481+
outcome: Result[object],
482482
hook_name: str,
483483
methods: Sequence[HookImpl],
484484
kwargs: Mapping[str, object],

src/pluggy/_result.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717

1818
_ExcInfo = Tuple[Type[BaseException], BaseException, Optional[TracebackType]]
19-
_T = TypeVar("_T")
19+
ResultType = TypeVar("ResultType")
2020

2121

2222
def _raise_wrapfail(
2323
wrap_controller: (
24-
Generator[None, _Result[_T], None] | Generator[None, object, object]
24+
Generator[None, Result[ResultType], None] | Generator[None, object, object]
2525
),
2626
msg: str,
2727
) -> NoReturn:
@@ -36,15 +36,15 @@ class HookCallError(Exception):
3636
"""Hook was called incorrectly."""
3737

3838

39-
class _Result(Generic[_T]):
39+
class Result(Generic[ResultType]):
4040
"""An object used to inspect and set the result in a :ref:`hook wrapper
4141
<hookwrappers>`."""
4242

4343
__slots__ = ("_result", "_exception")
4444

4545
def __init__(
4646
self,
47-
result: _T | None,
47+
result: ResultType | None,
4848
exception: BaseException | None,
4949
) -> None:
5050
""":meta private:"""
@@ -66,7 +66,7 @@ def exception(self) -> BaseException | None:
6666
return self._exception
6767

6868
@classmethod
69-
def from_call(cls, func: Callable[[], _T]) -> _Result[_T]:
69+
def from_call(cls, func: Callable[[], ResultType]) -> Result[ResultType]:
7070
""":meta private:"""
7171
__tracebackhide__ = True
7272
result = exception = None
@@ -76,7 +76,7 @@ def from_call(cls, func: Callable[[], _T]) -> _Result[_T]:
7676
exception = exc
7777
return cls(result, exception)
7878

79-
def force_result(self, result: _T) -> None:
79+
def force_result(self, result: ResultType) -> None:
8080
"""Force the result(s) to ``result``.
8181
8282
If the hook was marked as a ``firstresult`` a single value should
@@ -98,7 +98,7 @@ def force_exception(self, exception: BaseException) -> None:
9898
self._result = None
9999
self._exception = exception
100100

101-
def get_result(self) -> _T:
101+
def get_result(self) -> ResultType:
102102
"""Get the result(s) for this hook call.
103103
104104
If the hook was marked as a ``firstresult`` only a single value
@@ -107,6 +107,10 @@ def get_result(self) -> _T:
107107
__tracebackhide__ = True
108108
exc = self._exception
109109
if exc is None:
110-
return cast(_T, self._result)
110+
return cast(ResultType, self._result)
111111
else:
112112
raise exc.with_traceback(exc.__traceback__)
113+
114+
115+
# Historical name (pluggy<=1.2), kept for backward compatibility.
116+
_Result = Result

0 commit comments

Comments
 (0)