Clarification about how to implement namespace packages (as in PEP 420) via import hooks for PEP 660 use case
还没有人认领这个 Issue。
评估
调研方向
首先阅读 Lib/importlib._bootstrap_external.py 中所引用的 namespace package 实现附近的内容,然后将 PEP 420 与链接的 Discourse 讨论和 proof-of-concept gist 进行比较。完成的标准是确定并记录 Solutions A 或 B 是否与规范兼容,以及 import hooks 应如何支持 PEP 660 用例。
由索引模型根据 Issue 内容生成。
描述
This is a follow up of 2 previous discussions in the Python discourse and an issue in pypa/packaging-problems:
- https://discuss.python.org/t/pep-660-and-setuptools/14855/15
- https://discuss.python.org/t/how-to-implement-namespace-packages-as-in-pep-420-via-import-hooks/15192
- https://github.com/pypa/packaging-problems/issues/588
Summary
One of the popular ways of implementing PEP 660 is via import hooks (since other alternatives like symlinking package folders are not available in all platforms).
However the reference implementation editables currently does not support namespaces.
Specifically the dynamic path computation aspect of PEP 420 is a challenge.
After inspecting the implementation for importlib._bootstrap_external, I may have managed to find 2 solutions for this problem, described in the detail in this discourse post and demonstrated as a PoC in this gist:
- Solution A: Create a custom "path class" that tries to emulate the
_NamespacePathbehaviour. - Solution B: Create a custom
PathEntryFinderand add a “bogus” entry tosys.pathjust to trigger it (implicitly relying thatimportlib.machinery.PathFinderwill take care of setting__path__to_NamespacePathobject under the hood).
However, the problem is that I don't know if these approaches are reliable in the long run and in accordance with the language specification.
Open Questions
Questions like the following are still not clear to me:
- Is there a chance that an internal check for namespace packages will fail if
__path__uses a custom class instead of_NamespacePatheven if the behaviour described in PEP 420 is observed? - Can we rely on the fact that the import machinery will automatically set the namespace loader for specs in the form of
ModuleSpec("name", None, is_package=True)? - Is
pkgutil(and particularlypkgutil.extend_path) still considered first class citizen of the standard library or is it better to avoid it when writing new code? - Can we rely on the behaviour of
sys.path_hooks+importlib.machinery.PathFinderto createModuleSpecobjects for namespaces that will automatically perform dynamic path computation? Or is this an internal implementation detail that can change in the future?
If the discussed approaches are not recommended, how can we support namespace packages using import hooks and deliver a complete solution for PEP 660?
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 36k
- 平均合并
- 1 天 9 小时
- 30 天内合并 PR
- 558
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python/cpython 的其他 Issue
-
docs pending
难度 2/5 1-3 小时 新手友好度 78/100
-
stdlib type-feature
难度 2/5 1-3 小时 新手友好度 78/100
-
stdlib type-feature
难度 2/5 1-3 小时 新手友好度 72/100
-
build type-bug
难度 2/5 1-3 小时 新手友好度 76/100
-
stdlib topic-email type-feature
难度 2/5 1-3 小时 新手友好度 70/100
相似的 Issue
-
area/auth bug comp/agent P3 platform/discord type/security
难度 2/5 1-3 小时 新手友好度 88/100
NousResearch/hermes-agent#117848 ·
-
难度 2/5 1-3 小时 新手友好度 74/100
bancolombia/sentinel#23 ·
-
test md 未关闭CI
难度 2/5 1-3 小时 新手友好度 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
难度 2/5 1-3 小时 新手友好度 74/100
langchain-ai/deepagents#6450 ·
-
bug client
难度 2/5 1-3 小时 新手友好度 88/100