Skip to content

Commit b56eaee

Browse files
committed
do not use pkg_resources for entrypoints, towards fixing #42
1 parent 8b9c52d commit b56eaee

File tree

6 files changed

+50
-21
lines changed

6 files changed

+50
-21
lines changed

pyproject.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,12 @@ dependencies = []
2525
dynamic = ["readme"]
2626

2727
[project.optional-dependencies]
28-
mypy = [
29-
"types-setuptools",
30-
"types-pkg-resources",
31-
]
28+
mypy = []
3229
test = [
3330
"pytest",
3431
"pytest-cov",
3532
"pytest-mock",
3633
"httpretty",
37-
"types-setuptools",
3834
]
3935

4036
[project.urls]

src/mxdev/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def __init__(
6868
if line:
6969
self.ignore_keys.append(line)
7070

71-
def is_ns_member(name):
71+
def is_ns_member(name) -> bool:
7272
for hook in hooks:
7373
if name.startswith(hook.namespace):
7474
return True

src/mxdev/entry_points.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# this is a helper to load entrypoints with importlib, since pkg_resources
2+
# is deprecated. In Python 3.12 an API incompatible change was introduced,
3+
# so this code is that ugly now.
4+
from importlib.metadata import entry_points
5+
6+
7+
try:
8+
# do we have Python 3.12+?
9+
from importlib.metadata import EntryPoints # type: ignore # noqa: F401
10+
11+
HAS_IMPORTLIB_ENTRYPOINTS = True
12+
except ImportError:
13+
HAS_IMPORTLIB_ENTRYPOINTS = False
14+
15+
16+
def load_eps_by_group(group: str) -> list:
17+
if HAS_IMPORTLIB_ENTRYPOINTS:
18+
eps = entry_points(group=group) # type: ignore
19+
else:
20+
eps_base = entry_points()
21+
if group not in eps_base:
22+
return []
23+
eps = eps_base[group]
24+
# XXX: for some reasons entry points are loaded twice. not sure if this
25+
# is a glitch when installing with uv or something related to
26+
# importlib.metadata.entry_points
27+
return list(eps) # type: ignore

src/mxdev/hooks.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
from .entry_points import load_eps_by_group
12
from .state import State
2-
from pkg_resources import iter_entry_points
33

44
import typing
55

66

7+
try:
8+
# do we have Python 3.12+
9+
from importlib.metadata import EntryPoints # type: ignore # noqa: F401
10+
11+
HAS_IMPORTLIB_ENTRYPOINTS = True
12+
except ImportError:
13+
HAS_IMPORTLIB_ENTRYPOINTS = False
14+
15+
716
class Hook:
817
"""Entry point for hooking into mxdev."""
918

@@ -18,11 +27,12 @@ def write(self, state: State) -> None:
1827

1928

2029
def load_hooks() -> list:
21-
return [ep.load()() for ep in iter_entry_points("mxdev") if ep.name == "hook"]
30+
return [ep.load()() for ep in load_eps_by_group("mxdev") if ep.name == "hook"]
2231

2332

2433
def read_hooks(state: State, hooks: typing.List[Hook]) -> None:
2534
for hook in hooks:
35+
breakpoint()
2636
hook.read(state)
2737

2838

src/mxdev/tests/test_common.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ def test_WorkingCopies_process(mocker, caplog):
143143
def test_WorkingCopies_checkout(mocker, caplog, tmpdir):
144144
caplog.set_level(logging.INFO)
145145

146-
class SysExit(Exception):
147-
...
146+
class SysExit(Exception): ...
148147

149148
class Exit:
150149
def __call__(self, code):

src/mxdev/vcs/common.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
from ..entry_points import load_eps_by_group
2+
13
import abc
24
import logging
35
import os
4-
import pkg_resources
56
import platform
67
import queue
78
import re
@@ -93,20 +94,16 @@ def should_update(self, **kwargs) -> bool:
9394
return update
9495

9596
@abc.abstractmethod
96-
def checkout(self, **kwargs) -> typing.Union[str, None]:
97-
...
97+
def checkout(self, **kwargs) -> typing.Union[str, None]: ...
9898

9999
@abc.abstractmethod
100-
def status(self, **kwargs) -> typing.Union[typing.Tuple[str, str], str]:
101-
...
100+
def status(self, **kwargs) -> typing.Union[typing.Tuple[str, str], str]: ...
102101

103102
@abc.abstractmethod
104-
def matches(self) -> bool:
105-
...
103+
def matches(self) -> bool: ...
106104

107105
@abc.abstractmethod
108-
def update(self, **kwargs) -> typing.Union[str, None]:
109-
...
106+
def update(self, **kwargs) -> typing.Union[str, None]: ...
110107

111108

112109
def yesno(
@@ -151,12 +148,12 @@ def get_workingcopytypes() -> typing.Dict[str, typing.Type[BaseWorkingCopy]]:
151148
return _workingcopytypes
152149
group = "mxdev.workingcopytypes"
153150
addons = {}
154-
for entrypoint in pkg_resources.iter_entry_points(group=group):
151+
for entrypoint in load_eps_by_group(group):
155152
key = entrypoint.name
156153
workingcopytype = entrypoint.load()
157154
if not entrypoint.dist:
158155
continue
159-
if entrypoint.dist.project_name == "mxdev":
156+
if entrypoint.dist.name == "mxdev":
160157
_workingcopytypes[key] = workingcopytype
161158
continue
162159
if key in addons:

0 commit comments

Comments
 (0)