From 7bdb996c53c02a81f9443cb45f3da41821b29cc7 Mon Sep 17 00:00:00 2001 From: popsiclelmlm <7487674+popsiclelmlm@users.noreply.github.com> Date: Thu, 11 Jun 2026 09:46:46 +0800 Subject: [PATCH] Fix update-data escaping for test sections --- mypy/test/meta/test_update_data.py | 16 ++++++++++++++++ mypy/test/update_data.py | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/mypy/test/meta/test_update_data.py b/mypy/test/meta/test_update_data.py index 8e1c090fc68d7..ef43a33053812 100644 --- a/mypy/test/meta/test_update_data.py +++ b/mypy/test/meta/test_update_data.py @@ -72,6 +72,14 @@ def test_update_data(self) -> None: s1: str = 42 # E: bar [file b.py] s2: str = 43 # E: baz + + [case testEscapedBracketInFile] + # flags: --config-file tmp/mypy.ini + s: str = 42 # E: foo + [file mypy.ini] + \\[mypy] + warn_unused_ignores = True + [builtins fixtures/list.pyi] """) @@ -126,6 +134,14 @@ def test_update_data(self) -> None: s1: str = 42 # E: Incompatible types in assignment (expression has type "int", variable has type "str") [file b.py] s2: str = 43 # E: Incompatible types in assignment (expression has type "int", variable has type "str") + + [case testEscapedBracketInFile] + # flags: --config-file tmp/mypy.ini + s: str = 42 # E: Incompatible types in assignment (expression has type "int", variable has type "str") + [file mypy.ini] + \\[mypy] + warn_unused_ignores = True + [builtins fixtures/list.pyi] """) assert result.input_updated == expected diff --git a/mypy/test/update_data.py b/mypy/test/update_data.py index 84b6383b3f0cf..4199c9b2e7117 100644 --- a/mypy/test/update_data.py +++ b/mypy/test/update_data.py @@ -67,6 +67,7 @@ def _iter_fixes( comment_match = re.search(r"(?P\s+)(?P# [EWN]: .+)$", source_line) if comment_match: source_line = source_line[: comment_match.start("indent")] # strip old comment + source_line = _escape_test_data_line(source_line) if reports: indent = comment_match.group("indent") if comment_match else " " # multiline comments are on the first line and then on subsequent lines empty lines @@ -85,3 +86,11 @@ def _iter_fixes( end_lineno=testcase.line + test_item.end_line - 1, lines=fix_lines + [""] * test_item.trimmed_newlines, ) + + +def _escape_test_data_line(line: str) -> str: + if line.startswith("["): + return "\\" + line + if line.startswith("--"): + return "--" + line + return line