Issue 33190: problem with ABCMeta.prepare when called after types.new_class (original) (raw)

I am pretty sure this is a bug. If not I apologize.

Say I want to dynamically create a new C class, with base class MyABC (and dynamically assigned abstract method m). This works fine if I use type, but if I use new_class, the keyword argument to the m method implementation gets lost somewhere in the call to ABCMeta.__prepare___.

I've attached a file to demo. Thanks.

This is not a bug, but a misunderstanding:

types.new_class('Test', (A, B), {'metaclass': Meta, 'foo': 42})

id equivalent to

class Test(A, B, metaclass=Meta, foo=42): pass

If you want to populate the namespace, then you should use the fourth argument exec_body which should be a callable that takes newly created dictionary and populates it with items. For your case it should be:

C = new_class("C", (MyABC,), {}, exec_body=lambda ns: ns.update(namespace))

If you want to clarify the corresponding docstring, then please open a PR. Otherwise you can close the issue.