Visitar URL original
Prohibit direct retrieval of __prepare__ from an instance of the metaclass. · Issue #113045 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20240106171008/https://github.com/python/cpython/issues/113045
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

Prohibit direct retrieval of __prepare__ from an instance of the metaclass. #113045

Open
takuom opened this issue Dec 13, 2023 · 9 comments
Open
Labels
type-feature A feature request or enhancement

Comments

@takuom
Copy link

takuom commented Dec 13, 2023 •

Bug report

Bug description:

The class method __prepare__ of the built-in class type can sometimes be retrieved from an instance of type.

class O:
    pass

# An instance of `type`
instance = O

assert instance.__prepare__ == type(instance).__prepare__
# Fine.

However, I think that the most careful programmer (which I may not be unfortunately...) would usually avoid instance.__prepare__ since an instance of type can be a metaclass.

class C(type):
    pass

instance = C

assert instance.__prepare__ == type(instance).__prepare__
# AssertionError

When instance is a metaclass, instance.__prepare__ may silently return a wrong thing if type(instance).__prepare__ is intended by that.

I think that the behaviour of __prepare__.__get__, where __prepare__ is vars(type)["__prepare__"], should be such that it raises an exception when it's called for instance.__prepare__ (or __getattribute__.__get__(instance)("__prepare__"), where __getattribute__ is vars(type)["__getattribute__"], to be more precise) where the class instance doesn't have __prepare__ (so instance is not a metaclass in particular). Writing instance.__prepare__ for type(instance).__prepare__ isn't necessary and may bring you a trouble, so seems like something which no one really wants to be able to do.

(It would have been the easiest if a fix could be done by letting __prepare__.__get__(instance) (i.e., a call with the default second argument) raise an exception, but I suspect that the second argument from instance.__prepare__ will be type(instance). There might be a few options to consider.)

The exception could also help when it happens that cls is meant to be a metaclass in the expression cls.__prepare__, but is actually a non-meta-class.

(classmethod is not always a convenient class for a similar reason...)

CPython versions tested on:

3.11

Operating systems tested on:

macOS

@takuom takuom added the type-bug An unexpected behavior, bug, or error label Dec 13, 2023
@ericvsmith
Copy link
Member

In general Python does not try to protect the user from mistaken assumptions like this.

@takuom
Copy link
Author

takuom commented Dec 13, 2023

Thanks @ericvsmith for treating this. If that's the policy, then I don't seem to be able to affect it, but might you try to alert the user in documentation then? (Or did I miss an alert?) I don't think I could be cautious by myself when I used classmethod for the first time, or even when I encountered __prepare__ later. I didn't have sufficient experience yet, but I thought Python tries to be friendly to novices (and it has been so to me at least, for which I thank you guys).

@ericvsmith
Copy link
Member

It's the classic tension between "be friendly to users" and "we're all consenting adults here": We don't want to prohibit something that someone somewhere might be using.

I don't have any particular insight into this issue, beyond that.

@takuom
Copy link
Author

takuom commented Dec 13, 2023

Thanks @ericvsmith for your further comments.

We don't want to prohibit something that someone somewhere might be using.

Ah, that person may know what she's doing. That's possible.

What do you think of adding to documentation, which I proposed?

@ericvsmith
Copy link
Member

I don't have an opinion, other than: is this different from any other "special method" lookups, which are always done on the class, not instance?

@terryjreedy terryjreedy added type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error labels Dec 13, 2023
@terryjreedy
Copy link
Member

For the tracker, a bug is (usually) a discrepancy between code and doc, which seems not to be the case here. Eric asked the crucial question, whether __prepare__ is a unique special method in its behavior.

@takuom
Copy link
Author

takuom commented Dec 14, 2023

What's unique about __prepare__ on type is that it's a class method on a built-in metaclass (and that not every class has __prepare__). (__new__ is closer to __prepare__ than to instance methods in that it's a static method, but it doesn't have a similar issue since every class has __new__.)

Potentially, you can have a similar issue also with almost any classmethod object I think, but I picked up __prepare__ on type since this object seemed more difficult to avoid relying on than classmethod objects. I was not talking about the case where __prepare__ is automatically looked up and used for something like class O(metaclass=C): .... What I was talking about was retrieval of __prepare__ by __getattribute__ on type, with like __getattribute__.__get__(instance, type(instance))("__prepare__") (please see my first post), in which lookup may be done both on "the class" and on "the instance".

@ethanfurman
Copy link
Member

Looking at Enum for a more readable example:

>>> from enum import Enum

>>> Enum.__prepare__
<bound method EnumType.__prepare__ of <class 'enum.EnumType'>>

>>> type(Enum).__prepare__
<bound method EnumType.__prepare__ of <class 'enum.EnumType'>>

>>> type(type(Enum)).__prepare__
<built-in method __prepare__ of type object at 0x556874ec72c0>

The difference between O above and Enum is that EnumType does have its own __prepare__ (otherwise it would have gone straight to object.__prepare__).

This seems to be working just as it's supposed to, and I see no need for further action.

@takuom, do you have an example of where this behavior was a problem for you?

@takuom
Copy link
Author

takuom commented Dec 19, 2023

Thank you @ethanfurman for looking into this. I can point out at least two problems about the behaviour, but Eric has already convinced me that the behaviour shouldn't be changed. I think some remarks can be added to documentation about it if you'd like to make Python very friendly to less experienced programmers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants