Skip to content

Commit 59e0ca3

Browse files
committed
Defer loading of importtlib.metadata
This package is needed for `load_setuptools_entrypoints`, but it is quite slow to import. While most users do use `load_setuptools_entrypoints`, maybe some don't, so let's let them avoid the cost. For `import pluggy`: Before: 36ms After: 11ms
1 parent e474438 commit 59e0ca3

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/pluggy/_manager.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import importlib.metadata
43
import inspect
54
import types
65
import warnings
@@ -11,6 +10,7 @@
1110
from typing import Iterable
1211
from typing import Mapping
1312
from typing import Sequence
13+
from typing import TYPE_CHECKING
1414

1515
from . import _tracing
1616
from ._callers import _multicall
@@ -26,6 +26,10 @@
2626
from ._hooks import normalize_hookimpl_opts
2727
from ._result import Result
2828

29+
if TYPE_CHECKING:
30+
# importtlib.metadata import is slow, defer it.
31+
import importlib.metadata
32+
2933

3034
_BeforeTrace = Callable[[str, Sequence[HookImpl], Mapping[str, Any]], None]
3135
_AfterTrace = Callable[[Result[Any], str, Sequence[HookImpl], Mapping[str, Any]], None]
@@ -384,6 +388,8 @@ def load_setuptools_entrypoints(self, group: str, name: str | None = None) -> in
384388
:return:
385389
The number of plugins loaded by this call.
386390
"""
391+
import importlib.metadata
392+
387393
count = 0
388394
for dist in list(importlib.metadata.distributions()):
389395
for ep in dist.entry_points:

0 commit comments

Comments
 (0)