python / python/typeshed

Unify typing for `__path__`?

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

还没有人认领这个 Issue。

主要语言
Python
星标
5.1k
派生
2.1k
平均合并
1 天 19 小时
30 天内合并 PR
82

描述

After the last mypy update v0.920, I started to notice an inconsistency with how the module variable __path__ is typed mainly across typeshed, mypy, and pyright.

Type Link
typeshed - ModuleType MutableSequence[str] https://github.com/python/typeshed/pull/6200
typeshed - pkgutil.extend_path list[str] https://github.com/python/typeshed/pull/5222
mypy list[str] https://github.com/python/mypy/pull/9454
pyright Iterable[str] https://github.com/microsoft/pylance-release/issues/1098

In the Python docs it's mentioned as:

`__path__` must be an iterable of strings, but it may be empty.

However the docs for pkgutil.extend_path go on say it's mostly a list (?) cpython/pkgutil.py

    If the input path is not a list (as is the case for frozen
    packages) it is returned unchanged.  The input path is not
    modified; an extended copy is returned.  Items are only appended
    to the copy at the end.

From my point of view, it isn't entirely clear what the "correct" type should be. Some points to consider though:

  • In most cases it seems to be a list[str].
  • It was mentioned here however that it can sometimes also be a MutableSequence[str]
  • The __path__ type should be valid as first argument to pkgutil.extend_path. At the moment that only applies to list[str] but obviously the argument type for extend_path could be changed to, for example, accept a TypeVar bound to Iterable[str] instead.
  • It seems to be fairly common (and safe ?) to access the at least the first item of __path__ via __getitem__.

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先将 typeshed 的 ModuleTypepkgutil.extend_path 注解与 Python 关于模块路径和 pkgutil 的文档进行比较,然后查看链接的 mypy 和 pyright 讨论。确定一个一致的类型契约,以反映文档记载的行为和观察到的行为。当受影响的存根中的相关注解彼此一致,并且仍与 extend_path 兼容时,即表示完成。

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

评估

技术栈
python
领域
tooling
Issue 类型
重构
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
20/100

把新 issue 发到你的邮箱

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