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