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

improve enum rendering, fix #701 #708

Merged
merged 6 commits into from
Jul 10, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## Unreleased: pdoc next

- Improve rendering of enums on Python 3.12+.
([#708](https://github.com/mitmproxy/pdoc/pull/708), @mhils)
- Fix a bug where hyperlinks would get parsed as docstring references.
([#709](https://github.com/mitmproxy/pdoc/pull/709), @mhils)

Expand Down
12 changes: 12 additions & 0 deletions pdoc/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,9 @@ def docstring(self) -> str:
if doc == dict.__doc__:
# Don't display default docstring for dict subclasses (primarily TypedDict).
return ""
if doc in _Enum_default_docstrings:
# Don't display default docstring for enum subclasses.
return ""
is_dataclass_with_default_docstring = (
dataclasses.is_dataclass(self.obj)
# from https://github.com/python/cpython/blob/3.10/Lib/dataclasses.py
Expand Down Expand Up @@ -1306,6 +1309,15 @@ def _safe_getdoc(obj: Any) -> str:
return doc.strip()


_Enum_default_docstrings = tuple(
{
_safe_getdoc(enum.Enum),
_safe_getdoc(enum.IntEnum),
_safe_getdoc(_safe_getattr(enum, "StrEnum", enum.Enum)),
}
)


def _remove_memory_addresses(x: str) -> str:
"""Remove memory addresses from repr() output"""
return re.sub(r" at 0x[0-9a-fA-F]+(?=>)", "", x)
1 change: 1 addition & 0 deletions test/test_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def outfile(self, format: str) -> Path:
snapshots = [
Snapshot("ast_parsing"),
Snapshot("demo", min_version=(3, 9)),
Snapshot("enums", min_version=(3, 12)),
Snapshot("flavors_google"),
Snapshot("flavors_numpy"),
Snapshot("flavors_rst"),
Expand Down
Loading