bpo-28869: Set class module to caller module in ABCMeta.__new__#14126
bpo-28869: Set class module to caller module in ABCMeta.__new__#14126alegonz wants to merge 4 commits into
Conversation
…__new__. This is to make dynamically-defined classes pickleable.
… be removed in a future commit.
|
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA). Our records indicate we have not received your CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. If you have recently signed the CLA, please wait at least one business day You can check yourself to see if the CLA has been received. Thanks again for your contribution, we look forward to reviewing it! |
Sorry, something went wrong.
|
This PR is stale because it has been open for 30 days with no activity. |
Sorry, something went wrong.
Hello, this is a PR to the bug reported in bpo-28869 (https://bugs.python.org/issue28869).
Issues:
__module__attribute is set differently depending on whether a metaclass is explicitly called or invoked in a class statement.ABCMetadirectly, because it attempts to look for the class inabcrather than the module the class was defined in.Solution:
If the
__module__variable is not found in the namespace variable, inspect and use the module of the caller. This follows the same pattern used innamedtuple. Values set via__init_subclass__take precedence.There were concerns that inspection might slow down further ABCs creation, so I also did some benchmarking and got these results:
Notes:
timeit.repeatwith 50,000 repetitions.ABCMeta('ABC', (), {'__module__': __name__})as a proxy for the original implementation.Is 3~5% considered a significant drop in performance?
I temporarily commited the script for benchmarking in the root directory for sharing purposes. This will be removed in a future commit.
Also, following the discussion in the bpo thread regarding the pickle documentation, since this issue is not limited to
ABCMeta, I propose to add the following paragraph (highlighted in bold) in the "What can be pickled and unpickled?" section:Would this be an appropriate explanation?
https://bugs.python.org/issue28869