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

Strip collections.abc from type annotations #736

Merged
merged 3 commits into from
Sep 4, 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

- pdoc now strips `collections.abc.` from type annotations to make them more concise.
([#736](https://github.com/mitmproxy/pdoc/pull/736), @mhils)

## 2024-08-27: pdoc 14.6.1

Expand Down
18 changes: 14 additions & 4 deletions pdoc/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,9 +1132,11 @@ def default_value_str(self) -> str:
if self.default_value is empty:
return ""
if isinstance(self.default_value, TypeAliasType):
return formatannotation(self.default_value.__value__)
formatted = formatannotation(self.default_value.__value__)
return _remove_collections_abc(formatted)
elif self.annotation == TypeAlias:
return formatannotation(self.default_value)
formatted = formatannotation(self.default_value)
return _remove_collections_abc(formatted)

# This is not perfect, but a solid attempt at preventing accidental leakage of secrets.
# If you have input on how to improve the heuristic, please send a pull request!
Expand Down Expand Up @@ -1166,7 +1168,8 @@ def default_value_str(self) -> str:
def annotation_str(self) -> str:
"""The variable's type annotation as a pretty-printed str."""
if self.annotation is not empty:
return f": {formatannotation(self.annotation)}"
formatted = formatannotation(self.annotation)
return f": {_remove_collections_abc(formatted)}"
else:
return ""

Expand Down Expand Up @@ -1202,6 +1205,7 @@ def _params(self) -> list[str]:
for param in self.parameters.values():
formatted = str(param)
formatted = _remove_memory_addresses(formatted)
formatted = _remove_collections_abc(formatted)

kind = param.kind

Expand Down Expand Up @@ -1238,7 +1242,8 @@ def _params(self) -> list[str]:

def _return_annotation_str(self) -> str:
if self.return_annotation is not empty:
return formatannotation(self.return_annotation)
formatted = formatannotation(self.return_annotation)
return _remove_collections_abc(formatted)
else:
return ""

Expand Down Expand Up @@ -1333,3 +1338,8 @@ def _safe_getdoc(obj: Any) -> str:
def _remove_memory_addresses(x: str) -> str:
"""Remove memory addresses from repr() output"""
return re.sub(r" at 0x[0-9a-fA-F]+(?=>)", "", x)


def _remove_collections_abc(x: str) -> str:
"""Remove 'collections.abc' from type signatures."""
return re.sub(r"(?!\.)\bcollections\.abc\.", "", x)
1 change: 1 addition & 0 deletions test/test_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def outfile(self, format: str) -> Path:

snapshots = [
Snapshot("ast_parsing"),
Snapshot("collections_abc", min_version=(3, 9)),
Snapshot("demo", min_version=(3, 9)),
Snapshot("enums", min_version=(3, 12)),
Snapshot("flavors_google"),
Expand Down
Loading