You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
problem with ABCMeta.__prepare__ when called after types.new_class #77371
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2018-03-30.20:43:02.783>labels= ['3.7', '3.8', 'type-bug', 'library']
title='problem with ABCMeta.__prepare__ when called after types.new_class'updated_at=<Date2018-03-31.02:58:22.949>user='https://github.com/Ricyteach'
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___.
First, ABCMeta doesn't have __prepare__, it is just type.__prepare__
Second, the third argument to types.new_class is called kwds for a reason. It is not a namespace like in type but the set of keywords in the equivalent class definition. For example:
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:
Activity
Ricyteach commentedon Mar 30, 2018
I am pretty sure this is a bug. If not I apologize.
Say I want to dynamically create a new
Cclass, with base classMyABC(and dynamically assigned abstract methodm). This works fine if I usetype, but if I usenew_class, the keyword argument to themmethod implementation gets lost somewhere in the call toABCMeta.__prepare___.I've attached a file to demo. Thanks.
ilevkivskyi commentedon Mar 30, 2018
This is not a bug, but a misunderstanding:
__prepare__, it is justtype.__prepare__types.new_classis calledkwdsfor a reason. It is not a namespace like intypebut the set of keywords in the equivalent class definition. For example:types.new_class('Test', (A, B), {'metaclass': Meta, 'foo': 42})
id equivalent to
If you want to populate the namespace, then you should use the fourth argument
exec_bodywhich should be a callable that takes newly created dictionary and populates it with items. For your case it should be:If you want to clarify the corresponding docstring, then please open a PR. Otherwise you can close the issue.
Ricyteach commentedon Mar 30, 2018
Excellent; thank you very much for the explanation.
I have never done a PR on a project this size but I'll give it a try.
ilevkivskyi commentedon Mar 30, 2018
https://devguide.python.org/ will help you.
Ricyteach commentedon Mar 31, 2018
Thank you; I gave it a go. Hopefully didn't cause too much additional work for someone.
furkanonder commentedon May 13, 2023
@ilevkivskyi I think we can close the problem because the issue is not a bug, it is a misunderstanding.
ilevkivskyi commentedon May 13, 2023
There is still an option to improve the docstring for
new_class()but if no-one wants to do this, then we can close.