From 928cb083b18d8f874d5baad1c4d478f41581cec7 Mon Sep 17 00:00:00 2001 From: Eduardo Villalpando Mello Date: Tue, 1 Sep 2026 19:19:08 -0400 Subject: [PATCH 1/3] gh-154675: pprint use lazy imports Use lazy imports instead of inline imports in pprint --- Lib/pprint.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/Lib/pprint.py b/Lib/pprint.py index 7355021998081d..8994f6158faa8e 100644 --- a/Lib/pprint.py +++ b/Lib/pprint.py @@ -39,6 +39,10 @@ import types as _types from io import StringIO as _StringIO +lazy from dataclasses import is_dataclass +lazy from dataclasses import fields as dataclass_fields +lazy import re + __all__ = ["pprint","pformat","isreadable","isrecursive","saferepr", "PrettyPrinter", "pp"] @@ -197,8 +201,7 @@ def _format(self, object, stream, indent, allowance, context, level): max_width = self._width - indent - allowance if len(rep) > max_width: p = self._dispatch.get(type(object).__repr__, None) - # Lazy import to improve module import time - from dataclasses import is_dataclass + if p is not None: context[objid] = 1 @@ -240,9 +243,6 @@ def _write_indent_padding(self, write): write((self._indent_per_level - 1) * " ") def _pprint_dataclass(self, object, stream, indent, allowance, context, level): - # Lazy import to improve module import time - from dataclasses import fields as dataclass_fields - cls_name = object.__class__.__name__ if self._expand: indent += self._indent_per_level @@ -422,9 +422,6 @@ def _pprint_str(self, object, stream, indent, allowance, context, level): if len(rep) <= max_width1: chunks.append(rep) else: - # Lazy import to improve module import time - import re - # A list of alternating (non-space, space) strings parts = re.findall(r'\S*\s*', line) assert parts From 82d7d1f9ef474a73c299469b5a8175abdc256591 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 1 Sep 2026 23:24:35 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2026-09-01-23-24-34.gh-issue-154675.hmvwgW.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2026-09-01-23-24-34.gh-issue-154675.hmvwgW.rst diff --git a/Misc/NEWS.d/next/Library/2026-09-01-23-24-34.gh-issue-154675.hmvwgW.rst b/Misc/NEWS.d/next/Library/2026-09-01-23-24-34.gh-issue-154675.hmvwgW.rst new file mode 100644 index 00000000000000..2bad30fbaf6d28 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-09-01-23-24-34.gh-issue-154675.hmvwgW.rst @@ -0,0 +1 @@ +Use lazy imports instead of inline imports in pprint From e0556834a346638f08b43d7b2f11a79a0226c561 Mon Sep 17 00:00:00 2001 From: Eduardo Villalpando Mello Date: Wed, 2 Sep 2026 12:15:09 -0400 Subject: [PATCH 3/3] Use private imports --- Lib/pprint.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Lib/pprint.py b/Lib/pprint.py index 8994f6158faa8e..28f8cc85b4ce0b 100644 --- a/Lib/pprint.py +++ b/Lib/pprint.py @@ -39,9 +39,9 @@ import types as _types from io import StringIO as _StringIO -lazy from dataclasses import is_dataclass -lazy from dataclasses import fields as dataclass_fields -lazy import re +lazy from dataclasses import is_dataclass as _is_dataclass +lazy from dataclasses import fields as _dataclass_fields +lazy import re as _re __all__ = ["pprint","pformat","isreadable","isrecursive","saferepr", "PrettyPrinter", "pp"] @@ -208,7 +208,7 @@ def _format(self, object, stream, indent, allowance, context, level): p(self, object, stream, indent, allowance, context, level + 1) del context[objid] return - elif (is_dataclass(object) and + elif (_is_dataclass(object) and not isinstance(object, type) and object.__dataclass_params__.repr and # Check dataclass has generated repr method. @@ -248,7 +248,7 @@ def _pprint_dataclass(self, object, stream, indent, allowance, context, level): indent += self._indent_per_level else: indent += len(cls_name) + 1 - items = [(f.name, getattr(object, f.name)) for f in dataclass_fields(object) if f.repr] + items = [(f.name, getattr(object, f.name)) for f in _dataclass_fields(object) if f.repr] stream.write(self._format_block_start(cls_name + '(', indent)) self._format_namespace_items(items, stream, indent, allowance, context, level) stream.write(self._format_block_end(')', indent - self._indent_per_level)) @@ -423,7 +423,7 @@ def _pprint_str(self, object, stream, indent, allowance, context, level): chunks.append(rep) else: # A list of alternating (non-space, space) strings - parts = re.findall(r'\S*\s*', line) + parts = _re.findall(r'\S*\s*', line) assert parts assert not parts[-1] parts.pop() # drop empty last part