Skip to content

Commit ec48dd7

Browse files
authored
Merge pull request #2405 from dvarrazzo/fix/drop-pkg-resources
Drop use of non-standard pkg_resources API
2 parents 0c8fc82 + be46cd4 commit ec48dd7

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

isort/settings.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646
from .wrap_modes import from_string as wrap_mode_from_string
4747

4848
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+
4954
tomllib: Any
5055
else:
5156
if sys.version_info >= (3, 11):
@@ -356,9 +361,7 @@ def __init__(
356361
profile: Dict[str, Any] = {}
357362
if profile_name:
358363
if profile_name not in profiles:
359-
import pkg_resources
360-
361-
for plugin in pkg_resources.iter_entry_points("isort.profiles"):
364+
for plugin in entry_points(group="isort.profiles"):
362365
profiles.setdefault(plugin.name, plugin.load())
363366

364367
if profile_name not in profiles:
@@ -473,9 +476,7 @@ def __init__(
473476
combined_config["src_paths"] = tuple(src_paths)
474477

475478
if "formatter" in combined_config:
476-
import pkg_resources
477-
478-
for plugin in pkg_resources.iter_entry_points("isort.formatters"):
479+
for plugin in entry_points(group="isort.formatters"):
479480
if plugin.name == combined_config["formatter"]:
480481
combined_config["formatting_function"] = plugin.load()
481482
break
@@ -715,9 +716,7 @@ def sorting_function(self) -> Callable[..., List[str]]:
715716
self._sorting_function = sorted
716717
else:
717718
available_sort_orders = ["natural", "native"]
718-
import pkg_resources
719-
720-
for sort_plugin in pkg_resources.iter_entry_points("isort.sort_function"):
719+
for sort_plugin in entry_points(group="isort.sort_function"):
721720
available_sort_orders.append(sort_plugin.name)
722721
if sort_plugin.name == self.sort_order:
723722
self._sorting_function = sort_plugin.load()
@@ -938,4 +937,17 @@ def _as_bool(value: str) -> bool:
938937
raise ValueError(f"invalid truth value {value}")
939938

940939

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+
941953
DEFAULT_CONFIG = Config()

pyproject.toml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ include = [
3838
]
3939
requires-python = ">=3.9.0"
4040

41-
dependencies = []
41+
dependencies = [
42+
# we only need the entry_points() function, which appeared in the stdlib
43+
# in Python 3.10.
44+
# importlib_metadata 4.6 is the version matching stdlib 3.10
45+
# See grid at https://pypi.org/project/importlib-metadata/
46+
"importlib_metadata >= 4.6.0; python_version < '3.10'",
47+
]
4248

4349
[project.urls]
4450
Homepage = "https://pycqa.github.io/isort/index.html"
@@ -174,6 +180,10 @@ allow_untyped_defs = true
174180
allow_incomplete_defs = true
175181
allow_untyped_calls = true
176182

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

tests/unit/test_regressions.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,11 +1875,7 @@ class Bar:
18751875
def test_isort_should_produce_the_same_code_on_subsequent_runs_issue_1799(tmpdir):
18761876
code = """import sys
18771877
1878-
if sys.version_info[:2] >= (3, 8):
1879-
# TODO: Import directly (no need for conditional) when `python_requires = >= 3.8`
1880-
from importlib.metadata import PackageNotFoundError, version # pragma: no cover
1881-
else:
1882-
from importlib_metadata import PackageNotFoundError, version # pragma: no cover
1878+
from importlib.metadata import PackageNotFoundError, version
18831879
"""
18841880
config_file = tmpdir.join(".isort.cfg")
18851881
config_file.write(

0 commit comments

Comments
 (0)