FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-154675: pprint use lazy imports by edvilme · Pull Request #156803 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
19 changes: 8 additions & 11 deletions Lib/pprint.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
import types as _types
from io import StringIO as _StringIO

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"]

Expand Down Expand Up @@ -197,15 +201,14 @@ 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
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.
Expand Down Expand Up @@ -240,15 +243,12 @@ 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
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))
Expand Down Expand Up @@ -422,11 +422,8 @@ 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)
parts = _re.findall(r'\S*\s*', line)
assert parts
assert not parts[-1]
parts.pop() # drop empty last part
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use lazy imports instead of inline imports in pprint
Loading

Back | FazBrowse Home | New Git URL