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

Close #9272: autodoc: Render enum values for the default argument value better #9281

Merged
merged 1 commit into from May 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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