metaclass/__init_subclass__ keyword checking
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Feature
A way for plugins to mark a metaclass as safely passing keyword arguments to __init_subclass__.
Pitch
The current situation
Currently, if a class is inherited from that both has a custom metaclass and __init_subclass__ defined then there is no type checking done for the keyword arguments in __init_subclass__. For example the following is an error:
class Base:
def __init_subclass__(cls, keyword: int = 0): ...
class Derived(Base, keyword='fail'): ...
error: Argument "keyword" to "__init_subclass__" of "Base" has incompatible type "str"; expected "int"
But this is not:
class Meta(type): ...
class Base(metaclass=Meta):
def __init_subclass__(cls, keyword: int = 0): ...
class Derived(Base, keyword='fail'): ...
The specifics around this are in #7723.
My issue
I have a base class that operates similar to dataclass object. I have a plugin that provides the semantics to mypy already. However, this class uses a metaclass and __init_subclass__ and I would like type checking on the __init_subclass__ keyword arguments. The metaclass __new__ does not do anything with the keyword arguments but pass them along, so I know it's safe to do the type checking. I would like for my plugin to be able to tell mypy that this metaclass is safe.
Unfortunately I can't just get rid of the metaclass as it provides a class level __await__.
My current solution is just to hack the TypeInfo._fullname for the metaclass to be builtins.type.
The solutions that come to mind would be either:
- A boolean on
TypeInfothat says that the metaclass is__init_subclass__safe (plugins just need to flip this in one of the existing hooks) - A way for plugins to manipulate the set of safe metaclasses that
Checkeruses (hook to customizeCheckerprior to run?)
I'm happy to do the work if a solution can be found.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、どのメタクラスが安全かを判定する Checker のロジックを読み、次に issue で言及されている TypeInfo と既存の plugin hook を調べます。提案されている TypeInfo フラグと、安全なメタクラスの集合をカスタマイズする hook を比較します。plugin がメタクラスを init_subclass のキーワードを安全に転送するものとしてマークでき、例に型チェックが適用されれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers, devtools
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 30/100