Fix get_dict_ascii_tree ignoring new_line=False (#2805)

`get_dict_ascii_tree(items, prepend="", new_line=True)` immediately reassigned
`new_line` to the horizontal box-drawing glyph ("─"), shadowing the boolean
parameter. The trailing `if not new_line: text = text[1:]` — meant to strip the
leading newline — therefore tested a non-empty string (always truthy) and never
ran. Callers passing `new_line=False` (e.g. `maigret.py` printing a user's
identity data) got a stray leading blank line.

Rename the glyph local to `h_line` so the `new_line` parameter is preserved and
its strip takes effect. The tree drawing is unchanged.

Adds a regression test asserting `new_line=False` drops the leading newline
while keeping the rest of the tree identical.
This commit is contained in:
jichaowang02-lang
2026-06-28 20:11:46 +02:00
committed by GitHub
parent ca6d7e474e
commit ac25cd71bb
2 changed files with 15 additions and 2 deletions
+2 -2
View File
@@ -84,14 +84,14 @@ def ascii_data_display(data: str) -> Any:
def get_dict_ascii_tree(items, prepend="", new_line=True):
new_result = b'\xe2\x94\x9c'.decode()
new_line = b'\xe2\x94\x80'.decode()
h_line = b'\xe2\x94\x80'.decode()
last_result = b'\xe2\x94\x94'.decode()
skip_result = b'\xe2\x94\x82'.decode()
text = ""
for num, item in enumerate(items):
box_symbol = (
new_result + new_line if num != len(items) - 1 else last_result + new_line
new_result + h_line if num != len(items) - 1 else last_result + h_line
)
if isinstance(item, tuple):
+13
View File
@@ -233,3 +233,16 @@ def test_is_plausible_username_rejects_non_strings():
assert not is_plausible_username(None)
assert not is_plausible_username(42)
assert not is_plausible_username(["alice"])
def test_get_dict_ascii_tree_new_line_false_strips_leading_newline():
# new_line=False must drop the leading newline. It used to be a no-op
# because the parameter was shadowed by the horizontal box-drawing glyph.
items = [('a', '1'), ('b', '2')]
with_nl = get_dict_ascii_tree(items)
without_nl = get_dict_ascii_tree(items, new_line=False)
assert with_nl.startswith('\n')
assert not without_nl.startswith('\n')
# Only the leading newline differs; the tree content is identical.
assert with_nl[1:] == without_nl