From b67de01f80d842e1237bd24062a08692cfb7f81c Mon Sep 17 00:00:00 2001 From: Shengyu Zhang Date: Sat, 13 Jan 2024 14:31:46 +0800 Subject: [PATCH 1/4] Use repr when Enum has custom __repr__ impl --- sphinx/util/inspect.py | 2 ++ tests/test_util_inspect.py | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/sphinx/util/inspect.py b/sphinx/util/inspect.py index 218f4abca21..c9aa92c7fc6 100644 --- a/sphinx/util/inspect.py +++ b/sphinx/util/inspect.py @@ -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: diff --git a/tests/test_util_inspect.py b/tests/test_util_inspect.py index 73f96562f42..6ae6ac0a9b1 100644 --- a/tests/test_util_inspect.py +++ b/tests/test_util_inspect.py @@ -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 From f4a6bd0100a8c389a5942ad28a6077865efb025a Mon Sep 17 00:00:00 2001 From: Shengyu Zhang Date: Sun, 14 Jan 2024 17:39:10 +0800 Subject: [PATCH 2/4] Update CHANGES.rst --- CHANGES.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 73cb7409de0..a4f3a24f0a4 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -21,6 +21,9 @@ Features added Patch by Bénédikt Tran. .. _``: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/search +* #11803: autodoc: Render enum values using ``repr()`` when they have custom + ``__repr__`` implementation. + Patch by Shengyu Zhang. Bugs fixed ---------- From 636460f251aa482387ce37d70d739d39cad8822a Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Tue, 16 Jan 2024 18:21:37 +0000 Subject: [PATCH 3/4] mypy --- sphinx/util/inspect.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx/util/inspect.py b/sphinx/util/inspect.py index c9aa92c7fc6..a70beb70e3b 100644 --- a/sphinx/util/inspect.py +++ b/sphinx/util/inspect.py @@ -387,7 +387,7 @@ 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__: + if obj.__repr__.__func__ is not enum.Enum.__repr__: # type: ignore[attr-defined] return repr(obj) return f'{obj.__class__.__name__}.{obj.name}' elif isinstance(obj, tuple): From 8d40b05c041b530ce4fa897fd91320b65f0ea2b2 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Tue, 16 Jan 2024 18:24:18 +0000 Subject: [PATCH 4/4] CHANGES --- CHANGES.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index a4f3a24f0a4..3843e3691cd 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -21,9 +21,8 @@ Features added Patch by Bénédikt Tran. .. _``: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/search -* #11803: autodoc: Render enum values using ``repr()`` when they have custom - ``__repr__`` implementation. - Patch by Shengyu Zhang. +* #11803: autodoc: Use an overriden ``__repr__()`` function in an enum, + if defined. Patch by Shengyu Zhang. Bugs fixed ----------