Skip to content

Commit

Permalink
strip collections.abc from type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
mhils committed Sep 2, 2024
1 parent ecad4ca commit dae19f2
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

## Unreleased: pdoc next

- pdoc now strips `collections.abc.` from type annotations to make them more concise.

## 2024-08-27: pdoc 14.6.1

Expand Down
17 changes: 13 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,7 @@ 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"),
Snapshot("demo", min_version=(3, 9)),
Snapshot("enums", min_version=(3, 12)),
Snapshot("flavors_google"),
Expand Down
Loading

0 comments on commit dae19f2

Please sign in to comment.