[New-bugs-announce] [issue40703] PyType_FromSpec*() overwrites the type's "__module__"

Stefan Behnel report at bugs.python.org
Wed May 20 17:24:32 EDT 2020


New submission from Stefan Behnel <stefan_ml at behnel.de>:

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.

----------
components: C API
messages: 369476
nosy: ncoghlan, petr.viktorin, scoder, serhiy.storchaka
priority: normal
severity: normal
status: open
title: PyType_FromSpec*() overwrites the type's "__module__"
type: behavior
versions: Python 3.10, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40703>
_______________________________________


More information about the New-bugs-announce mailing list