Skip to content

[3.12] EnumMeta.__getattr__ removed without deprecation #106762

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

Closed
jacobtylerwalls opened this issue Jul 15, 2023 · 8 comments · Fixed by #107466
Closed

[3.12] EnumMeta.__getattr__ removed without deprecation #106762

jacobtylerwalls opened this issue Jul 15, 2023 · 8 comments · Fixed by #107466
Assignees
Labels
3.12 only security fixes 3.13 bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@jacobtylerwalls
Copy link
Contributor

jacobtylerwalls commented Jul 15, 2023

Bug report

>>> from enum import Enum
>>> class Color(Enum):
...     RED = "red"
... 

3.11

>>> Color.__getattr__("RED")
<Color.RED: 'red'>

3.12b4

>>> Color.__getattr__("RED")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'Color' has no attribute '__getattr__'. Did you mean: '__getitem__'?

I see that __getattr__ is documented, so I would have expected either a deprecation notice or an entry in What's New for Python 3.12.

Linked PRs

@jacobtylerwalls jacobtylerwalls added the type-bug An unexpected behavior, bug, or error label Jul 15, 2023
@AlexWaygood AlexWaygood added stdlib Python modules in the Lib dir 3.12 only security fixes 3.13 bugs and security fixes labels Jul 15, 2023
@ethanfurman
Copy link
Member

It's actually EnumMeta.__getattr__, but yes, it needs to be removed from the documentation as well as having a news entry.

@ethanfurman ethanfurman changed the title [3.12] Enum.__getattr__ removed without deprecation [3.12] EnumMeta.__getattr__ removed without deprecation Jul 15, 2023
@jacobtylerwalls
Copy link
Contributor Author

Do you remember what the rationale was for removing __getattr__ in 4e704d7? Most of that commit was reverted in #103236 but not the removal of __getattr__.

The docs reference to __getattr__ was already removed in 6a980de.

@ethanfurman
Copy link
Member

It was the original method of looking up enum members, but it was quite slow; a more performant method is being used now.

Do you have a situation where __getattr__ was being directly called?

@jacobtylerwalls
Copy link
Contributor Author

Yes, I found it when trying to pass the pylint test suite against Python 3.12. Apparently the presence of a __getattr__ attribute is used as a heuristic for whether dot-access syntax is permitted, so pylint's linting of enums will have false negatives without a patch.

Do you have a situation where getattr was being directly called?

So, not called directly, but more like if getattr(EnumClass, "__getattr__"):

A deprecation period would give just a little more runway to deal with that, but the patch probably isn't particularly involved.

@jacobtylerwalls
Copy link
Contributor Author

What about just keeping __getattr__ as a shim for __getitem__?

@ethanfurman
Copy link
Member

Where is has_dynamic_getattr defined? Is it just looking for presence, or something more?

@jacobtylerwalls
Copy link
Contributor Author

jacobtylerwalls commented Jul 17, 2023

It's in an upstream dependency astroid. Here's has_dynamic_getattr: I'm fairly certain it just checks for presence. (When it calls self.getattr, it's checking for the presence of an entry in self.locals, where self is astroid's version of the ast node for the class, and locals is the set of names defined on the class, like this):

# python 3.11
(Pdb) self
<ClassDef.Color l.2 at 0x106fb0910>
(Pdb) self.metaclass()
<ClassDef.EnumType l.468 at 0x1072c9450>
(Pdb) '__getattr__' in self.metaclass().locals
True

@jacobtylerwalls
Copy link
Contributor Author

cc/ @Yhg1s to put on your radar for rc1.

jacobtylerwalls added a commit to jacobtylerwalls/cpython that referenced this issue Jul 30, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jul 31, 2023
ethanfurman pushed a commit that referenced this issue Jul 31, 2023
) (GH-107509)

gh-106762: Add news for `EnumMeta.__getattr__` removal (GH-107466)
(cherry picked from commit de51ded)

Co-authored-by: Jacob Walls <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 only security fixes 3.13 bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants