Skip to content

Commit 8defa6a

Browse files
committed
Allow regexes on specific messages.
1 parent 2019cb1 commit 8defa6a

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ Implementation notes:
139139
main:2: note: .*str.*
140140
```
141141

142+
#### 5. Regular expressions specific lines of output.
143+
144+
```yaml
145+
- case: expected_single_message_regex
146+
main: |
147+
a = 'hello'
148+
reveal_type(a) # NR: .*str.*
149+
```
150+
142151
## Options
143152

144153
```

pytest_mypy_plugins/tests/test-regex_assertions.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,9 @@
1414
reveal_type(a)
1515
out: |
1616
main:2: note: .*str.*
17+
18+
- case: expected_single_message_regex
19+
regex: no
20+
main: |
21+
a = 'hello'
22+
reveal_type(a) # NR: .*str.*

pytest_mypy_plugins/utils.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,29 +302,42 @@ def extract_output_matchers_from_comments(fname: str, input_lines: List[str], re
302302
"""Transform comments such as '# E: message' or
303303
'# E:3: message' in input.
304304
305-
The result is lines like 'fnam:line: error: message'.
305+
The result is a list pf output matchers
306306
"""
307307
fname = fname.replace(".py", "")
308308
matchers = []
309309
for index, line in enumerate(input_lines):
310310
# The first in the split things isn't a comment
311311
for possible_err_comment in line.split(" # ")[1:]:
312-
m = re.search(r"^([ENW]):((?P<col>\d+):)? (?P<message>.*)$", possible_err_comment.strip())
313-
if m:
314-
if m.group(1) == "E":
312+
match = re.search(r"^([ENW])(?P<regex>[R]):((?P<col>\d+):)? (?P<message>.*)$", possible_err_comment.strip())
313+
if match:
314+
if match.group(1) == "E":
315315
severity = "error"
316-
elif m.group(1) == "N":
316+
elif match.group(1) == "N":
317317
severity = "note"
318-
elif m.group(1) == "W":
318+
elif match.group(1) == "W":
319319
severity = "warning"
320-
col = m.group("col")
320+
else:
321+
severity = match.group(1)
322+
col = match.group("col")
321323
matchers.append(
322-
OutputMatcher(fname, index + 1, severity, message=m.group("message"), regex=regex, col=col)
324+
OutputMatcher(
325+
fname,
326+
index + 1,
327+
severity,
328+
message=match.group("message"),
329+
regex=regex or bool(match.group("regex")),
330+
col=col,
331+
)
323332
)
324333
return matchers
325334

326335

327336
def extract_output_matchers_from_out(out: str, params: Mapping[str, Any], regex: bool) -> List[OutputMatcher]:
337+
"""Transform output lines such as 'function:9: E: message'
338+
339+
The result is a list of output matchers
340+
"""
328341
matchers = []
329342
for line in pystache.render(out, params).split("\n"):
330343
match = re.search(

0 commit comments

Comments
 (0)