Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable flake8-pytest-style #2822

Merged
merged 37 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
482446d
WIP enable flake8-pytest-style
CoolCat467 Oct 19, 2023
1e429c3
Merge branch 'master' into enable-flake8-pytest-style
CoolCat467 Oct 24, 2023
79f0fc4
Merge branch 'master' into enable-flake8-pytest-style
CoolCat467 Oct 25, 2023
a99aaf2
Merge branch 'master' into enable-flake8-pytest-style
CoolCat467 Oct 31, 2023
29f9ddc
Merge branch 'master' into enable-flake8-pytest-style
CoolCat467 Nov 10, 2023
09f5954
Autofixes
CoolCat467 Nov 10, 2023
2d56d57
Add todo match arguments
CoolCat467 Nov 11, 2023
5ef2fe6
WIP replace match todos
CoolCat467 Nov 11, 2023
331b85b
WIP match todos
CoolCat467 Nov 11, 2023
f4cef61
Update trio/_core/_tests/test_instrumentation.py
CoolCat467 Nov 11, 2023
ff666ab
Apply suggestions from code review
CoolCat467 Nov 11, 2023
2517bc4
Merge remote-tracking branch 'origin/master' into enable-flake8-pytes…
CoolCat467 Nov 11, 2023
2ed3285
Merge branch 'master' into enable-flake8-pytest-style
CoolCat467 Nov 17, 2023
d7290cf
Merge branch 'master' into enable-flake8-pytest-style
CoolCat467 Nov 26, 2023
ed1976e
Replace more TODO matches
CoolCat467 Nov 26, 2023
c4bf0c6
More match argument fixing
CoolCat467 Nov 26, 2023
ec43741
More match argument fixing
CoolCat467 Nov 26, 2023
858037a
Fix more match TODOs
CoolCat467 Dec 1, 2023
958f963
Merge branch 'master' into enable-flake8-pytest-style
CoolCat467 Dec 1, 2023
adddb54
Fix remaining ruff issues
CoolCat467 Dec 1, 2023
0cb1b98
Fix a few remaining windows match TODOs
CoolCat467 Dec 1, 2023
e89633e
Add start and end tags to some of the match regexes
CoolCat467 Dec 1, 2023
c4fc60d
Fix regexes
CoolCat467 Dec 1, 2023
21807ef
Fix more regex errors
CoolCat467 Dec 1, 2023
92fb1f1
Fix non-escaped `...`
CoolCat467 Dec 1, 2023
3cbcf4a
Fix unbalanced parenthesis
CoolCat467 Dec 1, 2023
1fc842a
Merge branch 'master' into enable-flake8-pytest-style
CoolCat467 Dec 1, 2023
f89cd0a
Apply suggestions from code review
CoolCat467 Dec 1, 2023
ba539d7
Fix `is` check
CoolCat467 Dec 1, 2023
77cd609
Merge branch 'master' into enable-flake8-pytest-style
CoolCat467 Dec 3, 2023
e054523
Fix new changes
CoolCat467 Dec 3, 2023
c683a7d
Add comments and simplify `test_socket.py`
CoolCat467 Dec 6, 2023
b6c1ea0
Merge branch 'master' into enable-flake8-pytest-style
CoolCat467 Dec 6, 2023
33b6894
fix coverage fail - unrelated to this PR
jakkdl Dec 9, 2023
dbad60c
try coaxing CI to run
jakkdl Dec 9, 2023
b729216
pytest.raises(match=) makes assertion irrelevant
jakkdl Dec 9, 2023
7a8f3ab
revert test_highlevel_open_tcp_listeners fix, for another PR
jakkdl Dec 9, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

I also like the setting that forces using a sequence of strings in parametrize rather than a comma-separated string...


[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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PT012 is a bit of a pain when trio so often tests context managers, so not entirely sure it's worth having enabled. It did catch a couple ugly cases this one time though.

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