argparseのテストコードで、メタクラスParserTesterMetaclassの利用
ParserTestCase = ParserTesterMetaclass('ParserTestCase', bases, {})
https://github.com/python/cpython/blob/v3.10.5/Lib/test/test_argparse.py#L269
ParserTestCaseはメタクラスのインスタンス、つまりクラス
ParserTestCaseインスタンス化時の__init__は早期リターン
https://github.com/python/cpython/blob/v3.10.5/Lib/test/test_argparse.py#L172-L173
basesはテストコードで定義したTestCaseだけからなるタプル
このTestCaseはunittest.TestCaseを継承している
ParserTestCaseの基底クラスはこのTestCase
ParserTestCaseクラスを継承して後続のテストを書いている
3つのクラス属性を指定するだけで済んでいる
argument_signatures -- a list of Sig objects which specify the signatures of Argument objects to be created
failures -- a list of args lists that should cause the parser to fail
successes -- a list of (initial_args, options, remaining_args) tuples where initial_args specifies the string args to be parsed, options is a dict that should match the vars() of the options parsed out of initial_args, and remaining_args should be any remaining unparsed arguments
全てのサブクラスでParserTesterMetaclassの__init__が呼び出される
ParserTesterMetaclassの__init__のclsはメタクラス(ParserTesterMetaclass)ではなく、生成されたクラスオブジェクト
ref: メタクラス(エキpy)
ParserTesterMetaclassの__init__の実装読みメモ