[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2019-10-13 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: PR 14166 now fixes the issue in more generic way. It skips one additional frame when type.__new__ is called not directly from type.__call__. -- ___ Python tracker

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2019-10-09 Thread hongweipeng
hongweipeng added the comment: I think we can refer to typing.py, it does not have this issue. >>> from typing import NamedTuple >>> A = NamedTuple('A', [('name', str), ('id', int)]) >>> class B(NamedTuple): ... name: str ... id: int ... >>> A.__module__ '__main__' >>> B.__module__

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2019-10-07 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: There is a simpler way to fix this issue. Instead of hacking __new__, the inheritance registry can be set up in ABCMeta.__init__. There are no reasons of making this in __new__. -- versions: +Python 3.7, Python 3.8, Python 3.9 -Python 3.4, Python

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2019-10-07 Thread Serhiy Storchaka
Change by Serhiy Storchaka : -- pull_requests: +16202 pull_request: https://github.com/python/cpython/pull/16613 ___ Python tracker ___

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2019-07-22 Thread Alejandro Gonzalez
Alejandro Gonzalez added the comment: I see, that’s an interesting point. In that case, Serhiy’s approach is indeed better. PR 14166 seems stalled. I’d like to help if there’s anything left to do, if possible :) -- ___ Python tracker

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2019-07-21 Thread Ivan Levkivskyi
Ivan Levkivskyi added the comment: FWIW I like Serhiy's approach more. I have never seen a single metaclass overriding type.__call__, while overriding type.__new__ is a common practice. -- ___ Python tracker

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2019-07-20 Thread Alejandro Gonzalez
Alejandro Gonzalez added the comment: Hello, Could anyone take a look at the PR 14126 submitted? Sorry for the trouble and thank you in advance. -- ___ Python tracker ___

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2019-06-17 Thread Alejandro Gonzalez
Alejandro Gonzalez added the comment: >It can be fixed in general if deduce __module__ in type.__call__() instead of >type.__new__(). But wouldn't we have the same problem if a metaclass overrides type.__call__ instead? Also, wouldn't that reset the __module__ value anytime the class is

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2019-06-17 Thread Serhiy Storchaka
Change by Serhiy Storchaka : -- pull_requests: +14008 pull_request: https://github.com/python/cpython/pull/14166 ___ Python tracker ___

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2019-06-17 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: The problem is that __module__ is deduced from the caller's frame in type.__new__(). In case if type.__new__() is called directly, __module__ is set to the name of the module where type.__new__() is called. So virtually every type subclass which defines

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2019-06-16 Thread Alejandro Gonzalez
Alejandro Gonzalez added the comment: >Getting the module name from the caller's frame is an expensive operation. It >is safe to set __module__ to None. In such case the pickle module is able to >find the proper module containing the definition of the class. Wow, indeed it works. I also

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2019-06-16 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: >>> from abc import * >>> A = ABCMeta('A', (), {}) >>> A.__module__ 'abc' >>> import pickle, pickletools >>> pickletools.dis(pickletools.optimize(pickle.dumps(A))) Traceback (most recent call last): File "", line 1, in _pickle.PicklingError: Can't pickle

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2019-06-16 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: Getting the module name from the caller's frame is an expensive operation. It is safe to set __module__ to None. In such case the pickle module is able to find the proper module containing the definition of the class. -- nosy: +serhiy.storchaka

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2019-06-16 Thread Alejandro Gonzalez
Change by Alejandro Gonzalez : -- keywords: +patch pull_requests: +13973 stage: -> patch review pull_request: https://github.com/python/cpython/pull/14126 ___ Python tracker

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2019-06-12 Thread Ivan Levkivskyi
Ivan Levkivskyi added the comment: > Perhaps adding some section like "Notes on pickling dynamically-defined > classes" in the `pickle` module would be more appropriate? I think just a note with few sentences would be enough. -- ___ Python

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2019-06-12 Thread Alejandro Gonzalez
Alejandro Gonzalez added the comment: >I think we can proceed with option A, but only if doesn't cause visible >slow-down for creating ABCs (which is already slower that normal classes). >TBH, I don't want to document A as "official" recipe (maybe however mention >the problem in the

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2019-06-11 Thread Ivan Levkivskyi
Ivan Levkivskyi added the comment: I think we can proceed with option A, but only if doesn't cause visible slow-down for creating ABCs (which is already slower that normal classes). TBH, I don't want to document A as "official" recipe (maybe however mention the problem in the `pickle`

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2019-06-09 Thread Alejandro Gonzalez
Alejandro Gonzalez added the comment: Hello, I would like to add further on this issue. (I'm new to bpo. Please advise if this report could be better) Issue - Pickling breaks when attempting to dump an instance of a class that was defined by calling ABCMeta directly, because it

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2016-12-04 Thread Steven D'Aprano
Steven D'Aprano added the comment: I had a brief look at the source for ABCMeta, and it seems to me that the __module__ behaviour is coming from `type`. I'm not sure whether it can, or should, can be fixed in type, but I think that the correct behaviour for ABCMeta is to set __module__ to the

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2016-12-04 Thread Emanuel Barry
Emanuel Barry added the comment: Oh, nicely spotted! Apparently I was wrong, and it does create a key; defaulting to __name__. About the original issue, I don't think it's easily possible to fix, sadly. -- ___ Python tracker

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2016-12-04 Thread Ivan Levkivskyi
Ivan Levkivskyi added the comment: > As a matter of fact, A.__module__ in this case is abc.ABCMeta.__module__. A > class body creates a __module__ key, while a direct metaclass call does not. But >>> A = ABCMeta('A', (), {}) >>> ABCMeta.__module__ = 'hi' >>> A.__module__ 'abc' >>>

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2016-12-04 Thread Emanuel Barry
Emanuel Barry added the comment: As a matter of fact, A.__module__ in this case is abc.ABCMeta.__module__. A class body creates a __module__ key, while a direct metaclass call does not. -- nosy: +ebarry ___ Python tracker

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2016-12-04 Thread Ivan Levkivskyi
Changes by Ivan Levkivskyi : -- type: -> behavior ___ Python tracker ___ ___

[issue28869] __module__ attribute is not set correctly for a class created by direct metaclass call

2016-12-04 Thread Ivan Levkivskyi
New submission from Ivan Levkivskyi: __module__ attribute is set differently depending on whether a metaclass is explicitly called or invoked in a class statement: >>> A = ABCMeta('A', (), {}) >>> A.__module__ 'abc' >>> class B(metaclass=ABCMeta): ... ... >>> B.__module__ '__main__'