Skip to content

Commit e412a60

Browse files
cleanups
* switch ruff config to extend-select and enable UP rules * make mypy --strict pass for src
1 parent 0ceb558 commit e412a60

File tree

6 files changed

+238
-130
lines changed

6 files changed

+238
-130
lines changed

src/pluggy/_callers.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,22 @@
44

55
from __future__ import annotations
66

7-
from collections.abc import Generator
8-
from collections.abc import Mapping
9-
from collections.abc import Sequence
10-
from typing import cast
11-
from typing import NoReturn
127
import warnings
138

149
from ._hooks import HookImpl
10+
from ._result import _raise_wrapfail
1511
from ._result import HookCallError
1612
from ._result import Result
17-
from ._warnings import PluggyTeardownRaisedWarning
13+
14+
TYPE_CHECKING = False
15+
if TYPE_CHECKING:
16+
from typing import cast
17+
from typing import Generator
18+
from typing import Mapping
19+
from typing import NoReturn
20+
from typing import Sequence
21+
from typing import Tuple
22+
from typing import Union
1823

1924

2025
# Need to distinguish between old- and new-style hook wrappers.

src/pluggy/_hooks.py

Lines changed: 78 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,70 +8,87 @@
88
from collections.abc import Mapping
99
from collections.abc import Sequence
1010
from collections.abc import Set
11-
import inspect
1211
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
2412
import warnings
2513

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
2790

2891

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
7592

7693

7794
@final
@@ -297,6 +314,8 @@ def varnames(func: object) -> tuple[tuple[str, ...], tuple[str, ...]]:
297314
In case of a class, its ``__init__`` method is considered.
298315
For methods the ``self`` parameter is not included.
299316
"""
317+
import inspect
318+
300319
if inspect.isclass(func):
301320
try:
302321
func = func.__init__

src/pluggy/_importlib_metadata.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""this module contains importlib_metadata usage and importing
2+
3+
it's deferred to avoid import-time dependency on importlib_metadata
4+
5+
.. code-block:: console
6+
7+
python -X importtime -c 'import pluggy' 2> import0.log
8+
tuna import0.log
9+
10+
11+
"""
12+
from __future__ import annotations
13+
14+
import importlib.metadata
15+
from typing import Any
16+
17+
from . import _manager
18+
19+
20+
class DistFacade:
21+
"""Emulate a pkg_resources Distribution"""
22+
23+
def __init__(self, dist: importlib.metadata.Distribution) -> None:
24+
self._dist = dist
25+
26+
@property
27+
def project_name(self) -> str:
28+
name: str = self.metadata["name"]
29+
return name
30+
31+
def __getattr__(self, attr: str, default: object | None = None) -> Any:
32+
return getattr(self._dist, attr, default)
33+
34+
def __dir__(self) -> list[str]:
35+
return sorted(dir(self._dist) + ["_dist", "project_name"])
36+
37+
38+
def load_importlib_entrypoints(
39+
manager: _manager.PluginManager,
40+
group: str,
41+
name: str | None = None,
42+
) -> int:
43+
"""Load modules from querying the specified setuptools ``group``.
44+
45+
:param group:
46+
Entry point group to load plugins.
47+
:param name:
48+
If given, loads only plugins with the given ``name``.
49+
50+
:return:
51+
The number of plugins loaded by this call.
52+
"""
53+
count = 0
54+
for dist in list(importlib.metadata.distributions()):
55+
for ep in dist.entry_points:
56+
if (
57+
ep.group != group
58+
or (name is not None and ep.name != name)
59+
# already registered
60+
or manager.get_plugin(ep.name)
61+
or manager.is_blocked(ep.name)
62+
):
63+
continue
64+
plugin = ep.load()
65+
manager.register(plugin, name=ep.name)
66+
manager._plugin_dist_metadata.append((plugin, dist))
67+
count += 1
68+
return count

0 commit comments

Comments
 (0)