Skip to content

Commit dd1d5e2

Browse files
committed
fix(frontend): keep warnings about analysed sources off the analyzer's stderr
Closes #241
1 parent aa0871d commit dd1d5e2

2 files changed

Lines changed: 66 additions & 1 deletion

File tree

‎src/coretrace_python/frontend/parser.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import ast
4+
import warnings
45

56
from coretrace_python.source import SourceFile
67

@@ -11,7 +12,12 @@ class ParseError(Exception):
1112

1213
def parse_source(source: str, filename: str = "<unknown>") -> ast.Module:
1314
try:
14-
return ast.parse(source, filename=filename)
15+
with warnings.catch_warnings():
16+
# A warning raised while parsing (an invalid escape sequence: a SyntaxWarning
17+
# from 3.12, a DeprecationWarning on 3.11) is about the analysed source, not
18+
# the analyzer, and would reach its stderr once per process that parses (#241).
19+
warnings.simplefilter("ignore")
20+
return ast.parse(source, filename=filename)
1521
except SyntaxError as error:
1622
line = error.lineno or 0
1723
column = error.offset or 0

‎tests/test_parse_warnings.py‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""Acceptance tests for issue #241: a warning Python raises while parsing an analysed
2+
source is about that source, not about the analyzer, and never reaches its stderr.
3+
4+
An invalid escape sequence is a ``SyntaxWarning`` from Python 3.12 on and a
5+
``DeprecationWarning`` on 3.11. With ``-j N`` the parent and a worker both parse every
6+
module, so an unfiltered warning was printed twice.
7+
"""
8+
9+
from __future__ import annotations
10+
11+
import subprocess
12+
import sys
13+
import warnings
14+
from pathlib import Path
15+
16+
import pytest
17+
18+
from coretrace_python.frontend.parser import parse_source
19+
20+
INVALID_ESCAPE = 'import re\n\nPATTERN = re.compile("\\d+")\n'
21+
22+
23+
def test_parsing_raises_no_warning_about_the_analysed_source() -> None:
24+
with warnings.catch_warnings(record=True) as caught:
25+
warnings.simplefilter("always")
26+
tree = parse_source(INVALID_ESCAPE, "escapes.py")
27+
28+
assert caught == []
29+
assert len(tree.body) == 2
30+
31+
32+
def test_parsing_leaves_the_warning_filters_of_the_process_alone() -> None:
33+
with warnings.catch_warnings(record=True) as caught:
34+
warnings.simplefilter("always")
35+
parse_source(INVALID_ESCAPE, "escapes.py")
36+
warnings.warn("raised by the analyzer itself", SyntaxWarning, stacklevel=1)
37+
38+
assert [str(warning.message) for warning in caught] == ["raised by the analyzer itself"]
39+
40+
41+
@pytest.mark.parametrize("jobs", ["1", "2"])
42+
def test_check_prints_nothing_on_stderr_about_the_analysed_sources(
43+
tmp_path: Path, jobs: str
44+
) -> None:
45+
root = tmp_path / "project"
46+
root.mkdir()
47+
(root / "escapes.py").write_text(INVALID_ESCAPE, encoding="utf-8")
48+
(root / "other.py").write_text("def run():\n return 1\n", encoding="utf-8")
49+
50+
checked = subprocess.run(
51+
[sys.executable, "-m", "coretrace_python", "--check", str(root), "-j", jobs],
52+
capture_output=True,
53+
text=True,
54+
check=False,
55+
)
56+
57+
assert checked.stderr == ""
58+
assert checked.stdout == "no findings\ncoverage: 2/2 files, 2/2 functions\n"
59+
assert checked.returncode == 0

0 commit comments

Comments
 (0)