diff --git a/src/pluggy/__init__.py b/src/pluggy/__init__.py index 36ce1680..8aacb0bc 100644 --- a/src/pluggy/__init__.py +++ b/src/pluggy/__init__.py @@ -25,13 +25,25 @@ from ._hooks import HookCaller 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 from ._manager import PluginValidationError from ._result import HookCallError from ._result import Result from ._warnings import PluggyTeardownRaisedWarning from ._warnings import PluggyWarning + + +TYPE_CHECKING = False +if TYPE_CHECKING: + from ._types import HookimplOpts + from ._types import HookspecOpts +else: + + def __getattr__(name: str) -> object: + if name.endswith("Opts"): + from . import _types + + return getattr(_types, name) + raise AttributeError(name) diff --git a/src/pluggy/_hooks.py b/src/pluggy/_hooks.py index c1a6f9c7..bb442ec5 100644 --- a/src/pluggy/_hooks.py +++ b/src/pluggy/_hooks.py @@ -26,7 +26,6 @@ from typing import Sequence from typing import Tuple from typing import TYPE_CHECKING - from typing import TypedDict from typing import TypeVar from typing import Union @@ -42,41 +41,8 @@ _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.""" - - #: Whether the hook is :ref:`first result only `. - firstresult: bool - #: Whether the hook is :ref:`historic `. - historic: bool - #: Whether the hook :ref:`warns when implemented `. - warn_on_impl: Warning | None - #: Whether the hook warns when :ref:`certain arguments are requested - #: `. - #: - #: .. versionadded:: 1.5 - warn_on_impl_args: Mapping[str, Warning] | None - - class HookimplOpts(TypedDict): - """Options for a hook implementation.""" - - #: Whether the hook implementation is a :ref:`wrapper `. - wrapper: bool - #: Whether the hook implementation is an :ref:`old-style wrapper - #: `. - hookwrapper: bool - #: Whether validation against a hook specification is :ref:`optional - #: `. - optionalhook: bool - #: Whether to try to order this hook implementation :ref:`first - #: `. - tryfirst: bool - #: Whether to try to order this hook implementation :ref:`last - #: `. - trylast: bool - #: The name of the hook specification to match, see :ref:`specname`. - specname: str | None - + from ._types import HookimplOpts + from ._types import HookspecOpts else: def final(func: _F) -> _F: @@ -382,8 +348,6 @@ 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 diff --git a/src/pluggy/_manager.py b/src/pluggy/_manager.py index 87259451..10c16ef6 100644 --- a/src/pluggy/_manager.py +++ b/src/pluggy/_manager.py @@ -27,9 +27,9 @@ from ._hooks import _HookImplFunction from ._hooks import _Namespace - from ._hooks import HookimplOpts - from ._hooks import HookspecOpts from ._importlib_metadata import DistFacade + from ._types import HookimplOpts + from ._types import HookspecOpts _BeforeTrace: TypeAlias = "Callable[[str, Sequence[HookImpl], Mapping[str, Any]], None]" _AfterTrace: TypeAlias = ( diff --git a/src/pluggy/_types.py b/src/pluggy/_types.py new file mode 100644 index 00000000..1e978f8e --- /dev/null +++ b/src/pluggy/_types.py @@ -0,0 +1,43 @@ +from typing import Mapping +from typing import TypedDict +import warnings + + +warnings.warn(ImportWarning(f"{__name__} imported outside of type checking")) + + +class HookspecOpts(TypedDict): + """Options for a hook specification.""" + + #: Whether the hook is :ref:`first result only `. + firstresult: bool + #: Whether the hook is :ref:`historic `. + historic: bool + #: Whether the hook :ref:`warns when implemented `. + warn_on_impl: Warning | None + #: Whether the hook warns when :ref:`certain arguments are requested + #: `. + #: + #: .. versionadded:: 1.5 + warn_on_impl_args: Mapping[str, Warning] | None + + +class HookimplOpts(TypedDict): + """Options for a hook implementation.""" + + #: Whether the hook implementation is a :ref:`wrapper `. + wrapper: bool + #: Whether the hook implementation is an :ref:`old-style wrapper + #: `. + hookwrapper: bool + #: Whether validation against a hook specification is :ref:`optional + #: `. + optionalhook: bool + #: Whether to try to order this hook implementation :ref:`first + #: `. + tryfirst: bool + #: Whether to try to order this hook implementation :ref:`last + #: `. + trylast: bool + #: The name of the hook specification to match, see :ref:`specname`. + specname: str | None