python / python/cpython

Semantic and documentation of Interpreter.prepare_main()

未关闭
#137,639 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

docs
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Interpreter.prepare_main() is documented as:

   .. method:: prepare_main(ns=None, **kwargs)
      
      Bind objects in the interpreter's :mod:`!__main__` module.
   
      Some objects are actually shared and some are copied efficiently,
      but most are copied via :mod:`pickle`.  See :ref:`interp-object-sharing`.

But it is not clear what ns is, and how kwargs is related. A module and a class are nemspaces, so can you pass them? Or the SimpleNamespace instance? No. Looking at the code, ns can only be None, a mapping or an iterable of pairs. Is it what was intended? Than it should be documented.

But accepting an iterable of pairs may be not intended. This can hardly be called a namespace. Then the code should be changed from

ns = dict(ns, **kwargs) if ns is not None else kwargs

to

ns = {**ns, **kwargs} if ns is not None else kwargs

And was it necessary to accept None? This is error-prone, as you can accidentally get None instead of a valid value (missing return or missing initialization). {} could be used as a default value, it is fine if it is not mutated. If exclude None, it can be simply written as:

ns = {**ns, **kwargs}

And I suggest to to change the name of the parameter, as it is not a general namespace. "dict" or "mapping" would better reflect its type. "updates" or "changes" could also be used, but then we should also rename "kwargs" to "updates2" or "changes2". Since this is positional-only parameter, changing the name does not break user code.

cc @ericsnowcurrently

贡献指南

打开贡献指南

从这里开始

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

调研方向

从 Interpreter.prepare_main() 的文档及其实现入手,尤其要弄清 ns 和 kwargs 是如何组合的。确定预期接受的输入类型,以及是否应更改参数名称或行为,然后更新文档或相关代码,并添加或调整相关测试,使 API 语义明确。

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

评估

技术栈
python
领域
documentation
Issue 类型
文档
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

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