Allow subclassing without supertyping
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 1.8k
- 派生
- 302
- 平均合并
- 23 小时
- 30 天内合并 PR
- 8
描述
This issue comes from python/mypy#1237. I'll try to make a summary of the discussion here
Some one reported an issue with an artificial example (https://github.com/python/mypy/issues/1237#issue-136162181):
class Foo:
def factory(self) -> str:
return 'Hello'
class Bar(Foo):
def factory(self) -> int:
return 10
and then with the following stub files (https://github.com/python/mypy/issues/1237#issuecomment-188710925):
class QPixmap(QPaintDevice):
def swap(self, other: 'QPixmap') -> None: ...
class QBitmap(QPixmap):
def swap(self, other: 'QBitmap') -> None: ...
Which mypy currently reports as erroneous: Argument 1 of "swap" incompatible with supertype "QPixmap"
These were initially was argued to be a correct error because they violate Liskov Substitution Principle (the same case by a change of return type, the second is a covariant argument change). The problem in this scenario is that the actual classes are not meant to be substituted (the first example is actually a wrapper for a non-virtual C++ function so they're not substitutable, and the second aren't supposed to be mixed together, just reuse part of the code). (see https://github.com/python/mypy/issues/1237#issuecomment-189490903)
There was also a suggestion that allow covariant args explicitly (in the same way that Eiffel allows it and adds a runtime check) with a decorator
class QBitmap(QPixmap):
@covariant_args
def swap(self, other: 'QBitmap') -> None: ...
My proposal instead was add what some dialects of Eiffel call "non-conforming inheritance" ("conforms" is the Eiffel word for what PEP-483 calls "is-consistent-with"). It is essentially a mechanism to use subclassing just as a way to get the implementation from the superclass without creating a subtyping relation between them. See https://github.com/python/mypy/issues/1237#issuecomment-231199419 for a detailed explanation.
The proposal is to haven Implementation in typing so you can write:
class QBitmap(Implementation[QPixmap]):
def swap(self, other: 'QBitmap') -> None: ...
which just defines a QBitmap class with all the mothods copied from QPixmap, but without making one a subtype of the other. In runtime we can just make Implementation[QPixmap] == QPixmap
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
先阅读链接的 mypy#1237 讨论,以及其中引用的关于不符合规范的继承的说明。将提议的 Implementation[QPixmap] 行为与现有的子类型和方法重写规则进行比较。只有在语义、运行时行为和类型规则达成一致,并由项目的实现和测试覆盖后,工作才算完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- devtools
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 20/100