-
-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
STORE_ANNOTATION bytecode is unnecessary and can be removed. #76731
Comments
The STORE_ANNOTATION bytecode is used to implement annotations. Consequently, can be trivially replaced with |
There some subtle differences.
|
There are several corner cases. For example consider this code: >>> class C:
... del __annotations__
... x: int Currently this correctly raises NameError, with your replacement it will instead stick {'x': int} in the module |
PEP-526 explicitly states I consider deleting __annotations__ to be undocumented use. Do you really think that an obscure difference in the behaviour of >>> class C:
... del __annotations__
... x: int justifies an extra bytecode, but the implicit return at the end of all functions (LOAD_CONST None; RETURN_VALUE) does not? |
This PR moves the constant for the name from `co_names` to `co_consts`. There is no duplication.
>>> def f():
... class C:
... a : 1
(It does add __annotations__ to `co_names`, but that seems reasonable to me)
Current:
>>> f.__code__.co_consts[1].co_names
('__name__', '__module__', '__qualname__', 'a')
>>> f.__code__.co_consts[1].co_consts
('f.<locals>.C', 1, None)
With PR 5181:
>>> f.__code__.co_consts[1].co_names
('__name__', '__module__', '__qualname__', '__annotations__')
>>> f.__code__.co_consts[1].co_consts
('f.<locals>.C', 1, 'a', None) |
There is also another corner case to consider: class C:
exec('x: int') assert C.__annotations__ == {'x': int} I am not sure this one will be covered correctly. Let us see what others think about this. |
Works as expected: >>> class C:
... exec('x: int')
...
>>> C.__annotations__
{'x': <class 'int'>}
>>> __annotations__
{} |
I would like to see this patch go forward. Making __annotations__ special and subtly different just adds to language complexity. |
I won't object. It makes sense that the implementation of __annotations__ should be low-key. |
But if there is an initializer, the name is left in I don't think this (as well as possible performance difference) is important. My only concerns are about subtle behavior differences. For example, is the name always interned (even if long or non-ASCII)? Does any code depend on interning keys in __annotations__? (There is a code that depends on interning keys in type.__dict__). |
There is very little code that depends on __annotations__ at all, and none |
There are a couple of merge conflicts in the patch. Once deconflicted, I think this PR is ready to apply. |
Is this going to make it into beta 1 before tonight? If not should we abandon it or put it off till 3.8 or can it go into 3.7beta2? |
If Mark can get it in tonight, that would be great. The patch has been ready for a long time. It just needs to have the merge conflicts resolved. Otherwise, I think 3.7beta2 would be fine. |
I expect that Mark only checks his mail occasionally. Sometimes GitHub lets you edit a PR and then you can also resolve the conflict yourself. If that's not the case feel free to fork the PR and submit it yourself. |
If it can wait another hour, I will be at home and can do the rebase then. |
Mark, at the moment, you have at least another 14 hours until the announced code freeze deadline :) |
Awesome! |
Rebased, pushed and CI is green. |
W00t! |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: