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

enum.global_enum: cls_name undefined in global_str #93100

Closed
The-Compiler opened this issue May 23, 2022 · 1 comment
Closed

enum.global_enum: cls_name undefined in global_str #93100

The-Compiler opened this issue May 23, 2022 · 1 comment
Assignees
Labels
3.11 only security fixes 3.12 bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@The-Compiler
Copy link
Contributor

Bug report

In acf7403 (#30582), @ethanfurman implemented a global_enum decorator, replacing the __str__ of an enum with enum.global_str:

cpython/Lib/enum.py

Lines 1638 to 1645 in 760ec89

def global_str(self):
"""
use enum_name instead of class.enum_name
"""
if self._name_ is None:
return "%s(%r)" % (cls_name, self._value_)
else:
return self._name_

(This was later reverted in 42a64c0 and reintroduced in 83d544b, but I believe the code to be the same).

If the ._name_ attribute is None, this uses cls_name, but that's not defined anywhere in that function!

Here is a (somewhat contrived) example:

import enum

@enum.global_enum
class EnumTest(enum.Enum):
    VAL = 1

EnumTest.VAL._name_ = None

print(str(VAL))

this fails with:

Traceback (most recent call last):
  File "/home/florian/tmp/x.py", line 9, in <module>
    print(str(VAL))
          ^^^^^^^^
  File "/usr/lib/python3.11/enum.py", line 1646, in global_str
    return "%s(%r)" % (cls_name, self._value_)
                       ^^^^^^^^
NameError: name 'cls_name' is not defined

(Found by running flake8 over Lib/ out of curiosity, see #93010 (comment))

Your environment

  • CPython versions tested on: Python 3.11.0b1
  • Operating system and architecture: Archlinux x86_64
@The-Compiler The-Compiler added the type-bug An unexpected behavior, bug, or error label May 23, 2022
@AlexWaygood AlexWaygood added stdlib Python modules in the Lib dir 3.11 only security fixes 3.12 bugs and security fixes labels May 23, 2022
ethanfurman added a commit that referenced this issue May 23, 2022
@ethanfurman
Copy link
Member

Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants