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

Ruff preview mode #3024

Merged
merged 37 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
1cca9b9
Enable ruff preview mode
CoolCat467 Aug 4, 2024
12589fc
Automatic preview fixes
CoolCat467 Aug 4, 2024
03c50ed
Manually-reviewed preview unsafe autofixes
CoolCat467 Aug 4, 2024
240d050
Manually fix a few trivial issues
CoolCat467 Aug 4, 2024
78c6c74
Ignore `PYI063` and add comments everywhere
CoolCat467 Aug 4, 2024
4061f6a
Manual fixes
CoolCat467 Aug 4, 2024
155b6d1
Fix a bunch of test issues
CoolCat467 Aug 4, 2024
65cf29d
More test fixes
CoolCat467 Aug 4, 2024
10c7926
Fix missing await
CoolCat467 Aug 4, 2024
9b3b6a2
Fix missing async marker
CoolCat467 Aug 4, 2024
997016e
Merge branch 'main' into ruff-preview-mode
CoolCat467 Aug 21, 2024
c545d72
Change to not use pi to avoid FURB152 triggering
CoolCat467 Aug 22, 2024
0a41c88
Parenthesis are not required
CoolCat467 Aug 22, 2024
06bcfc6
Use `Path` to read and write files instead of doing it manually
CoolCat467 Aug 22, 2024
4776a76
Use `extend` instead of repeated `append`
CoolCat467 Aug 22, 2024
1dc61f3
FURB131 Prefer `clear` over deleting a full slice
CoolCat467 Aug 22, 2024
b0f273c
UP031 Use format specifiers instead of percent format
CoolCat467 Aug 22, 2024
37a0f28
A005 Module is shadowing python standard library
CoolCat467 Aug 22, 2024
240c1b4
A004 Ignore exceptiongroup imports shadowing builtins
CoolCat467 Aug 22, 2024
b2d9c5b
Merge branch 'main' into ruff-preview-mode
CoolCat467 Aug 22, 2024
3eb8328
Add type annotation for `out`
CoolCat467 Aug 22, 2024
8f0ae46
Merge remote-tracking branch 'origin/main' into ruff-preview-mode
CoolCat467 Sep 20, 2024
7dd9ae5
I don't think `newline` should be required
CoolCat467 Sep 20, 2024
0c53be9
Add missing explain comment
CoolCat467 Sep 20, 2024
198e641
Remove duplicate overload I somehow introduced
CoolCat467 Sep 20, 2024
8588018
Update to ruff 0.6.6
CoolCat467 Sep 20, 2024
ad10b07
Merge branch 'main' into ruff-preview-mode
CoolCat467 Sep 24, 2024
d2a4031
Disable RUF029 in test files
CoolCat467 Sep 24, 2024
924b6dd
Fix several things from review
CoolCat467 Sep 24, 2024
a38f7f6
Fix more remnants of old that got removed
CoolCat467 Sep 24, 2024
fe334dc
Ignore `A005`
CoolCat467 Sep 24, 2024
287d690
Merge remote-tracking branch 'origin/main' into ruff-preview-mode
CoolCat467 Oct 2, 2024
d8ffd87
Revert formatting changes from prior noqa lines that exist no longer
CoolCat467 Oct 2, 2024
31d416d
Revert other formatting in same files
CoolCat467 Oct 2, 2024
0aaa21f
Merge remote-tracking branch 'origin/main' into ruff-preview-mode
CoolCat467 Oct 7, 2024
03d4061
Merge remote-tracking branch 'origin/main' into ruff-preview-mode
CoolCat467 Oct 9, 2024
ded5cac
Merge branch 'main' into ruff-preview-mode
CoolCat467 Oct 17, 2024
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ repos:
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.5
rev: v0.6.6
hooks:
- id: ruff
types: [file]
Expand Down
9 changes: 6 additions & 3 deletions notes-to-self/how-does-windows-so-reuseaddr-work.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@ def table_entry(mode1, bind_type1, mode2, bind_type2):
"""
second bind
| """
+ " | ".join(["%-19s" % mode for mode in modes]),
+ " | ".join(f"{mode:<19}" for mode in modes),
)

print(""" """, end="")
for _ in modes:
print(" | " + " | ".join(["%8s" % bind_type for bind_type in bind_types]), end="")
print(
" | " + " | ".join(f"{bind_type:>8}" for bind_type in bind_types),
end="",
)

print(
"""
Expand All @@ -72,5 +75,5 @@ def table_entry(mode1, bind_type1, mode2, bind_type2):
# print(mode1, bind_type1, mode2, bind_type2, entry)
print(
f"{mode1:>19} | {bind_type1:>8} | "
+ " | ".join(["%8s" % entry for entry in row]),
+ " | ".join(f"{entry:>8}" for entry in row),
)
2 changes: 1 addition & 1 deletion notes-to-self/socket-scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def pt(desc, *, count=total, item="socket"):
now = time.perf_counter()
total_ms = (now - last_time) * 1000
per_us = total_ms * 1000 / count
print(f"{desc}: {total_ms:.2f} ms total, {per_us:.2f} µs/{item}")
print(f"{desc}: {total_ms:.2f} ms total, {per_us:.2f} μs/{item}")
last_time = now

print(f"\n-- {total} sockets --")
Expand Down
2 changes: 1 addition & 1 deletion notes-to-self/thread-dispatch-bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def main():
in_q.put(lambda: None)
out_q.get()
end = time.monotonic()
print(f"{(end - start) / COUNT * 1e6:.2f} µs/job")
print(f"{(end - start) / COUNT * 1e6:.2f} μs/job")


main()
6 changes: 3 additions & 3 deletions notes-to-self/trivial-err.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
sys.stderr = sys.stdout


async def child1():
async def child1(): # noqa: RUF029 # async not required
raise ValueError


Expand All @@ -15,11 +15,11 @@ async def child2():
nursery.start_soon(grandchild2)


async def grandchild1():
async def grandchild1(): # noqa: RUF029 # async not required
raise KeyError


async def grandchild2():
async def grandchild2(): # noqa: RUF029 # async not required
raise NameError("Bob")


Expand Down
2 changes: 1 addition & 1 deletion notes-to-self/trivial.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import trio


async def foo():
async def foo(): # noqa: RUF029 # await not used
print("in foo!")
return 3

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ extend-exclude = [
]

[tool.ruff.lint]
preview = true
allowed-confusables = ["–"]

select = [
Expand Down
3 changes: 3 additions & 0 deletions src/trio/_abc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# ruff: noqa: A005
# Module `_abc` is shadowing a Python builtin module

from __future__ import annotations

import socket
Expand Down
6 changes: 4 additions & 2 deletions src/trio/_core/_entry_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ def run_cb(job: Job) -> None:
sync_fn(*args)
except BaseException as exc:

async def kill_everything(exc: BaseException) -> NoReturn:
async def kill_everything( # noqa: RUF029 # await not used
exc: BaseException,
) -> NoReturn:
raise exc

try:
Expand Down Expand Up @@ -139,7 +141,7 @@ def run_sync_soon(
# wakeup call might trigger an OSError b/c the IO manager has
# already been shut down.
if idempotent:
self.idempotent_queue[(sync_fn, args)] = None
self.idempotent_queue[sync_fn, args] = None
else:
self.queue.append((sync_fn, args))
self.wakeup.wakeup_thread_and_signal_safe()
Expand Down
15 changes: 9 additions & 6 deletions src/trio/_core/_tests/test_asyncgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ async def agen() -> AsyncGenerator[int, None]:
await _core.wait_all_tasks_blocked()
assert record == ["crashing"]
# Following type ignore is because typing for LogCaptureFixture is wrong
exc_type, exc_value, exc_traceback = caplog.records[0].exc_info # type: ignore[misc]
exc_type, exc_value, _exc_traceback = caplog.records[0].exc_info # type: ignore[misc]
assert exc_type is ValueError
assert str(exc_value) == "oops"
assert "during finalization of async generator" in caplog.records[0].message
Expand Down Expand Up @@ -189,7 +189,8 @@ def test_last_minute_gc_edge_case() -> None:
record = []
needs_retry = True

async def agen() -> AsyncGenerator[int, None]:
# async function missing await
async def agen() -> AsyncGenerator[int, None]: # noqa: RUF029
try:
yield 1
finally:
Expand Down Expand Up @@ -227,8 +228,8 @@ async def async_main() -> None:
# failure as small as we want.
for _attempt in range(50):
needs_retry = False
del record[:]
del saved[:]
record.clear()
saved.clear()
_core.run(async_main)
if needs_retry: # pragma: no cover
assert record == ["cleaned up"]
Expand Down Expand Up @@ -270,10 +271,12 @@ def abort_fn(_: _core.RaiseCancelT) -> _core.Abort:
async def test_fallback_when_no_hook_claims_it(
capsys: pytest.CaptureFixture[str],
) -> None:
async def well_behaved() -> AsyncGenerator[int, None]:
# async function missing await
async def well_behaved() -> AsyncGenerator[int, None]: # noqa: RUF029
yield 42

async def yields_after_yield() -> AsyncGenerator[int, None]:
# async function missing await
async def yields_after_yield() -> AsyncGenerator[int, None]: # noqa: RUF029
with pytest.raises(GeneratorExit):
yield 42
yield 100
Expand Down
17 changes: 12 additions & 5 deletions src/trio/_core/_tests/test_guest_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ async def trio_return(in_host: InHost) -> str:

assert trivial_guest_run(trio_return) == "ok"

async def trio_fail(in_host: InHost) -> NoReturn:
async def trio_fail( # noqa: RUF029 # async function missing await
in_host: InHost,
) -> NoReturn:
raise KeyError("whoopsiedaisy")

with pytest.raises(KeyError, match="whoopsiedaisy"):
Expand Down Expand Up @@ -254,7 +256,9 @@ async def synchronize() -> None:
def test_warn_set_wakeup_fd_overwrite() -> None:
assert signal.set_wakeup_fd(-1) == -1

async def trio_main(in_host: InHost) -> str:
async def trio_main( # noqa: RUF029 # async function missing await
in_host: InHost,
) -> str:
return "ok"

a, b = socket.socketpair()
Expand Down Expand Up @@ -295,8 +299,10 @@ async def trio_main(in_host: InHost) -> str:
# then it's left alone and there's no warning
signal.set_wakeup_fd(a.fileno())
try:

async def trio_check_wakeup_fd_unaltered(in_host: InHost) -> str:
# async function missing await
async def trio_check_wakeup_fd_unaltered( # noqa: RUF029
in_host: InHost,
) -> str:
fd = signal.set_wakeup_fd(-1)
assert fd == a.fileno()
signal.set_wakeup_fd(fd)
Expand Down Expand Up @@ -594,7 +600,8 @@ async def trio_main(in_host: InHost) -> None:
# Also check chaining in the case where KI is injected after main exits
final_exc = KeyError("whoa")

async def trio_main_raising(in_host: InHost) -> NoReturn:
# async function missing await
async def trio_main_raising(in_host: InHost) -> NoReturn: # noqa: RUF029
in_host(partial(signal_raise, signal.SIGINT))
raise final_exc

Expand Down
8 changes: 4 additions & 4 deletions src/trio/_core/_tests/test_instrumentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def task_spawned(self, task: Task) -> None:
def task_exited(self, task: Task) -> None:
record.append(("exited", task))

async def main() -> Task:
async def main() -> Task: # noqa: RUF029 # async function missing await
return _core.current_task()

main_task = _core.run(main, instruments=[SpawnExitRecorder()])
Expand All @@ -191,7 +191,7 @@ def close(self) -> None:
# works right.
record.append("closed") # pragma: no cover

async def main() -> Task:
async def main() -> Task: # noqa: RUF029 # async function missing await
record.append("main ran")
return _core.current_task()

Expand All @@ -204,7 +204,7 @@ async def main() -> Task:
assert ("after_run", None) in r.record
# And we got a log message
assert caplog.records[0].exc_info is not None
exc_type, exc_value, exc_traceback = caplog.records[0].exc_info
exc_type, exc_value, _exc_traceback = caplog.records[0].exc_info
assert exc_type is ValueError
assert str(exc_value) == "oops"
assert "Instrument has been disabled" in caplog.records[0].message
Expand Down Expand Up @@ -254,7 +254,7 @@ def task_exited(self, task: Task) -> NoReturn:
def after_run(self) -> NoReturn:
raise ValueError("oops")

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

Expand Down
9 changes: 5 additions & 4 deletions src/trio/_core/_tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ async def block_on_write() -> None:

@read_socket_test
async def test_double_read(socketpair: SocketPair, wait_readable: WaitSocket) -> None:
a, b = socketpair
a, _b = socketpair

# You can't have two tasks trying to read from a socket at the same time
async with _core.open_nursery() as nursery:
Expand All @@ -174,7 +174,7 @@ async def test_double_read(socketpair: SocketPair, wait_readable: WaitSocket) ->

@write_socket_test
async def test_double_write(socketpair: SocketPair, wait_writable: WaitSocket) -> None:
a, b = socketpair
a, _b = socketpair

# You can't have two tasks trying to write to a socket at the same time
fill_socket(a)
Expand All @@ -195,7 +195,7 @@ async def test_interrupted_by_close(
wait_writable: WaitSocket,
notify_closing: Callable[[stdlib_socket.socket], object],
) -> None:
a, b = socketpair
a, _b = socketpair

async def reader() -> None:
with pytest.raises(_core.ClosedResourceError):
Expand Down Expand Up @@ -301,7 +301,8 @@ async def receiver(sock: stdlib_socket.socket, key: str) -> None:
assert results["send_b"] == results["recv_a"]


async def test_notify_closing_on_invalid_object() -> None:
# async function missing await
async def test_notify_closing_on_invalid_object() -> None: # noqa: RUF029
# It should either be a no-op (generally on Unix, where we don't know
# which fds are valid), or an OSError (on Windows, where we currently only
# support sockets, so we have to do some validation to figure out whether
Expand Down
21 changes: 13 additions & 8 deletions src/trio/_core/_tests/test_ki.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async def aprotected() -> None:
await aunprotected()

@_core.disable_ki_protection
async def aunprotected() -> None:
async def aunprotected() -> None: # noqa: RUF029 # async fn missing await
assert not _core.currently_ki_protected()

await aprotected()
Expand Down Expand Up @@ -128,7 +128,8 @@ async def child(expected: bool) -> None:

# This also used to be broken due to
# https://bugs.python.org/issue29590
async def test_generator_based_context_manager_throw() -> None:
# async function missing await
async def test_generator_based_context_manager_throw() -> None: # noqa: RUF029
@contextlib.contextmanager
@_core.enable_ki_protection
def protected_manager() -> Iterator[None]:
Expand Down Expand Up @@ -195,16 +196,18 @@ async def agen_unprotected2() -> None:

async def test_native_agen_protection() -> None:
# Native async generators
# async function missing await
@_core.enable_ki_protection
async def agen_protected() -> AsyncIterator[None]:
async def agen_protected() -> AsyncIterator[None]: # noqa: RUF029
assert _core.currently_ki_protected()
try:
yield
finally:
assert _core.currently_ki_protected()

# async function missing await
@_core.disable_ki_protection
async def agen_unprotected() -> AsyncIterator[None]:
async def agen_unprotected() -> AsyncIterator[None]: # noqa: RUF029
assert not _core.currently_ki_protected()
try:
yield
Expand Down Expand Up @@ -321,7 +324,9 @@ async def check_protected_kill() -> None:
# error, then kill)
print("check 3")

async def check_kill_during_shutdown() -> None:
async def check_kill_during_shutdown() -> ( # noqa: RUF029 # async fn missing await
None
):
token = _core.current_trio_token()

def kill_during_shutdown() -> None:
Expand Down Expand Up @@ -426,7 +431,7 @@ def abort(raise_cancel: RaiseCancelT) -> Abort:
print("check 9")

@_core.enable_ki_protection
async def main_6() -> None:
async def main_6() -> None: # noqa: RUF029 # async function missing await
ki_self()

with pytest.raises(KeyboardInterrupt):
Expand Down Expand Up @@ -485,7 +490,7 @@ def test_ki_is_good_neighbor() -> None:
def my_handler(signum: object, frame: object) -> None: # pragma: no cover
pass

async def main() -> None:
async def main() -> None: # noqa: RUF029 # async function missing await
signal.signal(signal.SIGINT, my_handler)

_core.run(main)
Expand All @@ -509,7 +514,7 @@ def test_ki_with_broken_threads() -> None:
del threading._active[thread.ident] # type: ignore[attr-defined]

@_core.enable_ki_protection
async def inner() -> None:
async def inner() -> None: # noqa: RUF029 # async function missing await
assert signal.getsignal(signal.SIGINT) != signal.default_int_handler

_core.run(inner)
Expand Down
Loading
Loading