Author scoder
Recipients ncoghlan, petr.viktorin, scoder, serhiy.storchaka
Date 2020-05-20.21:24:30
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1590009872.3.0.237127808252.issue40703@roundup.psfhosted.org>
In-reply-to
Content
The PyType_FromSpec() functions set the type's "__module__" attribute at the end:

https://github.com/python/cpython/blob/0509c4547fc95cc32a91ac446a26192c3bfdf157/Objects/typeobject.c#L3154-L3172

There are only two possible cases, either it finds a module name in the spec name and sets "__module__" from that, or it outputs a deprecation warning. Both behaviours are annoying because they ignore anything that the type already has in its dict, e.g. a property entry from the "members" or "getset" structs.

Since this code can't really be moved before the call to "PyType_Ready()" (which creates the type's dict and populates it), I think the best fix would be to first check if "__module__" is already in the dict and only otherwise take care of setting it.

I noticed this when trying to make the "__module__" attribute of Cython's coroutine type work with type specs, which should actually return the instance specific module name, i.e. the module that defines the coroutine, not the module that defines the type. This would normally be solved with an entry in "members", but that is discarded by the code above. While this approach means that the type does not have its own "__module__" entry, I think the use cases of pickling and documentation support it, because they care about the module name of the instance, not of the type.

I think working around this behaviour is somewhat easy by creating a new descriptor with PyDescr_NewMember() and re-adding it after calling PyType_FromSpec*(), so this is something that can just be fixed in Py3.9+.

Relevant tickets: issue 15146 for setting "__module__" and issue 20204 for the deprecation warning.
History
Date User Action Args
2020-05-20 21:24:32scodersetrecipients: + scoder, ncoghlan, petr.viktorin, serhiy.storchaka
2020-05-20 21:24:32scodersetmessageid: <1590009872.3.0.237127808252.issue40703@roundup.psfhosted.org>
2020-05-20 21:24:32scoderlinkissue40703 messages
2020-05-20 21:24:30scodercreate