python / python/cpython

Improvements Related to Namespace Package Portions for PathEntryFinders

未关闭
#92,896 8 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

interpreter-core topic-importlib type-feature
主要语言
Python
星标
77.2k
派生
36k
PR 合并指标
PR 指标待抓取

描述

tl;dr there are some oddities we should consider addressing with namespace package portions coming from path entry finders. It probably isn't worth bothering though (except for the docs fixes).


PEP 420 added (implicit) namespace packages. At that time, and since then, namespace packages (and portions) have gotten special-casing throughout the import machinery. Generally, we treat them like normal modules (or specs) even though certain characteristics don't apply. This isn't a problem generally because namespace packages aren't that common (and because, I expect, only builtin importers ever deal with them). However, there are a few things that it might be worth addressing.

PEP 451 introduced ModuleSpec, etc. PathEntryFinder.find_spec() as the replacement for PathEntryFinder.find_loader(), with some notable differences:

  • find_loader() returns (loader, portion)
    • the interpretation of this value is clearly documented
    • the finder can't provide any other possible information
    • if the returned loader is not None then the portion is ignored (not even a warning is set)
  • find_spec() returns a ModuleSpec with loader set to None and submodule_search_locations set to the portion
    • the interpretation of this value for portions is not clearly documented anywhere
    • the path entry finder may set addition info on the spec (e.g. loader_state) but it is all ignored (not even a warning if set)

In both cases, the language reference is not clear about how to identify namespace package portions from a path entry finder.

What to Do?

  • update the PathEntryFinder.find_spec() docs entry
  • (a weak maybe) add the details to the language reference
  • (maybe) be more explicit when returning a namespace package portion
  • (maybe) strictly disallow (or warn) for extraneous information tied to a portion
explicitly identify namespace package portions
  1. do nothing
  2. use a dedicated singleton instead of None
  3. use a dedicated ModuleSpec subclass (e.g. NamespacePackagePortionSpec)
  4. use a dedicated ModuleSpec-compatible class (e.g. NamespacePackagePortionSpec)
explicitly deal with unexpectedly set data

(This is basically just if spec.origin is set.)

  1. do nothing
  2. emit a warning
  3. fail
explicitly deal with spec.loader_state
  1. do nothing
  2. emit a warning
  3. fail
  4. preserve the info on the spec of the combined namespace package

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 PathEntryFinder.find_spec() 文档条目和 issue 中链接的 language-reference namespace-packages 部分开始。阅读 PEP 420 和 PEP 451 的背景,然后审查用于识别各部分以及处理额外 Spec 数据的未决方案。项目选择的行为得到清晰记录,并且任何相关的行为变更都在实现前达成一致后,即视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
documentation, tooling
Issue 类型
文档
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。