|
5 | 5 | from __future__ import annotations |
6 | 6 |
|
7 | 7 | from collections.abc import Mapping |
8 | | -from typing import TypedDict |
9 | | - |
10 | | - |
11 | | -class HookspecOpts(TypedDict): |
12 | | - """Options for a hook specification.""" |
13 | | - |
14 | | - #: Whether the hook is :ref:`first result only <firstresult>`. |
15 | | - firstresult: bool |
16 | | - #: Whether the hook is :ref:`historic <historic>`. |
17 | | - historic: bool |
18 | | - #: Whether the hook :ref:`warns when implemented <warn_on_impl>`. |
19 | | - warn_on_impl: Warning | None |
20 | | - #: Whether the hook warns when :ref:`certain arguments are requested |
21 | | - #: <warn_on_impl>`. |
22 | | - #: |
23 | | - #: .. versionadded:: 1.5 |
24 | | - warn_on_impl_args: Mapping[str, Warning] | None |
25 | | - |
26 | | - |
27 | | -class HookimplOpts(TypedDict): |
28 | | - """Options for a hook implementation.""" |
29 | | - |
30 | | - #: Whether the hook implementation is a :ref:`wrapper <hookwrapper>`. |
31 | | - wrapper: bool |
32 | | - #: Whether the hook implementation is an :ref:`old-style wrapper |
33 | | - #: <old_style_hookwrappers>`. |
34 | | - hookwrapper: bool |
35 | | - #: Whether validation against a hook specification is :ref:`optional |
36 | | - #: <optionalhook>`. |
37 | | - optionalhook: bool |
38 | | - #: Whether to try to order this hook implementation :ref:`first |
39 | | - #: <callorder>`. |
40 | | - tryfirst: bool |
41 | | - #: Whether to try to order this hook implementation :ref:`last |
42 | | - #: <callorder>`. |
43 | | - trylast: bool |
44 | | - #: The name of the hook specification to match, see :ref:`specname`. |
45 | | - specname: str | None |
46 | | - |
47 | | - |
48 | | -def normalize_hookimpl_opts(opts: HookimplOpts) -> None: |
49 | | - opts.setdefault("tryfirst", False) |
50 | | - opts.setdefault("trylast", False) |
51 | | - opts.setdefault("wrapper", False) |
52 | | - opts.setdefault("hookwrapper", False) |
53 | | - opts.setdefault("optionalhook", False) |
54 | | - opts.setdefault("specname", None) |
| 8 | +from typing import Any |
| 9 | +from typing import Final |
| 10 | +from typing import final |
| 11 | + |
| 12 | + |
| 13 | +@final |
| 14 | +class HookspecConfiguration: |
| 15 | + """Configuration for a hook specification.""" |
| 16 | + |
| 17 | + __slots__ = ( |
| 18 | + "firstresult", |
| 19 | + "historic", |
| 20 | + "warn_on_impl", |
| 21 | + "warn_on_impl_args", |
| 22 | + ) |
| 23 | + firstresult: Final[bool] |
| 24 | + historic: Final[bool] |
| 25 | + warn_on_impl: Final[Warning | None] |
| 26 | + warn_on_impl_args: Final[Mapping[str, Warning] | None] |
| 27 | + |
| 28 | + def __init__( |
| 29 | + self, |
| 30 | + firstresult: bool = False, |
| 31 | + historic: bool = False, |
| 32 | + warn_on_impl: Warning | None = None, |
| 33 | + warn_on_impl_args: Mapping[str, Warning] | None = None, |
| 34 | + ) -> None: |
| 35 | + if historic and firstresult: |
| 36 | + raise ValueError("cannot have a historic firstresult hook") |
| 37 | + #: Whether the hook is :ref:`first result only <firstresult>`. |
| 38 | + self.firstresult = firstresult |
| 39 | + #: Whether the hook is :ref:`historic <historic>`. |
| 40 | + self.historic = historic |
| 41 | + #: Whether the hook :ref:`warns when implemented <warn_on_impl>`. |
| 42 | + self.warn_on_impl = warn_on_impl |
| 43 | + #: Whether the hook warns when :ref:`certain arguments are requested |
| 44 | + #: <warn_on_impl>`. |
| 45 | + self.warn_on_impl_args = warn_on_impl_args |
| 46 | + |
| 47 | + def __repr__(self) -> str: |
| 48 | + attrs = [ |
| 49 | + f"{slot}={getattr(self, slot)!r}" |
| 50 | + for slot in self.__slots__ |
| 51 | + if getattr(self, slot) |
| 52 | + ] |
| 53 | + return f"HookspecConfiguration({', '.join(attrs)})" |
| 54 | + |
| 55 | + |
| 56 | +@final |
| 57 | +class HookimplConfiguration: |
| 58 | + """Configuration for a hook implementation.""" |
| 59 | + |
| 60 | + __slots__ = ( |
| 61 | + "wrapper", |
| 62 | + "hookwrapper", |
| 63 | + "optionalhook", |
| 64 | + "tryfirst", |
| 65 | + "trylast", |
| 66 | + "specname", |
| 67 | + ) |
| 68 | + wrapper: Final[bool] |
| 69 | + hookwrapper: Final[bool] |
| 70 | + optionalhook: Final[bool] |
| 71 | + tryfirst: Final[bool] |
| 72 | + trylast: Final[bool] |
| 73 | + specname: Final[str | None] |
| 74 | + |
| 75 | + def __init__( |
| 76 | + self, |
| 77 | + wrapper: bool = False, |
| 78 | + hookwrapper: bool = False, |
| 79 | + optionalhook: bool = False, |
| 80 | + tryfirst: bool = False, |
| 81 | + trylast: bool = False, |
| 82 | + specname: str | None = None, |
| 83 | + ) -> None: |
| 84 | + #: Whether the hook implementation is a :ref:`wrapper <hookwrapper>`. |
| 85 | + self.wrapper = wrapper |
| 86 | + #: Whether the hook implementation is an :ref:`old-style wrapper |
| 87 | + #: <old_style_hookwrappers>`. |
| 88 | + self.hookwrapper = hookwrapper |
| 89 | + #: Whether validation against a hook specification is :ref:`optional |
| 90 | + #: <optionalhook>`. |
| 91 | + self.optionalhook = optionalhook |
| 92 | + #: Whether to try to order this hook implementation :ref:`first |
| 93 | + #: <callorder>`. |
| 94 | + self.tryfirst = tryfirst |
| 95 | + #: Whether to try to order this hook implementation :ref:`last |
| 96 | + #: <callorder>`. |
| 97 | + self.trylast = trylast |
| 98 | + #: The name of the hook specification to match, see :ref:`specname`. |
| 99 | + self.specname = specname |
| 100 | + |
| 101 | + def __repr__(self) -> str: |
| 102 | + attrs = [ |
| 103 | + f"{slot}={getattr(self, slot)!r}" |
| 104 | + for slot in self.__slots__ |
| 105 | + if getattr(self, slot) |
| 106 | + ] |
| 107 | + return f"HookimplConfiguration({', '.join(attrs)})" |
| 108 | + |
| 109 | + |
| 110 | +def hookspec_config_from_mapping( |
| 111 | + opts: Mapping[str, Any], |
| 112 | +) -> HookspecConfiguration: |
| 113 | + """Build a :class:`HookspecConfiguration` from a mapping. |
| 114 | +
|
| 115 | + Intended for pytest/support migration only — not the public options API. |
| 116 | + Prefer constructing :class:`HookspecConfiguration` directly. |
| 117 | + """ |
| 118 | + return HookspecConfiguration( |
| 119 | + firstresult=bool(opts.get("firstresult", False)), |
| 120 | + historic=bool(opts.get("historic", False)), |
| 121 | + warn_on_impl=opts.get("warn_on_impl"), |
| 122 | + warn_on_impl_args=opts.get("warn_on_impl_args"), |
| 123 | + ) |
| 124 | + |
| 125 | + |
| 126 | +def hookimpl_config_from_mapping( |
| 127 | + opts: Mapping[str, Any], |
| 128 | +) -> HookimplConfiguration: |
| 129 | + """Build a :class:`HookimplConfiguration` from a mapping. |
| 130 | +
|
| 131 | + Intended for pytest/support migration only — not the public options API. |
| 132 | + Prefer constructing :class:`HookimplConfiguration` directly. |
| 133 | + """ |
| 134 | + return HookimplConfiguration( |
| 135 | + wrapper=bool(opts.get("wrapper", False)), |
| 136 | + hookwrapper=bool(opts.get("hookwrapper", False)), |
| 137 | + optionalhook=bool(opts.get("optionalhook", False)), |
| 138 | + tryfirst=bool(opts.get("tryfirst", False)), |
| 139 | + trylast=bool(opts.get("trylast", False)), |
| 140 | + specname=opts.get("specname"), |
| 141 | + ) |
| 142 | + |
| 143 | + |
| 144 | +def hookspec_config_to_mapping( |
| 145 | + config: HookspecConfiguration, |
| 146 | +) -> dict[str, Any]: |
| 147 | + """Serialize configuration to a legacy mapping (pytest/support only).""" |
| 148 | + return { |
| 149 | + "firstresult": config.firstresult, |
| 150 | + "historic": config.historic, |
| 151 | + "warn_on_impl": config.warn_on_impl, |
| 152 | + "warn_on_impl_args": config.warn_on_impl_args, |
| 153 | + } |
| 154 | + |
| 155 | + |
| 156 | +def hookimpl_config_to_mapping( |
| 157 | + config: HookimplConfiguration, |
| 158 | +) -> dict[str, Any]: |
| 159 | + """Serialize configuration to a legacy mapping (pytest/support only).""" |
| 160 | + return { |
| 161 | + "wrapper": config.wrapper, |
| 162 | + "hookwrapper": config.hookwrapper, |
| 163 | + "optionalhook": config.optionalhook, |
| 164 | + "tryfirst": config.tryfirst, |
| 165 | + "trylast": config.trylast, |
| 166 | + "specname": config.specname, |
| 167 | + } |
0 commit comments