Skip to content

Commit e3ce0bf

Browse files
authored
Fix possible hanging while comparing regex (#102)
1 parent 30f82bd commit e3ce0bf

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

pytest_mypy_plugins/tests/test-regex_assertions.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@
1515
out: |
1616
main:2: note: .*str.*
1717
18+
- case: regex_with_out_does_not_hang
19+
expect_fail: yes
20+
regex: yes
21+
main: |
22+
'abc'.split(4)
23+
out: |
24+
main:1: error: Argument 1 to "split" of "str" has incompatible type "int"; expected "Optional[str]"
25+
26+
- case: regex_with_comment_does_not_hang
27+
expect_fail: yes
28+
regex: yes
29+
main: |
30+
a = 'abc'.split(4) # E: Argument 1 to "split" of "str" has incompatible type "int"; expected "Optional[str]"
31+
1832
- case: expected_single_message_regex
1933
regex: no
2034
main: |
@@ -34,7 +48,7 @@
3448
a = 'hello'
3549
reveal_type(a) # N: .*str.*
3650
37-
- case: regext_does_not_match
51+
- case: regex_does_not_match
3852
expect_fail: yes
3953
regex: no
4054
main: |

pytest_mypy_plugins/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ def _add_aligned_message(s1: str, s2: str, error_message: str) -> str:
144144

145145
error_message += "Alignment of first line difference:\n"
146146

147+
assert s1 != s2
148+
147149
trunc = False
148150
while s1[:30] == s2[:30]:
149151
s1 = s1[10:]
@@ -260,7 +262,9 @@ def format_error_lines(lines: List[str]) -> str:
260262
format_error_lines(actual_message_lines), format_error_lines(expected_message_lines)
261263
)
262264

263-
if (
265+
if expected_line and expected_line.regex:
266+
error_message += "The actual output does not match the expected regex."
267+
elif (
264268
first_diff_actual is not None
265269
and first_diff_expected is not None
266270
and (

0 commit comments

Comments
 (0)