Skip to content

Commit 9695754

Browse files
refactor: Remove indent_start attribute
1 parent e4d163c commit 9695754

File tree

1 file changed

+31
-36
lines changed

1 file changed

+31
-36
lines changed

util/pformat.py

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -47,95 +47,90 @@ def format(self, o: object):
4747
self._fmt(dest, 0, ContextStack(), o)
4848
return dest.get_value()
4949

50-
def _fmt(self, dest: Dest, indent: int, ctx: ContextStack, o: object, indent_start=True):
50+
def _fmt(self, dest: Dest, indent: int, ctx: ContextStack, o: object):
5151
# Handle internal hacks (to make _get_seq work) before the entering
5252
# the ctx as we don't want our internals showing up on the stack
5353
# and ruining the `up` values in circular refs
5454
if isinstance(o, DontReprMe):
55-
return self._indented_write(dest, indent if indent_start else 0, o.string)
55+
return dest.write(o.string) # Don't write indent - handled by parent
5656
if isinstance(o, StringJoin):
5757
return self._fmt_string_join(dest, indent, ctx, o)
5858
if (idx := ctx.get_idx(o)) is not None:
59-
return self._fmt_circular(dest, indent if indent_start else 0, ctx, idx)
59+
return self._fmt_circular(dest, indent, ctx, idx)
6060
with ctx.enter_context(o):
61-
return self._fmt_inner(dest, indent, ctx, o, indent_start)
61+
return self._fmt_inner(dest, indent, ctx, o)
6262

63-
def _fmt_string_join(self, dest: Dest, indent: int, ctx: ContextStack, join: StringJoin,
64-
indent_start=True):
65-
self._write_indent(dest, indent if indent_start else 0)
63+
def _fmt_string_join(self, dest: Dest, indent: int, ctx: ContextStack, join: StringJoin):
64+
# Don't write indent on first line (handled by parent)
6665
for v in join.parts:
6766
# Only indent first thing (done above)
68-
self._fmt(dest, indent, ctx, v, indent_start=False)
67+
self._fmt(dest, indent, ctx, v)
6968
return
7069

71-
def _fmt_inner(self, dest: Dest, indent: int, ctx: ContextStack, o: object,
72-
indent_start=True):
70+
def _fmt_inner(self, dest: Dest, indent: int, ctx: ContextStack, o: object):
7371
if isinstance(o, enum.Enum):
74-
return self._indented_write(dest, indent if indent_start else 0,
75-
f'{type(o).__name__}.{o.name}')
72+
# Don't write indent on first line (handled by parent)
73+
return dest.write(f'{type(o).__name__}.{o.name}')
7674
if isinstance(o, list):
77-
return self._fmt_seq(dest, indent, ctx, o, '[', ']',
78-
indent_start=indent_start)
75+
return self._fmt_seq(dest, indent, ctx, o, '[', ']')
7976
if isinstance(o, tuple):
8077
return self._fmt_seq(dest, indent, ctx, o, '(', ')',
81-
trailing_comma_if_unitary=True,
82-
indent_start=indent_start)
78+
trailing_comma_if_unitary=True)
8379
if isinstance(o, set):
8480
if len(o) == 0:
85-
return self._indented_write(dest, indent if indent_start else 0, 'set()')
81+
return dest.write('set()')
8682
keys = sorted(o, key=_SafeSortKey)
87-
return self._fmt_seq(dest, indent, ctx, keys, '{', '}',
88-
indent_start=indent_start)
83+
return self._fmt_seq(dest, indent, ctx, keys, '{', '}')
8984
if isinstance(o, dict):
90-
return self._fmt_dict(dest, indent, ctx, o, indent_start)
85+
return self._fmt_dict(dest, indent, ctx, o)
9186
if isinstance(o, frozenset):
9287
if len(o) == 0:
93-
return self._indented_write(
94-
dest, indent if indent_start else 0, 'frozenset()')
88+
return dest.write('frozenset()')
9589
keys = sorted(o, key=_SafeSortKey)
9690
return self._fmt_seq(dest, indent, ctx, keys, 'frozenset({', '})')
9791
# is_dataclass returns True for the class itself so check for that
9892
if dataclasses.is_dataclass(o) and not isinstance(o, type):
99-
return self._fmt_dataclass(dest, indent, ctx, o, indent_start)
100-
return self._fmt_fallback(dest, indent if indent_start else 0, ctx, o)
93+
return self._fmt_dataclass(dest, indent, ctx, o)
94+
return self._fmt_fallback(dest, o)
10195

102-
# _ctx is here for consistency w/ the other _fmt_* methods
103-
def _fmt_fallback(self, dest: Dest, indent: int, _ctx: ContextStack, o: object):
96+
# noinspection PyMethodMayBeStatic
97+
def _fmt_fallback(self, dest: Dest, o: object):
10498
# We will have to be fine with it all being on one line
105-
return self._indented_write(dest, indent, repr(o))
99+
return dest.write(repr(o))
106100

107101
# Any is used for `dcls` as type system is too basic to represent all dataclasses as one
108102
def _fmt_dataclass(self, dest: Dest, indent: int, ctx: ContextStack,
109-
dcls: Any, indent_start=True):
103+
dcls: Any):
110104
# Dataclasses have a reliable key order so don't sort them here
111105
# (For now also add the keys, TODO later might have an option for it?)
112106
self._fmt_seq(dest, indent, ctx, [
113107
StringJoin(DontReprMe(f'{f.name}='), getattr(dcls, f.name))
114108
for f in dataclasses.fields(dcls)
115109
if f.repr # TODO maybe could be slightly cleverer than this (e.g. `or .compare`)
116-
], f'{type(dcls).__name__}(', ')', indent_start=indent_start)
110+
], f'{type(dcls).__name__}(', ')')
117111

118-
def _fmt_circular(self, dest: Dest, indent: int, ctx: ContextStack, idx: int):
112+
# noinspection PyMethodMayBeStatic
113+
def _fmt_circular(self, dest: Dest, _indent: int, ctx: ContextStack, idx: int):
119114
# We would be inserted at ctx.len() so difference/how many to go up is the difference
120115
n_up = ctx.len() - idx
121-
self._indented_write(dest, indent, f'<Circular: {n_up} up>')
116+
dest.write(f'<Circular: {n_up} up>')
122117

123-
def _fmt_dict(self, dest: Dest, indent: int, ctx: ContextStack, d: dict,
124-
indent_start=True):
118+
def _fmt_dict(self, dest: Dest, indent: int, ctx: ContextStack, d: dict):
125119
items = sorted(d.items(), key=_dict_kv_sort_key)
126120
self._fmt_seq(dest, indent, ctx, [
127121
StringJoin(k, DontReprMe(': '), v)
128122
for k, v in items
129-
], '{', '}', indent_start=indent_start)
123+
], '{', '}')
130124

131125
def _fmt_seq(self, dest: Dest, indent: int, ctx: ContextStack,
132126
items: Sequence[object], start: str, end: str,
133-
trailing_comma_if_unitary=False, indent_start=True):
134-
self._write_indent(dest, indent if indent_start else 0)
127+
trailing_comma_if_unitary=False):
128+
# (Don't write indent on first line - handled by parent)
135129
if d := self._try_fmt_seq_short(ctx, items, start, end, trailing_comma_if_unitary):
136130
return dest.extend(d)
137131
dest.write(start, '\n')
138132
for i, v in enumerate(items):
133+
self._write_indent(dest, indent + 1)
139134
self._fmt(dest, indent + 1, ctx, v)
140135
if i != len(items) - 1:
141136
# No space after ',' if it's trailing

0 commit comments

Comments
 (0)