API incompatibility with `importlib.metadata` (or at least the API is not type-safe?)
還沒有人認領這個 Issue。
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 35/100
- Issue 類型
- 缺陷
- 描述清晰度
- 基本清楚
- 活躍度
- 停滯
- 技術堆疊
- python
研究方向
首先,使用自訂的 MetaPathFinder 和 issue 中描述的 Python 3.8 環境重現文件中的範例。檢查 Distribution.discover 及相關的 discovery 流程,然後針對支援的基準執行型別檢查器,以驗證第三方的 importlib.metadata 發行套件及其 entry_points 結果是否得到一致處理。
由索引模型根據 Issue 內容生成。
描述
When 3rd-party MetaPathFinder are implemented using importlib.metadata, the return types in importlib_metadata are not respected, which causes the API to behave in an unexpected way (with unexpected errors).
This is an example similar to the one identified in https://github.com/pypa/pyproject-hooks/pull/195#issuecomment-2088695300 and https://github.com/pypa/setuptools/issues/4338:
mkdir -p /tmp/stash/private_path/_test-0.0.1.dist-info
cat <<EOF > /tmp/stash/private_path/_test-0.0.1.dist-info/METADATA
Name: _test
Version: 0.0.1
EOF
cat <<EOF > /tmp/stash/private_path/_test-0.0.1.dist-info/entry_points.txt
[_test.importlib_metadata]
hello = world
EOF
cat <<EOF > /tmp/stash/install_finder.py
import sys
from importlib.machinery import PathFinder
from importlib.metadata import DistributionFinder, MetadataPathFinder
class _ExampleFinder:
def __init__(self, private_path):
self.private_path = private_path
def find_spec(self, fullname, _path, _target=None):
if "." in fullname:
# Rely on importlib to find nested modules based on parent's path
return None
# Ignore other items in _path or sys.path and use private_path instead:
return PathFinder.find_spec(fullname, path=self.private_path)
def find_distributions(self, context=None):
context = DistributionFinder.Context(path=self.private_path)
return MetadataPathFinder.find_distributions(context=context)
sys.meta_path.insert(0, _ExampleFinder(["/tmp/stash/private_path"]))
EOF
cd /tmp/stash/
python3.8 -m venv .venv
.venv/bin/python -m pip install -U importlib-metadata
.venv/bin/python
>>> import install_finder
>>> from importlib_metadata import distribution
>>> distribution("_test").entry_points.select(group="_test.importlib_metadata")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'select'
The expected behaviour as per API documentation would be:
>>> distribution("_test").entry_points.select(group="_test.importlib_metadata")
EntryPoints((EntryPoint(name='hello', value='world', group='_test.importlib_metadata'),))
It seems that the origin of this problem is a little "lie" in the API definition:
Instead of
importlib_metadata.Distribution.discover(...) -> Iterable[importlib_metadata.Distribution]
what actually happens is:
importlib_metadata.Distribution.discover(...) -> Iterable[importlib_metadata.Distribution | importlib.metadata.Distribution]
and that propagates throughout the whole API.
I haven't tested, but there is potential for other internal errors too, if internally importlib_metadata is relying that the objects will have type importlib_metadata.Distribution to call newer APIs.
It is probably worthy to change the return type of importlib_metadata.Distribution.discover(...) to Iterable[importlib_metadata.Distribution | importlib.metadata.Distribution] and then run the type checkers on the lowest Python supported (I suppose Python 3.8), to see if everything is OK.
It also means that consumers of importlib_metadata cannot rely on the newer APIs (unless they are sure that 3r-party packages installed in their environment are not using importlib.metadata).
- 主要語言
- Python
- 星號
- 142
- 分支
- 97
- PR 合併指標
- 30 天內沒有已合併 PR
貢獻指南
這個儲存庫沒有索引到貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
python/importlib_metadata 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 68/100
python/importlib_metadata#535 ·
-
難度 2/5 1-3 小時 新手友好度 75/100
python/importlib_metadata#528 · 4 則留言 ·
-
documentation help wanted
難度 2/5 1-3 小時 新手友好度 68/100
python/importlib_metadata#526 · 6 則留言 ·
-
難度 2/5 1-3 小時 新手友好度 68/100
python/importlib_metadata#525 · 2 則留言 ·
-
難度 5/5 一週以上 新手友好度 35/100
python/importlib_metadata#540 · 1 則留言 ·
查看 python/importlib_metadata 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 75/100
anthropics/skills#1811 · 1 則留言 ·
-
難度 2/5 1-3 小時 新手友好度 75/100
speaches-ai/speaches#678 ·
-
bug
難度 2/5 1-3 小時 新手友好度 75/100
datalayer/mcp-compose#42 ·
-
難度 2/5 1-3 小時 新手友好度 75/100
conda-forge/spacy-feedstock#177 ·
-
難度 2/5 1-3 小時 新手友好度 70/100
UKGovernmentBEIS/inspect_evals#2523 ·