[New-bugs-announce] [issue47136] Wrong value assigned automatically to the variable __module__ in the class body.

Takuo Matsuoka report at bugs.python.org
Sun Mar 27 07:17:56 EDT 2022


New submission from Takuo Matsuoka <motogeomtop at gmail.com>:

In the creation of a class, it looks as if the value of the variable
__name__ gets assigned automatically to the variable __module__ in the
class body.  However, the correct name space where the value of
__name__ should be looked up is NOT the mapping object returned by the
method __prepare__
of the metaclass.  In the class body, the programmer may use the
variable __name__ for some other purposes, and might not notice
__module__ was messed up.  Here's a code which produces a problem.

```
class C(type):
    @classmethod
    def __prepare__(cls, /, *args, **kwargs):
        return dict(__name__ = "whatever")

class O(metaclass=C):
    print(__module__) # "whatever" printed
```

Consequently,

>>> O.__module__
'whatever'

The issue is different from but seems related to

https://bugs.python.org/issue28869

I haven't figured out the exact relation.

Thanks.

----------
components: Interpreter Core
messages: 416118
nosy: Takuo Matsuoka
priority: normal
severity: normal
status: open
title: Wrong value assigned automatically to the variable __module__ in the class body.
type: behavior
versions: Python 3.9

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


More information about the New-bugs-announce mailing list