Allow subclassing without supertyping
まだ誰も着手していません。
評価
調査の方向性
まず、リンクされている mypy#1237 の議論と、そこで参照されている非準拠継承の説明を読んでください。提案されている Implementation[QPixmap] の動作を、既存のサブタイプおよびメソッドオーバーライドのルールと比較してください。セマンティクス、ランタイム動作、型付けルールについて合意され、プロジェクトの実装とテストでカバーされて初めて、作業は完了です。
索引モデルが issue の本文から書いたものです。
説明
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
- 主要言語
- Python
- スター
- 1.8k
- フォーク
- 302
- 平均マージ
- 23時間
- マージ済み PR(30日)
- 8
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python/typing のほかの issue
-
topic: typing spec
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
topic: typing spec
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
topic: documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
topic: documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
-
topic: conformance tests topic: typing spec
難易度 3/5 1〜2日 初心者へのやさしさ 72/100
似ている issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
zostera/django-bootstrap4#894 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
use-agent-os/agent-os#3276 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
NousResearch/hermes-agent#117848 ·