Augmenting third-party packages
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 1.8k
- 派生
- 302
- 平均合并
- 23 小时
- 30 天内合并 PR
- 8
描述
Motivation
Some framework packages (like pytest, polars, xarray, …) have APIs that allow plugin packages to define attributes on their classes/singletons using some registration function, e.g.:
import pytest
def pytest_configure(config: pytest.Config) -> None:
config.addinivalue_line("markers", "mychoice(select, skip): choose which stuff to test")
@pytest.mark.mychoice(x=1) # I want to make this into a type error by defining the signature somewhere
def test_thing(): ...
These plugins should have a way to specify the type of the new attribute. In our example, pytest itself has this definition for the type of the pytest.mark object:
@final
class MarkGenerator:
if TYPE_CHECKING:
skip: _SkipMarkDecorator
skipif: _SkipifMarkDecorator
xfail: _XfailMarkDecorator
parametrize: _ParametrizeMarkDecorator
usefixtures: _UsefixturesMarkDecorator
filterwarnings: _FilterwarningsMarkDecorator
# untyped marks:
def __getattr__(self, name: str) -> MarkDecorator: ...
which allows its own defined marks to be typed:
@pytest.mark.skipif(x=None) # this *is* a type error
def test_something(): ...
A plugin needs to have a way to add a new typed attribute to MarkGenerator.
Design considerations
The current way of shipping types has no good way of having potentially multiple stubs that can be merged into one: even if it’s possible to ship pytest/__init__.pyi in one plugin and have it merged with the actual pytest package, only one plugin could do that, and there would be no indication that this .pyi is intended to be an augmentation instead of a replacement for all of pytest’s types.
So we’d need a new way to locate augmentation stubs, I think.
As for how these stubs look like, I think typing.Protocol could do a good job:
A Protocol in an augmentation stub could be interpreted as an augmentation protocol, e.g.
$PYTHONPATH/my-pytest-plugin/typeshedding-location-for-augments/pytest/__init__.pyi or
$PYTHONPATH/typeshedding-location-for-augments/my-pytest-plugin/pytest/__init__.pyi
from typing import Protocol
import pytest
class MarkGenerator(Protocol):
mychoice: _MyChoiceMarkDecorator
class _MyChoiceMarkDecorator(pytest.MarkDecorator):
def __call__( # type: ignore[override]
self,
select: list[str] = ...,
skip: list[str] = ...,
) -> MarkDecorator: ...
Prior art
- TypeScript has this feature, explained here: https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
- PEP 484 had a location for distributing stubs outside of the actual package, we could bring it back for augments
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
未指定任何 repository 文件或测试。首先审阅 PEP 561、PEP 484 中所引用的 stub-distribution 部分,以及 TypeScript 的 declaration-merging 先例;比较提议的 augmentation 位置和 Protocol 的形状。完成的标准是产出一份关于如何定位、合并和解释第三方 augmentation stub 的一致设计。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- tooling
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 冷清
- 描述清晰度
- 需要澄清
- 新手友好度
- 30/100