|
3 | 3 | """
|
4 | 4 | from __future__ import annotations
|
5 | 5 |
|
6 |
| -import inspect |
7 | 6 | import sys
|
8 | 7 | import warnings
|
9 |
| -from types import ModuleType |
10 |
| -from typing import AbstractSet |
11 |
| -from typing import Any |
12 |
| -from typing import Callable |
13 |
| -from typing import Final |
14 |
| -from typing import final |
15 |
| -from typing import Generator |
16 |
| -from typing import List |
17 |
| -from typing import Mapping |
18 |
| -from typing import Optional |
19 |
| -from typing import overload |
20 |
| -from typing import Sequence |
21 |
| -from typing import Tuple |
22 |
| -from typing import TYPE_CHECKING |
23 |
| -from typing import TypedDict |
24 |
| -from typing import TypeVar |
25 |
| -from typing import Union |
26 |
| - |
27 |
| -from ._result import Result |
28 |
| - |
29 |
| - |
30 |
| -_T = TypeVar("_T") |
31 |
| -_F = TypeVar("_F", bound=Callable[..., object]) |
32 |
| -_Namespace = Union[ModuleType, type] |
| 8 | + |
33 | 9 | _Plugin = object
|
34 |
| -_HookExec = Callable[ |
35 |
| - [str, Sequence["HookImpl"], Mapping[str, object], bool], |
36 |
| - Union[object, List[object]], |
37 |
| -] |
38 |
| -_HookImplFunction = Callable[..., Union[_T, Generator[None, Result[_T], None]]] |
39 |
| - |
40 |
| - |
41 |
| -class HookspecOpts(TypedDict): |
42 |
| - """Options for a hook specification.""" |
43 |
| - |
44 |
| - #: Whether the hook is :ref:`first result only <firstresult>`. |
45 |
| - firstresult: bool |
46 |
| - #: Whether the hook is :ref:`historic <historic>`. |
47 |
| - historic: bool |
48 |
| - #: Whether the hook :ref:`warns when implemented <warn_on_impl>`. |
49 |
| - warn_on_impl: Warning | None |
50 |
| - |
51 |
| - |
52 |
| -class HookimplOpts(TypedDict): |
53 |
| - """Options for a hook implementation.""" |
54 |
| - |
55 |
| - #: Whether the hook implementation is a :ref:`wrapper <hookwrapper>`. |
56 |
| - wrapper: bool |
57 |
| - #: Whether the hook implementation is an :ref:`old-style wrapper |
58 |
| - #: <old_style_hookwrappers>`. |
59 |
| - hookwrapper: bool |
60 |
| - #: Whether validation against a hook specification is :ref:`optional |
61 |
| - #: <optionalhook>`. |
62 |
| - optionalhook: bool |
63 |
| - #: Whether to try to order this hook implementation :ref:`first |
64 |
| - #: <callorder>`. |
65 |
| - tryfirst: bool |
66 |
| - #: Whether to try to order this hook implementation :ref:`last |
67 |
| - #: <callorder>`. |
68 |
| - trylast: bool |
69 |
| - #: The name of the hook specification to match, see :ref:`specname`. |
70 |
| - specname: str | None |
| 10 | + |
| 11 | +TYPE_CHECKING = False |
| 12 | +if TYPE_CHECKING: |
| 13 | + from types import ModuleType |
| 14 | + from typing import AbstractSet |
| 15 | + from typing import Any |
| 16 | + from typing import Callable |
| 17 | + from typing import Final |
| 18 | + from typing import final |
| 19 | + from typing import Generator |
| 20 | + from typing import List |
| 21 | + from typing import Mapping |
| 22 | + from typing import Optional |
| 23 | + from typing import overload |
| 24 | + from typing import Sequence |
| 25 | + from typing import Tuple |
| 26 | + from typing import TYPE_CHECKING |
| 27 | + from typing import TypedDict |
| 28 | + from typing import TypeVar |
| 29 | + from typing import Union |
| 30 | + |
| 31 | + from ._result import Result |
| 32 | + |
| 33 | + |
| 34 | + _T = TypeVar("_T") |
| 35 | + _F = TypeVar("_F", bound=Callable[..., object]) |
| 36 | + _Namespace = Union[ModuleType, type] |
| 37 | + _HookExec = Callable[ |
| 38 | + [str, Sequence["HookImpl"], Mapping[str, object], bool], |
| 39 | + Union[object, List[object]], |
| 40 | + ] |
| 41 | + _HookImplFunction = Callable[..., Union[_T, Generator[None, Result[_T], None]]] |
| 42 | + _CallHistory = List[Tuple[Mapping[str, object], Optional[Callable[[Any], None]]]] |
| 43 | + |
| 44 | + |
| 45 | + class HookspecOpts(TypedDict): |
| 46 | + """Options for a hook specification.""" |
| 47 | + |
| 48 | + #: Whether the hook is :ref:`first result only <firstresult>`. |
| 49 | + firstresult: bool |
| 50 | + #: Whether the hook is :ref:`historic <historic>`. |
| 51 | + historic: bool |
| 52 | + #: Whether the hook :ref:`warns when implemented <warn_on_impl>`. |
| 53 | + warn_on_impl: Warning | None |
| 54 | + |
| 55 | + |
| 56 | + class HookimplOpts(TypedDict): |
| 57 | + """Options for a hook implementation.""" |
| 58 | + |
| 59 | + #: Whether the hook implementation is a :ref:`wrapper <hookwrapper>`. |
| 60 | + wrapper: bool |
| 61 | + #: Whether the hook implementation is an :ref:`old-style wrapper |
| 62 | + #: <old_style_hookwrappers>`. |
| 63 | + hookwrapper: bool |
| 64 | + #: Whether validation against a hook specification is :ref:`optional |
| 65 | + #: <optionalhook>`. |
| 66 | + optionalhook: bool |
| 67 | + #: Whether to try to order this hook implementation :ref:`first |
| 68 | + #: <callorder>`. |
| 69 | + tryfirst: bool |
| 70 | + #: Whether to try to order this hook implementation :ref:`last |
| 71 | + #: <callorder>`. |
| 72 | + trylast: bool |
| 73 | + #: The name of the hook specification to match, see :ref:`specname`. |
| 74 | + specname: str | None |
| 75 | + |
| 76 | +else: |
| 77 | + def final(func: _F) -> _F: |
| 78 | + return func |
| 79 | + overload = final |
| 80 | + |
| 81 | + |
71 | 82 |
|
72 | 83 |
|
73 | 84 | @final
|
@@ -286,6 +297,8 @@ def varnames(func: object) -> tuple[tuple[str, ...], tuple[str, ...]]:
|
286 | 297 | In case of a class, its ``__init__`` method is considered.
|
287 | 298 | For methods the ``self`` parameter is not included.
|
288 | 299 | """
|
| 300 | + import inspect |
| 301 | + |
289 | 302 | if inspect.isclass(func):
|
290 | 303 | try:
|
291 | 304 | func = func.__init__
|
@@ -364,7 +377,6 @@ def __getattr__(self, name: str) -> HookCaller:
|
364 | 377 | _HookRelay = HookRelay
|
365 | 378 |
|
366 | 379 |
|
367 |
| -_CallHistory = List[Tuple[Mapping[str, object], Optional[Callable[[Any], None]]]] |
368 | 380 |
|
369 | 381 |
|
370 | 382 | class HookCaller:
|
|
0 commit comments