Skip to content

Commit be46cd4

Browse files
committed
refactor: make importlib metadata package import lazy
No reason I can discern for it, just a matter of traditions I guess. #2405 (comment)
1 parent 18ecd0c commit be46cd4

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

isort/settings.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@
4545
from .wrap_modes import WrapModes
4646
from .wrap_modes import from_string as wrap_mode_from_string
4747

48-
if sys.version_info < (3, 10): # pragma: no cover
49-
from importlib_metadata import entry_points
50-
else:
51-
from importlib.metadata import entry_points
52-
5348
if TYPE_CHECKING:
49+
if sys.version_info < (3, 10): # pragma: no cover
50+
EntryPoints = Any
51+
else:
52+
from importlib.metadata import EntryPoints
53+
5454
tomllib: Any
5555
else:
5656
if sys.version_info >= (3, 11):
@@ -937,4 +937,17 @@ def _as_bool(value: str) -> bool:
937937
raise ValueError(f"invalid truth value {value}")
938938

939939

940+
def entry_points(group: str) -> "EntryPoints":
941+
"""Call entry_point after lazy loading it.
942+
943+
TODO: The reason for lazy loading here are unknown.
944+
"""
945+
if sys.version_info < (3, 10): # pragma: no cover
946+
from importlib_metadata import entry_points
947+
else:
948+
from importlib.metadata import entry_points
949+
950+
return entry_points(group=group)
951+
952+
940953
DEFAULT_CONFIG = Config()

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ allow_untyped_defs = true
180180
allow_incomplete_defs = true
181181
allow_untyped_calls = true
182182

183+
[[tool.mypy.overrides]]
184+
module = "importlib_metadata.*"
185+
ignore_missing_imports = true
186+
183187
[tool.ruff]
184188
line-length = 100
185189
lint.select = [

0 commit comments

Comments
 (0)