diff --git a/mypy/test/data.py b/mypy/test/data.py
index 30e4ef95adc85..d5ed644b4b474 100644
--- a/mypy/test/data.py
+++ b/mypy/test/data.py
@@ -545,7 +545,7 @@ def expand_errors(input: list[str], output: list[str], fnam: str) -> None:
for i in range(len(input)):
# The first in the split things isn't a comment
- for possible_err_comment in input[i].split(" # ")[1:]:
+ for possible_err_comment in re.split("(?:^| )# ", input[i])[1:]:
m = re.search(
r"^([ENW]):((?P
\d+):)? (?P.*)$", possible_err_comment.strip()
)
diff --git a/mypy/test/meta/test_parse_data.py b/mypy/test/meta/test_parse_data.py
index 07343414293c2..47324098b0e04 100644
--- a/mypy/test/meta/test_parse_data.py
+++ b/mypy/test/meta/test_parse_data.py
@@ -67,3 +67,23 @@ def test_bad_eq_version_check(self) -> None:
assert (
"version==3.7 always false since minimum runtime version is (3, 10)" in actual.stdout
)
+
+ def test_typical_error_comment(self) -> None:
+ # Act
+ actual = _run_pytest("""
+ [case abc]
+ a # E: typical-looking error comment
+ """)
+
+ # Assert
+ assert "typical-looking error comment" in actual.stderr
+
+ def test_lineleading_error_comment(self) -> None:
+ # Act
+ actual = _run_pytest("""
+ [case abc]
+ # E: line-leading error comment
+ """)
+
+ # Assert
+ assert "line-leading error comment" in actual.stderr