-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Specialize access to Enum attributes #95004
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
Comments
Are you working on this? |
No. If you want to do this, just assign yourself. |
I imagine that this will piggyback off of #93988, since |
|
Even if In case it matters, the exact descriptor would be |
|
@brandtbucher it's probably an oversight in how I implemented it. I was being conservative at the time (also I admit just using the direct results from the |
Some complications with specialising enums:
We probably need a specialisation for metaclasses, like what Brandt suggested in his other PR. |
All classes have a metaclass. The issue is whether the metaclass is mutable, has overrides, or shadows class attributes.
This is the only problem here, AFAICT. The easy fix is to make
Properties are descriptors. Which property are you talking about? |
This part is wrong. I initially assumed enum members defined |
No, I think it's just |
We're probably going to want to revisit this soon. Looking at it with fresh eyes, things like the
It seems to me that a much simpler solution would just be to version the metaclass too (main...brandtbucher:cpython:load-attr-metaclass). |
Simpler, but slower. It is probably the best thing to do for now, we can always do the more complex thing later. |
Is there anything I can do to make |
As far as I know, the only way is to set the right flags on the type at the C level. So, short of reimplementing |
I'll open a PR in a bit. |
In |
Ah, thanks. Also, I was wrong earlier: I totally forgot that descriptor |
At least they will be once the performance is improved (which I believe it is even now in 3.12) -- in 3.11 they are only descriptors if the enum class defines a member which matches a property in the inheritance chain:
In the above, the |
Even if the result of calling |
We don't cache the results of function calls anywhere, since in general that's an extremely fragile semantic break. We do things like caching the descriptors themselves, but not the result of calling any Python code. It also puts us in a really nasty situation that we've so far avoided: storing strong references in the inline caches. Realistically, the best we can probably do without adding caches is caching the descriptor and inlining the |
I'm not in an environment where I can research this so I'll just ask: is the enum specialization being discussed here limited only to stdlib enums? I ask because I've been thinking about resurrecting my |
Most likely not. The specializations work on things with the same "shape", "type" or belonging in the same "family" (behavior-wise). So anything specializing stdlib enums would also work for anything that has the same characteristics (ie, mutable metaclass, attribute belonging to a class). If your enum implementation is simpler, we likely already specialize for it. If you want to check the specialization works, run your enums in a loopy function then pass that to |
Is it currently possible in a satisfying way to make EnumType immutable? Unfortunately, in the current situation, implementing mixins which are inherited from mutable objects has no sense and implementing immutability on the user's side is very hard (if at all possible). |
The above comment is based on this Stackoverflow question. As I mention there, the immutability of |
Currently we don't specialize loading of enum attributes.
We can take advantage of the fact that
type(Color.RED) == Color
when specializing.Since we check the version number of
Color
inLOAD_ATTR_CLASS
, we are implicitly checking the version oftype(Color.RED)
.If
Color.RED
is not a descriptor when specialized, it will not be a descriptor when executed.Therefore, we can specialize it in the same way as immutable, non-descriptor class attributes using
LOAD_ATTR_CLASS
.@Fidget-Spinner
@brandtbucher
The text was updated successfully, but these errors were encountered: