Skip to content

Commit

Permalink
Merge pull request #9281 from tk0miya/9272_enum_for_defargs
Browse files Browse the repository at this point in the history
Close #9272: autodoc: Render enum values for the default argument value better
  • Loading branch information
tk0miya committed May 30, 2021
2 parents 429d0f7 + 31ec519 commit 7dbe8eb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions sphinx/util/inspect.py
Expand Up @@ -442,21 +442,24 @@ 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:
pass # Cannot sort frozenset values, fall back to generic repr
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:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_util_inspect.py
Expand Up @@ -10,6 +10,7 @@

import ast
import datetime
import enum
import functools
import sys
import types
Expand Down Expand Up @@ -516,6 +517,14 @@ def __repr__(self):
assert "<CustomType(2)>: 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
Expand Down

0 comments on commit 7dbe8eb

Please sign in to comment.