Skip to content

Add protected pprint.PrettyPrinter methods #14228

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
39 changes: 39 additions & 0 deletions stdlib/pprint.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
from collections import deque
from typing import IO

__all__ = ["pprint", "pformat", "isreadable", "isrecursive", "saferepr", "PrettyPrinter", "pp"]
Expand Down Expand Up @@ -110,3 +111,41 @@ class PrettyPrinter:
def isreadable(self, object: object) -> bool: ...
def isrecursive(self, object: object) -> bool: ...
def format(self, object: object, context: dict[int, int], maxlevels: int, level: int) -> tuple[str, bool, bool]: ...
def _format(
self, object: object, stream: IO[str], indent: int, allowance: int, context: dict[int, int], level: int
) -> None: ...
Comment on lines +114 to +116
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IO is generally not recommended to use in argument annotations. From looking at the code, only write() seems to be called on the stream, so SupportsWrite (from _typeshed) seems like a suitable replacement.

Affects the methods below as well.

def _pprint_dict(
self, object: dict[object, object], stream: IO[str], indent: int, allowance: int, context: dict[int, int], level: int
) -> None: ...
def _pprint_list(
self, object: list[object], stream: IO[str], indent: int, allowance: int, context: dict[int, int], level: int
) -> None: ...
def _pprint_tuple(
self, object: tuple[object, ...], stream: IO[str], indent: int, allowance: int, context: dict[int, int], level: int
) -> None: ...
def _pprint_set(
self, object: set[object], stream: IO[str], indent: int, allowance: int, context: dict[int, int], level: int
) -> None: ...
def _pprint_frozenset(
self, object: frozenset[object], stream: IO[str], indent: int, allowance: int, context: dict[int, int], level: int
) -> None: ...
def _pprint_deque(
self, object: deque[object], stream: IO[str], indent: int, allowance: int, context: dict[int, int], level: int
) -> None: ...
def _format_dict_items(
self,
items: list[tuple[object, object]],
stream: IO[str],
indent: int,
allowance: int,
context: dict[int, int],
level: int,
) -> None: ...
def _format_items(
self, items: list[object], stream: IO[str], indent: int, allowance: int, context: dict[int, int], level: int
) -> None: ...
def _repr(self, object: object, context: dict[int, int], level: int) -> str: ...
def _readable(self, object: object, context: dict[int, int]) -> bool: ...
def _recursive(self, object: object, context: dict[int, int]) -> bool: ...
def _depth(self, object: object, context: dict[int, int]) -> int: ...
Comment on lines +148 to +150
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These seem to be attributes, not methods.

def _safe_repr(self, object: object, context: dict[int, int], maxlevels: int, level: int) -> tuple[str, bool, bool]: ...
Loading