From ac25cd71bb7dcd39b313d5b9530423780fe229a5 Mon Sep 17 00:00:00 2001 From: jichaowang02-lang Date: Sun, 28 Jun 2026 19:11:46 +0100 Subject: [PATCH] Fix get_dict_ascii_tree ignoring new_line=False (#2805) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `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. --- maigret/utils.py | 4 ++-- tests/test_utils.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/maigret/utils.py b/maigret/utils.py index aef05cb..e980a6c 100644 --- a/maigret/utils.py +++ b/maigret/utils.py @@ -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): diff --git a/tests/test_utils.py b/tests/test_utils.py index 2a96c80..26ca1cd 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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