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
Comments
|
In general Python does not try to protect the user from mistaken assumptions like this. |
|
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 |
|
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. |
|
Thanks @ericvsmith for your further comments.
Ah, that person may know what she's doing. That's possible. What do you think of adding to documentation, which I proposed? |
|
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? |
|
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 |
|
What's unique about Potentially, you can have a similar issue also with almost any |
|
Looking at >>> 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 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? |
|
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. |


Bug report
Bug description:
The class method
__prepare__of the built-in classtypecan sometimes be retrieved from an instance oftype.However, I think that the most careful programmer (which I may not be unfortunately...) would usually avoid
instance.__prepare__since an instance oftypecan be a metaclass.When
instanceis a metaclass,instance.__prepare__may silently return a wrong thing iftype(instance).__prepare__is intended by that.I think that the behaviour of
__prepare__.__get__, where__prepare__isvars(type)["__prepare__"], should be such that it raises an exception when it's called forinstance.__prepare__(or__getattribute__.__get__(instance)("__prepare__"), where__getattribute__isvars(type)["__getattribute__"], to be more precise) where the classinstancedoesn't have__prepare__(soinstanceis not a metaclass in particular). Writinginstance.__prepare__fortype(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 frominstance.__prepare__will betype(instance). There might be a few options to consider.)The exception could also help when it happens that
clsis meant to be a metaclass in the expressioncls.__prepare__, but is actually a non-meta-class.(
classmethodis not always a convenient class for a similar reason...)CPython versions tested on:
3.11
Operating systems tested on:
macOS
The text was updated successfully, but these errors were encountered: