Skip to content
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

The variable __module__ in the class body getting an undesirable value from __prepare__ of the metaclass #91380

Closed
TakuoMatsuoka mannequin opened this issue Apr 5, 2022 · 2 comments
Labels
3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@TakuoMatsuoka
Copy link
Mannequin

TakuoMatsuoka mannequin commented Apr 5, 2022

BPO 47224
Nosy @ethanfurman

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2022-04-05.03:50:39.161>
created_at = <Date 2022-04-05.03:36:25.365>
labels = ['interpreter-core', 'type-bug', '3.9']
title = 'The variable __module__ in the class body getting an undesirable value from __prepare__ of the metaclass'
updated_at = <Date 2022-04-05.03:50:39.159>
user = 'https://bugs.python.org/TakuoMatsuoka'

bugs.python.org fields:

activity = <Date 2022-04-05.03:50:39.159>
actor = 'ethan.furman'
assignee = 'none'
closed = True
closed_date = <Date 2022-04-05.03:50:39.161>
closer = 'ethan.furman'
components = ['Interpreter Core']
creation = <Date 2022-04-05.03:36:25.365>
creator = 'Takuo Matsuoka'
dependencies = []
files = []
hgrepos = []
issue_num = 47224
keywords = []
message_count = 2.0
messages = ['416741', '416744']
nosy_count = 2.0
nosy_names = ['ethan.furman', 'Takuo Matsuoka']
pr_nums = []
priority = 'normal'
resolution = 'duplicate'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue47224'
versions = ['Python 3.9']

@TakuoMatsuoka
Copy link
Mannequin Author

TakuoMatsuoka mannequin commented Apr 5, 2022

Context
-------
Some classes have the variable __name__ in their namespace __dict__ ,
and one may wish to create more such classes with varied values of
__name__ . Some of those could be created with a metaclass whose
__prepare__ returns a mapping having key "__name__", for which the
value is created depending on the arguments of __prepare__ and can be
updated or deleted in the body of the class to be created. (See C
below for a very silly example of such a metaclass.)

Problem
-------
The value of __name__ given by __prepare__ becomes not just that in
the class body, but automatically also the value of __module__
there. As far as I could see, this is not documented, and the
programmer might not notice __module__ was messed up. I think this
behaviour is unexpected and problematic at least unless a warning is
given on it in the document.

Example
-------
Here's a code which produces a problem.

# In this example, the metaclass C is intended to be a class of
# subclasses of:
B = type

class C(type(B)):
    @classmethod
    def __prepare__(cls, /, *args, **kwargs):
        return dict(__name__ = cls._name(*args, **kwargs))
    @classmethod
    def _name(cls, /, *args, **kwargs):
        # The actual value of __name__ doesn't matter much to the
        # issue, so I make this function always return the same silly
        # thing in this example.
        return type.__dict__["__name__"]

class O(B, metaclass=C):
    print(__module__ == __name__) # True
    # Could update or delete __name__ here.

Consequently,

>>> O.__module__
<attribute '__name__' of 'type' objects>

Discussion
----------
If the value of __name__ can be read from the scope
outside for the assignment to __module__, then that will erase this
unexpected behaviour and I think it would be a much safer thing to do.

Remarks
-------
The issue was previously

https://bugs.python.org/issue47136

which I'm going to close now since I failed to specify the issue
clearly enough there. Here I've made the issue more specific.

(The issue is same as https://bugs.python.org/issue47223 which I closed
mistaking it with https://bugs.python.org/issue47136)

The issue is different from but seems related to

https://bugs.python.org/issue28869

I haven't figured out the exact relation.

Thanks.

@TakuoMatsuoka TakuoMatsuoka mannequin added 3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Apr 5, 2022
@ethanfurman
Copy link
Member

Stop creating new issues for the same problem.

If you didn't specify the first issue well enough, add your specifics to it and continue that conversation. Splitting the thoughts and feedback across multiple issues is not efficient.

Closing this issue, reopening the original (bpo-47136).

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant