From 31ec519d3808617e333794493e0aabc5fe5d4f81 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sun, 30 May 2021 02:15:28 +0900 Subject: [PATCH] Close #9272: autodoc: Render enum values for the default argument value better --- CHANGES | 1 + sphinx/util/inspect.py | 7 +++++-- tests/test_util_inspect.py | 9 +++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 64410011e3a..8007636a8cc 100644 --- a/CHANGES +++ b/CHANGES @@ -35,6 +35,7 @@ Features added * #8061, #9218: autodoc: Support variable comment for alias classes * #3014: autodoc: Add :event:`autodoc-process-bases` to modify the base classes of the class definitions +* #9272: autodoc: Render enum values for the default argument value better * #3257: autosummary: Support instance attributes for classes * #9129: html search: Show search summaries when html_copy_source = False * #9120: html theme: Eliminate prompt characters of code-block from copyable diff --git a/sphinx/util/inspect.py b/sphinx/util/inspect.py index a415a7074c8..23dd9e9307f 100644 --- a/sphinx/util/inspect.py +++ b/sphinx/util/inspect.py @@ -442,14 +442,14 @@ def object_description(object: Any) -> str: (object_description(key), object_description(object[key])) for key in sorted_keys) return "{%s}" % ", ".join(items) - if isinstance(object, set): + elif isinstance(object, set): try: sorted_values = sorted(object) except TypeError: pass # Cannot sort set values, fall back to generic repr else: return "{%s}" % ", ".join(object_description(x) for x in sorted_values) - if isinstance(object, frozenset): + elif isinstance(object, frozenset): try: sorted_values = sorted(object) except TypeError: @@ -457,6 +457,9 @@ def object_description(object: Any) -> str: else: return "frozenset({%s})" % ", ".join(object_description(x) for x in sorted_values) + elif isinstance(object, enum.Enum): + return "%s.%s" % (object.__class__.__name__, object.name) + try: s = repr(object) except Exception as exc: diff --git a/tests/test_util_inspect.py b/tests/test_util_inspect.py index de4ad92366b..2f805a87a89 100644 --- a/tests/test_util_inspect.py +++ b/tests/test_util_inspect.py @@ -10,6 +10,7 @@ import ast import datetime +import enum import functools import sys import types @@ -516,6 +517,14 @@ def __repr__(self): assert ": 2" in description +def test_object_description_enum(): + class MyEnum(enum.Enum): + FOO = 1 + BAR = 2 + + assert inspect.object_description(MyEnum.FOO) == "MyEnum.FOO" + + def test_getslots(): class Foo: pass