Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions mypy/test/meta/test_update_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
""")

Expand Down Expand Up @@ -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
9 changes: 9 additions & 0 deletions mypy/test/update_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def _iter_fixes(
comment_match = re.search(r"(?P<indent>\s+)(?P<comment># [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
Expand All @@ -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
Loading