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 8 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 @@ -107,6 +107,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 @@ -135,6 +136,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
3 changes: 1 addition & 2 deletions 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 @@ -527,7 +526,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 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"):
CoolCat467 marked this conversation as resolved.
Show resolved Hide resolved
_core.add_instrument(EvilInstrument())

# Make sure the instrument is fully removed from the per-method lists
Expand Down
2 changes: 1 addition & 1 deletion trio/_core/_tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ 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="Bad file descriptor"):
CoolCat467 marked this conversation as resolved.
Show resolved Hide resolved
await wait(fileno)


Expand Down
4 changes: 2 additions & 2 deletions 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="TODO: exception text"):
t1.reset(token3)

run(reset_check)
Expand Down
4 changes: 2 additions & 2 deletions 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
6 changes: 3 additions & 3 deletions trio/_core/_tests/test_multierror.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,15 @@
# check preservation of __cause__ and __context__
v = ValueError()
v.__cause__ = KeyError()
with pytest.raises(ValueError) as excinfo:
with pytest.raises(ValueError, match="TODO: exception text") as excinfo:
with pytest.warns(TrioDeprecationWarning), MultiError.catch(lambda exc: exc):
raise v
assert isinstance(excinfo.value.__cause__, KeyError)

v = ValueError()
context = KeyError()
v.__context__ = context
with pytest.raises(ValueError) as excinfo:
with pytest.raises(ValueError, match="TODO: exception text") as excinfo:

Check warning on line 344 in trio/_core/_tests/test_multierror.py

View check run for this annotation

Codecov / codecov/patch

trio/_core/_tests/test_multierror.py#L344

Added line #L344 was not covered by tests
with pytest.warns(TrioDeprecationWarning), MultiError.catch(lambda exc: exc):
raise v
assert excinfo.value.__context__ is context
Expand All @@ -353,7 +353,7 @@
v.__context__ = context
v.__suppress_context__ = suppress_context
distractor = RuntimeError()
with pytest.raises(ValueError) as excinfo:
with pytest.raises(ValueError, match="TODO: exception text") as excinfo:

Check warning on line 356 in trio/_core/_tests/test_multierror.py

View check run for this annotation

Codecov / codecov/patch

trio/_core/_tests/test_multierror.py#L356

Added line #L356 was not covered by tests

def catch_RuntimeError(exc):
if isinstance(exc, RuntimeError):
Expand Down
2 changes: 1 addition & 1 deletion trio/_core/_tests/test_parking_lot.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async def waiter(i: int, lot: ParkingLot) -> None:
)
lot.unpark_all()

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


Expand Down
41 changes: 23 additions & 18 deletions trio/_core/_tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def test_initial_task_error() -> None:
async def main(x: object) -> NoReturn:
raise ValueError(x)

with pytest.raises(ValueError) as excinfo:
with pytest.raises(ValueError, match="17") as excinfo:
_core.run(main, 17)
assert excinfo.value.args == (17,)

Expand Down Expand Up @@ -123,7 +123,7 @@ async def test_nursery_warn_use_async_with() -> None:
async def test_nursery_main_block_error_basic() -> None:
exc = ValueError("whoops")

with pytest.raises(ValueError) as excinfo:
with pytest.raises(ValueError, match="whoops") as excinfo:
async with _core.open_nursery():
raise exc
assert excinfo.value is exc
Expand Down Expand Up @@ -178,7 +178,7 @@ async def main() -> None:
nursery.start_soon(looper)
nursery.start_soon(crasher)

with pytest.raises(ValueError) as excinfo:
with pytest.raises(ValueError, match="argh") as excinfo:
_core.run(main)

assert looper_record == ["cancelled"]
Expand Down Expand Up @@ -224,9 +224,9 @@ async def main() -> None:

async def test_child_crash_wakes_parent() -> None:
async def crasher() -> NoReturn:
raise ValueError
raise ValueError("this is a crash")

with pytest.raises(ValueError):
with pytest.raises(ValueError, match="this is a crash"):
async with _core.open_nursery() as nursery:
nursery.start_soon(crasher)
await sleep_forever()
Expand All @@ -244,7 +244,7 @@ async def child1() -> None:
print("child1 woke")
assert x == 0
print("child1 rescheduling t2")
_core.reschedule(not_none(t2), outcome.Error(ValueError()))
_core.reschedule(not_none(t2), outcome.Error(ValueError("error message")))
print("child1 exit")

async def child2() -> None:
Expand All @@ -253,7 +253,7 @@ async def child2() -> None:
t2 = _core.current_task()
_core.reschedule(not_none(t1), outcome.Value(0))
print("child2 sleep")
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="error message"):
await sleep_forever()
print("child2 successful exit")

Expand Down Expand Up @@ -666,7 +666,8 @@ async def test_unshield_while_cancel_propagating() -> None:
await _core.checkpoint()
finally:
inner.shield = True
assert outer.cancelled_caught and not inner.cancelled_caught
assert outer.cancelled_caught
assert not inner.cancelled_caught


async def test_cancel_unbound() -> None:
Expand Down Expand Up @@ -1023,7 +1024,7 @@ async def test_exc_info() -> None:
seq = Sequencer()

async def child1() -> None:
with pytest.raises(ValueError) as excinfo:
with pytest.raises(ValueError, match="child1") as excinfo:
try:
async with seq(0):
pass # we don't yield until seq(2) below
Expand Down Expand Up @@ -1114,11 +1115,13 @@ async def child() -> None:
except Exception:
await sleep_forever()

with pytest.raises(ValueError) as excinfo:
with pytest.raises(ValueError, match="error text") as excinfo:
async with _core.open_nursery() as nursery:
nursery.start_soon(child)
await wait_all_tasks_blocked()
_core.reschedule(not_none(child_task), outcome.Error(ValueError()))
_core.reschedule(
not_none(child_task), outcome.Error(ValueError("error text"))
)

assert isinstance(excinfo.value.__context__, KeyError)

Expand Down Expand Up @@ -1236,9 +1239,9 @@ async def main() -> None:
# After main exits but before finally cleaning up, callback processed
# normally
token.run_sync_soon(lambda: record.append("sync-cb"))
raise ValueError
raise ValueError("error text")

with pytest.raises(ValueError):
with pytest.raises(ValueError, match="error text"):
_core.run(main)

assert record == ["sync-cb"]
Expand Down Expand Up @@ -2312,7 +2315,7 @@ async def do_a_cancel() -> None:
await sleep_forever()

async def crasher() -> NoReturn:
raise ValueError
raise ValueError("this is a crash")

old_flags = gc.get_debug()
try:
Expand All @@ -2326,7 +2329,7 @@ async def crasher() -> NoReturn:
# (See https://github.com/python-trio/trio/pull/1864)
await do_a_cancel()

with pytest.raises(ValueError):
with pytest.raises(ValueError, match="this is a crash"):
async with _core.open_nursery() as nursery:
# cover NurseryManager.__aexit__
nursery.start_soon(crasher)
Expand All @@ -2346,11 +2349,13 @@ async def test_cancel_scope_exit_doesnt_create_cyclic_garbage() -> None:
gc.collect()

async def crasher() -> NoReturn:
raise ValueError
raise ValueError("this is a crash")

old_flags = gc.get_debug()
try:
with pytest.raises(ValueError), _core.CancelScope() as outer:
with pytest.raises(
ValueError, match="this is a crash"
), _core.CancelScope() as outer:
async with _core.open_nursery() as nursery:
gc.collect()
gc.set_debug(gc.DEBUG_SAVEALL)
Expand Down Expand Up @@ -2583,7 +2588,7 @@ async def start_raiser() -> None:
) from None
raise

with pytest.raises(BaseException) as exc_info:
with pytest.raises(BaseException) as exc_info: # noqa: PT011 # no `match`
_core.run(start_raiser, strict_exception_groups=run_strict)

if start_raiser_strict or (run_strict and start_raiser_strict is None):
Expand Down
4 changes: 2 additions & 2 deletions trio/_core/_tests/test_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_winerror(monkeypatch: pytest.MonkeyPatch) -> None:
mock.reset_mock()

mock.return_value = (12, "test error")
with pytest.raises(OSError) as exc:
with pytest.raises(OSError, match="TODO: exception text") as exc:
raise_winerror(filename="file_1", filename2="file_2")
mock.assert_called_once_with()
mock.reset_mock()
Expand All @@ -63,7 +63,7 @@ def test_winerror(monkeypatch: pytest.MonkeyPatch) -> None:
assert exc.value.filename2 == "file_2"

# With an explicit number passed in, it overrides what getwinerror() returns.
with pytest.raises(OSError) as exc:
with pytest.raises(OSError, match="TODO: exception text") as exc:
raise_winerror(18, filename="a/file", filename2="b/file")
mock.assert_called_once_with(18)
mock.reset_mock()
Expand Down
2 changes: 1 addition & 1 deletion trio/_tests/test_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
async def test_channel() -> None:
with pytest.raises(TypeError):
open_memory_channel(1.0)
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="TODO: exception text"):
open_memory_channel(-1)

s, r = open_memory_channel[Union[int, str, None]](2)
Expand Down
4 changes: 2 additions & 2 deletions trio/_tests/test_dtls.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async def test_smoke(ipv6: bool) -> None:
await client_channel.send(b"goodbye")
assert await client_channel.receive() == b"goodbye"

with pytest.raises(ValueError):
with pytest.raises(ValueError, match="TODO: exception text"):
await client_channel.send(b"")

client_channel.set_ciphertext_mtu(1234)
Expand Down Expand Up @@ -289,7 +289,7 @@ async def null_handler(_: object) -> None: # pragma: no cover

async def test_dtls_over_dgram_only() -> None:
with trio.socket.socket() as s:
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="TODO: exception text"):
DTLSEndpoint(s)


Expand Down
10 changes: 6 additions & 4 deletions trio/_tests/test_exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def iter_modules(
# they might be using a newer version of Python with additional symbols which
# won't be reflected in trio.socket, and this shouldn't cause downstream test
# runs to start failing.
@pytest.mark.redistributors_should_skip
@pytest.mark.redistributors_should_skip()
# Static analysis tools often have trouble with alpha releases, where Python's
# internals are in flux, grammar may not have settled down, etc.
@pytest.mark.skipif(
Expand Down Expand Up @@ -192,7 +192,8 @@ def no_underscores(symbols: Iterable[str]) -> set[str]:
else:
mod_cache = trio_cache / (modname + ".data.json")

assert mod_cache.exists() and mod_cache.is_file()
assert mod_cache.exists()
assert mod_cache.is_file()
with mod_cache.open() as cache_file:
cache_json = json.loads(cache_file.read())
static_names = no_underscores(
Expand Down Expand Up @@ -267,7 +268,7 @@ def no_underscores(symbols: Iterable[str]) -> set[str]:
# modules, instead of once per class.
@slow
# see comment on test_static_tool_sees_all_symbols
@pytest.mark.redistributors_should_skip
@pytest.mark.redistributors_should_skip()
# Static analysis tools often have trouble with alpha releases, where Python's
# internals are in flux, grammar may not have settled down, etc.
@pytest.mark.skipif(
Expand Down Expand Up @@ -314,7 +315,8 @@ def no_hidden(symbols: Iterable[str]) -> set[str]:
else:
mod_cache = trio_cache / (modname + ".data.json")

assert mod_cache.exists() and mod_cache.is_file()
assert mod_cache.exists()
assert mod_cache.is_file()
with mod_cache.open() as cache_file:
cache_json = json.loads(cache_file.read())

Expand Down
2 changes: 1 addition & 1 deletion trio/_tests/test_fakenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async def test_basic_udp() -> None:
assert ip == "127.0.0.1"
assert port != 0

with pytest.raises(OSError) as exc: # Cannot rebind.
with pytest.raises(OSError, match="TODO: exception text") as exc: # Cannot rebind.
await s1.bind(("192.0.2.1", 0))
assert exc.value.errno == errno.EINVAL

Expand Down
2 changes: 1 addition & 1 deletion trio/_tests/test_highlevel_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ async def aclose(self) -> NoReturn:

stapled = StapledStream(BrokenSendStream(), BrokenReceiveStream())

with pytest.raises(ValueError) as excinfo:
with pytest.raises(ValueError, match="TODO: exception text") as excinfo:
await stapled.aclose()
assert isinstance(excinfo.value.__context__, ValueError)

Expand Down
Loading
Loading