Allow a base class to restrict the type of subclass's inner class
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Feature
For regular classvars, attributes and methods, mypy already checks that the types in a subclass match those declared in the superclass. However, this checking does not extend to inner classes in the subclass. It seems inner classes are ignored by mypy entirely.
For example, a framework could define an abstract class Model whose inner class Meta must implement a certain protocol:
# Defined in the framework
class InnerMetaProtocol(Protocol):
example_attr: str
class Model:
Meta: type[InnerMetaProtocol]
# An application developer using framework
class Widget(Model):
class Meta:
example_attr = 123 # This should be an error: int is not str
Class Widget implements Model, whose attribute Meta should conform to type[InnerMetaProtocol], meaning it requires attribute example_attr: str. But currently mypy ignores this constraint: Playground.
It's probably useful to support this with not just protocols, but also concrete classes (e.g. Meta: type[ConcreteClass]).
Pitch
This pattern of inner Meta classes is used quite extensively in the Django ecosystem, including for Django models, forms, Django REST Framework serializers, djangorestframework-dataclasses, etc. I'm sure there are other use cases as well, but I don't know off the top of my head.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
issue の Playground 例から始め、宣言された Model.Meta の型とサブクラスの内部 Meta クラスを比較します。mypy が互換性のない int の例を報告し、指定されたプロトコルおよび具象クラスのケースをサポートすれば、作業は完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100