Skip to content

Commit f1cb241

Browse files
CoolCat467webknjazjakkdl
authored
Enable flake8-pytest-style (#2822)
* WIP enable flake8-pytest-style * Update trio/_core/_tests/test_instrumentation.py Co-authored-by: Sviatoslav Sydorenko <[email protected]> * Apply suggestions from code review Co-authored-by: Sviatoslav Sydorenko <[email protected]> * Multiple parts of adding match argument * Fix ruff issues * Fix a few remaining windows match argument issues * Add start and end tags to some of the match regexes * Fix regexes * Fix more regex errors * Fix non-escaped `...` * Fix unbalanced parenthesis * Apply suggestions from code review Co-authored-by: jakkdl <[email protected]> * Fix `is` check * Fix new changes from merging * Add comments and simplify `test_socket.py` * fix coverage fail - unrelated to this PR * try coaxing CI to run * pytest.raises(match=) makes assertion irrelevant * revert test_highlevel_open_tcp_listeners fix, for another PR --------- Co-authored-by: Sviatoslav Sydorenko <[email protected]> Co-authored-by: jakkdl <[email protected]> Co-authored-by: jakkdl <[email protected]>
1 parent 597e345 commit f1cb241

35 files changed

+319
-221
lines changed

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ select = [
103103
"PYI", # flake8-pyi
104104
"SIM", # flake8-simplify
105105
"TCH", # flake8-type-checking
106+
"PT", # flake8-pytest-style
106107
]
107108
extend-ignore = [
108109
'F403', # undefined-local-with-import-star
@@ -131,6 +132,9 @@ extend-exclude = [
131132
[tool.ruff.isort]
132133
combine-as-imports = true
133134

135+
[tool.ruff.flake8-pytest-style]
136+
fixture-parentheses = false
137+
134138
[tool.mypy]
135139
python_version = "3.8"
136140

src/trio/_core/_tests/test_guest_mode.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import pytest
2727
from outcome import Outcome
28-
from pytest import MonkeyPatch, WarningsRecorder
2928

3029
import trio
3130
import trio.testing
@@ -171,17 +170,16 @@ async def early_task() -> None:
171170
assert res == "ok"
172171
assert set(record) == {"system task ran", "main task ran", "run_sync_soon cb ran"}
173172

173+
class BadClock:
174+
def start_clock(self) -> NoReturn:
175+
raise ValueError("whoops")
176+
177+
def after_start_never_runs() -> None: # pragma: no cover
178+
pytest.fail("shouldn't get here")
179+
174180
# Errors during initialization (which can only be TrioInternalErrors)
175181
# are raised out of start_guest_run, not out of the done_callback
176182
with pytest.raises(trio.TrioInternalError):
177-
178-
class BadClock:
179-
def start_clock(self) -> NoReturn:
180-
raise ValueError("whoops")
181-
182-
def after_start_never_runs() -> None: # pragma: no cover
183-
pytest.fail("shouldn't get here")
184-
185183
trivial_guest_run(
186184
trio_main, clock=BadClock(), in_host_after_start=after_start_never_runs
187185
)
@@ -527,7 +525,7 @@ async def aio_pingpong(
527525

528526

529527
def test_guest_mode_internal_errors(
530-
monkeypatch: MonkeyPatch, recwarn: WarningsRecorder
528+
monkeypatch: pytest.MonkeyPatch, recwarn: pytest.WarningsRecorder
531529
) -> None:
532530
with monkeypatch.context() as m:
533531

src/trio/_core/_tests/test_instrumentation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def after_run(self) -> NoReturn:
255255
raise ValueError("oops")
256256

257257
async def main() -> None:
258-
with pytest.raises(ValueError):
258+
with pytest.raises(ValueError, match="^oops$"):
259259
_core.add_instrument(EvilInstrument())
260260

261261
# Make sure the instrument is fully removed from the per-method lists

src/trio/_core/_tests/test_io.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,10 @@ async def test_wait_on_invalid_object() -> None:
319319
fileno = s.fileno()
320320
# We just closed the socket and don't do anything else in between, so
321321
# we can be confident that the fileno hasn't be reassigned.
322-
with pytest.raises(OSError):
322+
with pytest.raises(
323+
OSError,
324+
match=r"^\[\w+ \d+] (Bad file descriptor|An operation was attempted on something that is not a socket)$",
325+
):
323326
await wait(fileno)
324327

325328

src/trio/_core/_tests/test_local.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ async def reset_check() -> None:
5757
t2.reset(token2)
5858
assert t2.get() == "dogfish"
5959

60-
with pytest.raises(ValueError):
60+
with pytest.raises(ValueError, match="^token has already been used$"):
6161
t2.reset(token2)
6262

6363
token3 = t3.set("basculin")
6464
assert t3.get() == "basculin"
6565

66-
with pytest.raises(ValueError):
66+
with pytest.raises(ValueError, match="^token is not for us$"):
6767
t1.reset(token3)
6868

6969
run(reset_check)

src/trio/_core/_tests/test_mock_clock.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ def test_mock_clock() -> None:
2020
assert c.current_time() == 0
2121
c.jump(1.2)
2222
assert c.current_time() == 1.2
23-
with pytest.raises(ValueError):
23+
with pytest.raises(ValueError, match="^time can't go backwards$"):
2424
c.jump(-1)
2525
assert c.current_time() == 1.2
2626
assert c.deadline_to_sleep_time(1.1) == 0
2727
assert c.deadline_to_sleep_time(1.2) == 0
2828
assert c.deadline_to_sleep_time(1.3) > 999999
2929

30-
with pytest.raises(ValueError):
30+
with pytest.raises(ValueError, match="^rate must be >= 0$"):
3131
c.rate = -1
3232
assert c.rate == 0
3333

src/trio/_core/_tests/test_multierror.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ async def test_MultiErrorNotHashable() -> None:
136136
assert exc1 != exc2
137137
assert exc1 != exc3
138138

139-
with pytest.raises(MultiError):
139+
with pytest.raises(MultiError): # noqa: PT012
140140
async with open_nursery() as nursery:
141141
nursery.start_soon(raise_nothashable, 42)
142142
nursery.start_soon(raise_nothashable, 4242)
@@ -331,36 +331,35 @@ def simple_filter(exc):
331331
assert new_m.__context__ is None
332332

333333
# check preservation of __cause__ and __context__
334-
v = ValueError()
334+
v = ValueError("waffles are great")
335335
v.__cause__ = KeyError()
336-
with pytest.raises(ValueError) as excinfo:
336+
with pytest.raises(ValueError, match="^waffles are great$") as excinfo:
337337
with pytest.warns(TrioDeprecationWarning), MultiError.catch(lambda exc: exc):
338338
raise v
339339
assert isinstance(excinfo.value.__cause__, KeyError)
340340

341-
v = ValueError()
341+
v = ValueError("mushroom soup")
342342
context = KeyError()
343343
v.__context__ = context
344-
with pytest.raises(ValueError) as excinfo:
344+
with pytest.raises(ValueError, match="^mushroom soup$") as excinfo:
345345
with pytest.warns(TrioDeprecationWarning), MultiError.catch(lambda exc: exc):
346346
raise v
347347
assert excinfo.value.__context__ is context
348348
assert not excinfo.value.__suppress_context__
349349

350350
for suppress_context in [True, False]:
351-
v = ValueError()
351+
v = ValueError("unique text")
352352
context = KeyError()
353353
v.__context__ = context
354354
v.__suppress_context__ = suppress_context
355355
distractor = RuntimeError()
356-
with pytest.raises(ValueError) as excinfo:
357356

358-
def catch_RuntimeError(exc):
359-
if isinstance(exc, RuntimeError):
360-
return None
361-
else:
362-
return exc
357+
def catch_RuntimeError(exc: Exception) -> Exception | None:
358+
if isinstance(exc, RuntimeError):
359+
return None
360+
return exc
363361

362+
with pytest.raises(ValueError, match="^unique text$") as excinfo: # noqa: PT012
364363
with pytest.warns(TrioDeprecationWarning):
365364
with MultiError.catch(catch_RuntimeError):
366365
raise MultiError([v, distractor])

src/trio/_core/_tests/test_parking_lot.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ async def waiter(i: int, lot: ParkingLot) -> None:
7878
)
7979
lot.unpark_all()
8080

81-
with pytest.raises(ValueError):
81+
with pytest.raises(
82+
ValueError, match=r"^Cannot pop a non-integer number of tasks\.$"
83+
):
8284
lot.unpark(count=1.5)
8385

8486

0 commit comments

Comments
 (0)