(🎁) Add a `StubOnlyBase` decorator for types that don't actually extend things, but do in the stubs (like `Generic`)
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 1.8k
- 派生
- 302
- 平均合并
- 23 小时
- 30 天内合并 PR
- 8
描述
There are existing classes that extend things in an abstract way, like ABCs, and things that are Generic in theory but not in reality, for example all the collections (the collection classes are not Generics, they define __class_getitem__) and some are still not subscribe-able but state they are in the stubs, for instance _collections_abc.dict_keys or any of the collections pre 3.9.
It could be used something like:
# _collections_abc.pyi
@StubOnlyBase(KeysView[_KT_co], Generic[_KT_co, _VT_co])
class dict_keys:
...
# typing.pyi
@StubOnlyBase(Generic[_T])
class Container:
if sys.version_info >= (3, 9):
def __class_getitem__(???) -> ???:
@StubOnlyBase(Collection[_KT], Generic[_KT, _VT_co])
class Mapping(Collection):
...
Mapping doesn't actually extend Generic, but Container defines __class_getitem__, so that would also need to be updated in the stubs.
This might help resolve https://github.com/python/mypy/issues/3186, but still indicate that it's not actually one of the bases.
# builtins.pyi
@StubOnlyBase(numbers.Integral)
class int:
...
I think a change like this would make the stubs much more understandable and closer to the actual implementation, also fixing issues about older python versions not working properly with the current stubs(https://github.com/python/mypy/issues/11529).
I personally find it very confusing when referencing the stubs that the bases are often false.
No idea how this would be used in a real life annotation, but a workaround that works is:
from typing import _alias # type: ignore[attr-defined]
from typing import TYPE_CHECKING
from _collections_abc import dict_keys
if not TYPE_CHECKING:
dict_keys = _alias(dict_keys, 2)
def foo(dk: dict_keys[str, int]) -> None: ...
https://github.com/python/typeshed/pull/6312#issuecomment-976036379
https://github.com/python/typeshed/issues/6257
Obligatory shill to #953, which I think would render this moot.
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先审阅提议的 StubOnlyBase 用法和链接的讨论,然后检查指定的 _collections_abc.pyi、typing.pyi 和 builtins.pyi 存根。在提出如何处理示例和相关 issue 之前,确定装饰器的预期语义及其版本行为;只有在就设计及其对受影响存根的影响达成一致后,才算完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- devtools
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100