|
8 | 8 | from collections.abc import Mapping
|
9 | 9 | from collections.abc import Sequence
|
10 | 10 | from collections.abc import Set
|
11 |
| -import inspect |
12 | 11 | import sys
|
13 |
| -from types import ModuleType |
14 |
| -from typing import Any |
15 |
| -from typing import Callable |
16 |
| -from typing import Final |
17 |
| -from typing import final |
18 |
| -from typing import Optional |
19 |
| -from typing import overload |
20 |
| -from typing import TYPE_CHECKING |
21 |
| -from typing import TypedDict |
22 |
| -from typing import TypeVar |
23 |
| -from typing import Union |
24 | 12 | import warnings
|
25 | 13 |
|
26 |
| -from ._result import Result |
| 14 | +_Plugin = object |
| 15 | + |
| 16 | +TYPE_CHECKING = False |
| 17 | +if TYPE_CHECKING: |
| 18 | + from types import ModuleType |
| 19 | + from typing import AbstractSet |
| 20 | + from typing import Any |
| 21 | + from typing import Callable |
| 22 | + from typing import Final |
| 23 | + from typing import final |
| 24 | + from typing import Generator |
| 25 | + from typing import List |
| 26 | + from typing import Mapping |
| 27 | + from typing import Optional |
| 28 | + from typing import overload |
| 29 | + from typing import Sequence |
| 30 | + from typing import Tuple |
| 31 | + from typing import TYPE_CHECKING |
| 32 | + from typing import TypedDict |
| 33 | + from typing import TypeVar |
| 34 | + from typing import Union |
| 35 | + |
| 36 | + from ._result import Result |
| 37 | + |
| 38 | + |
| 39 | + _T = TypeVar("_T") |
| 40 | + _F = TypeVar("_F", bound=Callable[..., object]) |
| 41 | + _Namespace = Union[ModuleType, type] |
| 42 | + _HookExec = Callable[ |
| 43 | + [str, Sequence["HookImpl"], Mapping[str, object], bool], |
| 44 | + Union[object, list[object]], |
| 45 | + ] |
| 46 | + _HookImplFunction = Callable[..., Union[_T, Generator[None, Result[_T], None]]] |
| 47 | + _CallHistory = List[Tuple[Mapping[str, object], Optional[Callable[[Any], None]]]] |
| 48 | + |
| 49 | + |
| 50 | + class HookspecOpts(TypedDict): |
| 51 | + """Options for a hook specification.""" |
| 52 | + |
| 53 | + #: Whether the hook is :ref:`first result only <firstresult>`. |
| 54 | + firstresult: bool |
| 55 | + #: Whether the hook is :ref:`historic <historic>`. |
| 56 | + historic: bool |
| 57 | + #: Whether the hook :ref:`warns when implemented <warn_on_impl>`. |
| 58 | + warn_on_impl: Warning | None |
| 59 | + #: Whether the hook warns when :ref:`certain arguments are requested |
| 60 | + #: <warn_on_impl>`. |
| 61 | + #: |
| 62 | + #: .. versionadded:: 1.5 |
| 63 | + warn_on_impl_args: Mapping[str, Warning] | None |
| 64 | + |
| 65 | + |
| 66 | + class HookimplOpts(TypedDict): |
| 67 | + """Options for a hook implementation.""" |
| 68 | + |
| 69 | + #: Whether the hook implementation is a :ref:`wrapper <hookwrapper>`. |
| 70 | + wrapper: bool |
| 71 | + #: Whether the hook implementation is an :ref:`old-style wrapper |
| 72 | + #: <old_style_hookwrappers>`. |
| 73 | + hookwrapper: bool |
| 74 | + #: Whether validation against a hook specification is :ref:`optional |
| 75 | + #: <optionalhook>`. |
| 76 | + optionalhook: bool |
| 77 | + #: Whether to try to order this hook implementation :ref:`first |
| 78 | + #: <callorder>`. |
| 79 | + tryfirst: bool |
| 80 | + #: Whether to try to order this hook implementation :ref:`last |
| 81 | + #: <callorder>`. |
| 82 | + trylast: bool |
| 83 | + #: The name of the hook specification to match, see :ref:`specname`. |
| 84 | + specname: str | None |
| 85 | + |
| 86 | +else: |
| 87 | + def final(func: _F) -> _F: |
| 88 | + return func |
| 89 | + overload = final |
27 | 90 |
|
28 | 91 |
|
29 |
| -_T = TypeVar("_T") |
30 |
| -_F = TypeVar("_F", bound=Callable[..., object]) |
31 |
| -_Namespace = Union[ModuleType, type] |
32 |
| -_Plugin = object |
33 |
| -_HookExec = Callable[ |
34 |
| - [str, Sequence["HookImpl"], Mapping[str, object], bool], |
35 |
| - Union[object, list[object]], |
36 |
| -] |
37 |
| -_HookImplFunction = Callable[..., Union[_T, Generator[None, Result[_T], None]]] |
38 |
| - |
39 |
| - |
40 |
| -class HookspecOpts(TypedDict): |
41 |
| - """Options for a hook specification.""" |
42 |
| - |
43 |
| - #: Whether the hook is :ref:`first result only <firstresult>`. |
44 |
| - firstresult: bool |
45 |
| - #: Whether the hook is :ref:`historic <historic>`. |
46 |
| - historic: bool |
47 |
| - #: Whether the hook :ref:`warns when implemented <warn_on_impl>`. |
48 |
| - warn_on_impl: Warning | None |
49 |
| - #: Whether the hook warns when :ref:`certain arguments are requested |
50 |
| - #: <warn_on_impl>`. |
51 |
| - #: |
52 |
| - #: .. versionadded:: 1.5 |
53 |
| - warn_on_impl_args: Mapping[str, 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 | 92 |
|
76 | 93 |
|
77 | 94 | @final
|
@@ -297,6 +314,8 @@ def varnames(func: object) -> tuple[tuple[str, ...], tuple[str, ...]]:
|
297 | 314 | In case of a class, its ``__init__`` method is considered.
|
298 | 315 | For methods the ``self`` parameter is not included.
|
299 | 316 | """
|
| 317 | + import inspect |
| 318 | + |
300 | 319 | if inspect.isclass(func):
|
301 | 320 | try:
|
302 | 321 | func = func.__init__
|
|
0 commit comments