Skip to content

Commit

Permalink
Enable flake8-pytest-style (python-trio#2822)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
4 people authored Dec 13, 2023
1 parent 597e345 commit f1cb241
Show file tree
Hide file tree
Showing 35 changed files with 319 additions and 221 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ select = [
"PYI", # flake8-pyi
"SIM", # flake8-simplify
"TCH", # flake8-type-checking
"PT", # flake8-pytest-style
]
extend-ignore = [
'F403', # undefined-local-with-import-star
Expand Down Expand Up @@ -131,6 +132,9 @@ extend-exclude = [
[tool.ruff.isort]
combine-as-imports = true

[tool.ruff.flake8-pytest-style]
fixture-parentheses = false

[tool.mypy]
python_version = "3.8"

Expand Down
18 changes: 8 additions & 10 deletions src/trio/_core/_tests/test_guest_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import pytest
from outcome import Outcome
from pytest import MonkeyPatch, WarningsRecorder

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

class BadClock:
def start_clock(self) -> NoReturn:
raise ValueError("whoops")

def after_start_never_runs() -> None: # pragma: no cover
pytest.fail("shouldn't get here")

# Errors during initialization (which can only be TrioInternalErrors)
# are raised out of start_guest_run, not out of the done_callback
with pytest.raises(trio.TrioInternalError):

class BadClock:
def start_clock(self) -> NoReturn:
raise ValueError("whoops")

def after_start_never_runs() -> None: # pragma: no cover
pytest.fail("shouldn't get here")

trivial_guest_run(
trio_main, clock=BadClock(), in_host_after_start=after_start_never_runs
)
Expand Down Expand Up @@ -527,7 +525,7 @@ async def aio_pingpong(


def test_guest_mode_internal_errors(
monkeypatch: MonkeyPatch, recwarn: WarningsRecorder
monkeypatch: pytest.MonkeyPatch, recwarn: pytest.WarningsRecorder
) -> None:
with monkeypatch.context() as m:

Expand Down
2 changes: 1 addition & 1 deletion src/trio/_core/_tests/test_instrumentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def after_run(self) -> NoReturn:
raise ValueError("oops")

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

# Make sure the instrument is fully removed from the per-method lists
Expand Down
5 changes: 4 additions & 1 deletion src/trio/_core/_tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,10 @@ async def test_wait_on_invalid_object() -> None:
fileno = s.fileno()
# We just closed the socket and don't do anything else in between, so
# we can be confident that the fileno hasn't be reassigned.
with pytest.raises(OSError):
with pytest.raises(
OSError,
match=r"^\[\w+ \d+] (Bad file descriptor|An operation was attempted on something that is not a socket)$",
):
await wait(fileno)


Expand Down
4 changes: 2 additions & 2 deletions src/trio/_core/_tests/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ async def reset_check() -> None:
t2.reset(token2)
assert t2.get() == "dogfish"

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

token3 = t3.set("basculin")
assert t3.get() == "basculin"

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

run(reset_check)
Expand Down
4 changes: 2 additions & 2 deletions src/trio/_core/_tests/test_mock_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ def test_mock_clock() -> None:
assert c.current_time() == 0
c.jump(1.2)
assert c.current_time() == 1.2
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="^time can't go backwards$"):
c.jump(-1)
assert c.current_time() == 1.2
assert c.deadline_to_sleep_time(1.1) == 0
assert c.deadline_to_sleep_time(1.2) == 0
assert c.deadline_to_sleep_time(1.3) > 999999

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

Expand Down
23 changes: 11 additions & 12 deletions src/trio/_core/_tests/test_multierror.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ async def test_MultiErrorNotHashable() -> None:
assert exc1 != exc2
assert exc1 != exc3

with pytest.raises(MultiError):
with pytest.raises(MultiError): # noqa: PT012
async with open_nursery() as nursery:
nursery.start_soon(raise_nothashable, 42)
nursery.start_soon(raise_nothashable, 4242)
Expand Down Expand Up @@ -331,36 +331,35 @@ def simple_filter(exc):
assert new_m.__context__ is None

# check preservation of __cause__ and __context__
v = ValueError()
v = ValueError("waffles are great")
v.__cause__ = KeyError()
with pytest.raises(ValueError) as excinfo:
with pytest.raises(ValueError, match="^waffles are great$") as excinfo:
with pytest.warns(TrioDeprecationWarning), MultiError.catch(lambda exc: exc):
raise v
assert isinstance(excinfo.value.__cause__, KeyError)

v = ValueError()
v = ValueError("mushroom soup")
context = KeyError()
v.__context__ = context
with pytest.raises(ValueError) as excinfo:
with pytest.raises(ValueError, match="^mushroom soup$") as excinfo:
with pytest.warns(TrioDeprecationWarning), MultiError.catch(lambda exc: exc):
raise v
assert excinfo.value.__context__ is context
assert not excinfo.value.__suppress_context__

for suppress_context in [True, False]:
v = ValueError()
v = ValueError("unique text")
context = KeyError()
v.__context__ = context
v.__suppress_context__ = suppress_context
distractor = RuntimeError()
with pytest.raises(ValueError) as excinfo:

def catch_RuntimeError(exc):
if isinstance(exc, RuntimeError):
return None
else:
return exc
def catch_RuntimeError(exc: Exception) -> Exception | None:
if isinstance(exc, RuntimeError):
return None
return exc

with pytest.raises(ValueError, match="^unique text$") as excinfo: # noqa: PT012
with pytest.warns(TrioDeprecationWarning):
with MultiError.catch(catch_RuntimeError):
raise MultiError([v, distractor])
Expand Down
4 changes: 3 additions & 1 deletion src/trio/_core/_tests/test_parking_lot.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ async def waiter(i: int, lot: ParkingLot) -> None:
)
lot.unpark_all()

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


Expand Down
Loading

0 comments on commit f1cb241

Please sign in to comment.