Assuming `self` or `cls` parameter is positional-only
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 1.8k
- 派生
- 302
- 平均合并
- 23 小时
- 30 天内合并 PR
- 8
描述
I'm interested in guidance from the typing community relating to a question that came up in a recent pyright bug report.
The issue has to do with the self parameter in an instance method and the cls parameter in a class method — whether a type checker should assume that they are implicitly positional-only parameters. In particular, the discussion was about method declarations within a protocol class.
PEP 570 makes a reference to this topic in its introductory section:
One special case of this situation is the
selfparameter for class methods: it is undesirable that a caller can bind by keyword to the nameselfwhen calling the method from the class.
But PEP 570 doesn't provide any specific guidance about self or cls in its specification section. PEP 544 (which introduced protocols) is also silent on the topic.
Pyright currently assumes that self and cls are not positional-only parameters (unless explicitly followed by a / parameter marker or named with double underscores). This assumption affects protocol matching — and more generally, subtype compatibility evaluation for callables.
@AlexWaygood is proposing that the first parameter of instance and class methods should always be considered position-only parameters.
This raises some questions in my mind:
- Is this a safe assumption? Does anyone know of a situation where
selforclsare used as keyword arguments? - Does this apply only to protocol methods, or does it apply generally to methods in any class?
- Should it be considered a type checker error if someone attempts to use a keyword argument to target the first parameter of an instance or class method? It works at runtime.
class Foo:
def bar(self): pass
Foo.bar(self=Foo())
- Does the
clsexemption apply only to explicit class methods, or does it also apply to__new__and__init_subclass__?
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
先阅读 PEP 570 和 PEP 544,然后查看链接的 Pyright 错误报告和 issue 的讨论。确定对于方法、协议、new 和 init_subclass,self 和 cls 是否应被视为仅限位置参数。达到有文档记录的社区指导或规范决策即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- tooling
- Issue 类型
- 文档
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 20/100