Skip to content

Commit

Permalink
Use repr when Enum has custom __repr__ impl
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverRainZ committed Jan 13, 2024
1 parent 85400a0 commit b67de01
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions sphinx/util/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ def object_description(obj: Any, *, _seen: frozenset = frozenset()) -> str:
return 'frozenset({%s})' % ', '.join(object_description(x, _seen=seen)
for x in sorted_values)
elif isinstance(obj, enum.Enum):
if obj.__repr__.__func__ is not enum.Enum.__repr__:
return repr(obj)
return f'{obj.__class__.__name__}.{obj.name}'
elif isinstance(obj, tuple):
if id(obj) in seen:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_util_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,17 @@ class MyEnum(enum.Enum):
assert inspect.object_description(MyEnum.FOO) == "MyEnum.FOO"


def test_object_description_enum_custom_repr():
class MyEnum(enum.Enum):
FOO = 1
BAR = 2

def __repr__(self):
return self.name

assert inspect.object_description(MyEnum.FOO) == "FOO"


def test_getslots():
class Foo:
pass
Expand Down

0 comments on commit b67de01

Please sign in to comment.