Skip to content

Commit c4942ef

Browse files
committed
Fixed error line matching, so that it works when a single line throws multiple errors
1 parent d10ffec commit c4942ef

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

views/utils.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ def _type_check_with_pyright(cls, code: str) -> TypeCheckResult:
104104
if token.type == tokenize.COMMENT
105105
and token.string[1:].strip() == cls.EXPECT_ERROR_COMMENT
106106
]
107+
# Tracks whether an expected error has been reported by type checker.
108+
error_line_seen_in_err_msg: dict[int, bool] = {
109+
lineno: False for lineno in expect_error_line_numbers
110+
}
107111

108112
with tempfile.NamedTemporaryFile(suffix=".py") as temp:
109113
temp.write(code.encode())
@@ -122,19 +126,20 @@ def _type_check_with_pyright(cls, code: str) -> TypeCheckResult:
122126
if m is None:
123127
continue
124128
line_number, message = int(m.group(1)), m.group(2)
125-
if line_number in expect_error_line_numbers:
129+
if line_number in error_line_seen_in_err_msg:
126130
# Each reported error should be attached to a specific line,
127131
# If it is commented with # expect-type-error, let it pass.
128-
expect_error_line_numbers.remove(line_number)
132+
error_line_seen_in_err_msg[line_number] = True
129133
continue
130134
error_lines.append(f"{line_number}:{message}")
131135

132136
# If there are any lines that are expected to fail but not reported by pyright,
133137
# they should be considered as errors.
134-
for line_number in expect_error_line_numbers:
135-
error_lines.append(
136-
f"{line_number}: error: Expected type error but instead passed"
137-
)
138+
for line_number, seen in error_line_seen_in_err_msg.items():
139+
if not seen:
140+
error_lines.append(
141+
f"{line_number}: error: Expected type error but instead passed"
142+
)
138143

139144
passed = len(error_lines) == 0
140145
if passed:

0 commit comments

Comments
 (0)