Skip to content

Commit 56a19ea

Browse files
Merge pull request #1548 from VWS-Python/faster-lint
Make custom linting way faster by using pytest without a subprocess
2 parents 1a10c92 + 1ec122c commit 56a19ea

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

ci/custom_linters.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Custom lint tests.
33
"""
44

5-
import asyncio
65
from pathlib import Path
76

87
import pytest
@@ -23,20 +22,23 @@ def _ci_patterns() -> set[str]:
2322
return ci_patterns
2423

2524

26-
async def _tests_from_pattern(ci_pattern: str) -> set[str]:
25+
def _tests_from_pattern(
26+
ci_pattern: str,
27+
capsys: pytest.CaptureFixture[str],
28+
) -> set[str]:
2729
"""
2830
From a CI pattern, get all tests ``pytest`` would collect.
2931
"""
32+
# Clear the captured output.
33+
capsys.readouterr()
3034
tests: set[str] = set()
31-
args = ["pytest", "-q", "--collect-only", ci_pattern]
32-
process = await asyncio.create_subprocess_exec(
33-
*args,
34-
stdout=asyncio.subprocess.PIPE,
35-
)
36-
data, _ = await process.communicate()
35+
pytest.main(args=["-q", "--collect-only", ci_pattern])
36+
data = capsys.readouterr().out
3737
for line in data.splitlines():
38-
if line and b"collected in" not in line:
39-
tests.add(line.decode())
38+
# We filter empty lines and lines which look like
39+
# "9 tests collected in 0.01s".
40+
if line and "collected in" not in line:
41+
tests.add(line)
4042
return tests
4143

4244

@@ -54,22 +56,19 @@ def test_ci_patterns_valid() -> None:
5456
assert collect_only_result == 0, message
5557

5658

57-
@pytest.mark.asyncio()
58-
async def test_tests_collected_once() -> None:
59+
def test_tests_collected_once(
60+
capsys: pytest.CaptureFixture[str],
61+
) -> None:
5962
"""
6063
Each test in the test suite is collected exactly once.
6164
6265
This does not necessarily mean that they are run - they may be skipped.
6366
"""
6467
ci_patterns = _ci_patterns()
6568
tests_to_patterns: dict[str, set[str]] = {}
66-
tasks = []
67-
for pattern in ci_patterns:
68-
tasks.append(_tests_from_pattern(ci_pattern=pattern))
6969

70-
results = await asyncio.gather(*tasks)
71-
for index, pattern in enumerate(ci_patterns):
72-
tests = results[index]
70+
for pattern in ci_patterns:
71+
tests = _tests_from_pattern(ci_pattern=pattern, capsys=capsys)
7372
for test in tests:
7473
if test in tests_to_patterns:
7574
tests_to_patterns[test].add(pattern)
@@ -84,6 +83,6 @@ async def test_tests_collected_once() -> None:
8483
)
8584
assert len(patterns) == 1, message
8685

87-
all_tests = await _tests_from_pattern(ci_pattern=".")
86+
all_tests = _tests_from_pattern(ci_pattern=".", capsys=capsys)
8887
assert tests_to_patterns.keys() - all_tests == set()
8988
assert all_tests - tests_to_patterns.keys() == set()

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ dev = [
296296
"pylint==2.17.2",
297297
"pyright==1.1.301",
298298
"pyroma==4.2",
299-
"pytest-asyncio==0.21.0",
300299
"pytest-cov==4.0.0",
301300
"pytest==7.2.2",
302301
"requests-mock-flask==2023.3.5.1",

0 commit comments

Comments
 (0)