-
-
Notifications
You must be signed in to change notification settings - Fork 32k
bpo-28869: Set class module to caller module in ABCMeta.__new__ #14126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
bpo-28869: Set class module to caller module in ABCMeta.__new__ #14126
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! |
@the-knights-who-say-ni |
@@ -0,0 +1,56 @@ | |||
import csv |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file was temporarily added for sharing purposes. It will removed in a future commit.
TestLegacyAPI_Py, TestABC_Py, TestABCWithInitSubclass_Py = test_factory(abc.ABCMeta, | ||
abc.get_cache_token) | ||
abc.get_cache_token, | ||
ABCToPickle_C) | ||
TestLegacyAPI_C, TestABC_C, TestABCWithInitSubclass_C = test_factory(_py_abc.ABCMeta, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that the variables prefix do not match the module passed to the factory (they are flipped), is this a typo?
I would propose to close this in favor of #14166. |
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.ABCMeta
directly, because it attempts to look for the class inabc
rather 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.repeat
with 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