Skip to content

Commit dded41a

Browse files
committed
🐛 fix(alembic): more robust editable plugin detect
1 parent 2b35f7e commit dded41a

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

nonebot_plugin_orm/utils.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import logging
66
from io import StringIO
77
from pathlib import Path
8+
from functools import wraps
89
from itertools import repeat
910
from contextlib import suppress
1011
from typing import Any, TypeVar
11-
from functools import wraps, lru_cache
1212
from typing_extensions import Annotated
1313
from dataclasses import field, dataclass
1414
from collections.abc import Callable, Iterable
@@ -48,14 +48,6 @@
4848
DependsInner = type(Depends())
4949

5050

51-
class _ReturnEq:
52-
def __eq__(self, __o: _T) -> _T:
53-
return __o
54-
55-
56-
return_eq = _ReturnEq()
57-
58-
5951
class LoguruHandler(logging.Handler):
6052
def emit(self, record: logging.LogRecord):
6153
try:
@@ -226,31 +218,35 @@ def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> Iterable[_T]:
226218
return wrapper
227219

228220

229-
_packages_distributions = lru_cache(None)(packages_distributions)
221+
pkgs = packages_distributions()
230222

231223

232224
def is_editable(plugin: Plugin) -> bool:
233225
"""Check if the distribution is installed in editable mode"""
234226
while plugin.parent_plugin:
235227
plugin = plugin.parent_plugin
236228

229+
path = files(plugin.module)
230+
231+
if not isinstance(path, Path) or "site-packages" in path.parts:
232+
return False
233+
237234
dist: Distribution | None = None
238235

239-
if plugin.metadata:
240-
with suppress(PackageNotFoundError):
241-
dist = distribution(plugin.metadata.name)
242-
243-
# XXX: 有些包有不正确的 top_level.txt (例如: kiwisolver 的 src),
244-
# 导致下面的代码有可能得出错误的结果. 参见: https://github.com/nucleic/kiwi/issues/169
245-
# if not dist:
246-
# with suppress(KeyError, IndexError):
247-
# dist = distribution(
248-
# _packages_distributions()[plugin.module_name.split(".")[0]][0]
249-
# )
250-
251-
if not dist:
252-
path = files(plugin.module)
253-
return isinstance(path, Path) and "site-packages" not in path.parts
236+
with suppress(PackageNotFoundError):
237+
dist = distribution(plugin.name.replace("_", "-"))
238+
239+
if not (dist or plugin.module.__file__ is None):
240+
path = Path(plugin.module.__file__)
241+
for name in pkgs.get(plugin.module_name.split(".")[0], ()):
242+
dist = distribution(name)
243+
if path in map(methodcaller("locate"), dist.files or ()):
244+
break
245+
else:
246+
dist = None
247+
248+
if dist is None:
249+
return True
254250

255251
# https://github.com/pdm-project/pdm/blob/fee1e6bffd7de30315e2134e19f9a6f58e15867c/src/pdm/utils.py#L361-L374
256252
if getattr(dist, "link_file", None) is not None:

0 commit comments

Comments
 (0)