Unify typing for `__path__`?
未关闭
还没有人认领这个 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 topkgutil.extend_path. At the moment that only applies tolist[str]but obviously the argument type forextend_pathcould be changed to, for example, accept a TypeVar bound toIterable[str]instead. - It seems to be fairly common (and safe ?) to access the at least the first item of
__path__via__getitem__.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先将 typeshed 的 ModuleType 和 pkgutil.extend_path 注解与 Python 关于模块路径和 pkgutil 的文档进行比较,然后查看链接的 mypy 和 pyright 讨论。确定一个一致的类型契约,以反映文档记载的行为和观察到的行为。当受影响的存根中的相关注解彼此一致,并且仍与 extend_path 兼容时,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- tooling
- Issue 类型
- 重构
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 20/100