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 all 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
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}")
CoolCat467 marked this conversation as resolved.
Show resolved Hide resolved
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
15 changes: 13 additions & 2 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 Expand Up @@ -147,10 +148,20 @@ extend-ignore = [
# to export for public use.
'src/trio/__init__.py' = ['F401']
'src/trio/_core/__init__.py' = ['F401']
'src/trio/abc.py' = ['F401']
'src/trio/abc.py' = ['F401', 'A005']
'src/trio/lowlevel.py' = ['F401']
'src/trio/socket.py' = ['F401']
'src/trio/socket.py' = ['F401', 'A005']
'src/trio/testing/__init__.py' = ['F401']
# RUF029 is ignoring tests that are marked as async functions but
# do not use an await in their function bodies. There are several
# places where internal trio synchronous code relies on being
# called from an async function, where current task is set up.
'src/trio/_tests/*.py' = ['RUF029']
'src/trio/_core/_tests/*.py' = ['RUF029']
# A005 is ignoring modules that shadow stdlib modules.
'src/trio/_abc.py' = ['A005']
'src/trio/_socket.py' = ['A005']
'src/trio/_ssl.py' = ['A005']
Copy link
Contributor

Choose a reason for hiding this comment

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

Given we're disabling it for all modules it would trigger on, would it make sense to globally ignore it?

Copy link
Member Author

Choose a reason for hiding this comment

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

I had the intention of disabling it on current triggers so that in the event a new module was added it would be triggered for the new one. To be honest I don't see that realistically happening any time soon, but that was my logic.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah that would make sense. TBH using the built in setting of names to ignore might work with less noise for that case. I'm not sure what would be better.


[tool.ruff.lint.isort]
combine-as-imports = true
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
6 changes: 3 additions & 3 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 @@ -227,8 +227,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
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 @@ -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
6 changes: 3 additions & 3 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
6 changes: 4 additions & 2 deletions src/trio/_core/_tests/test_mock_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ async def test_mock_clock_autojump(mock_clock: MockClock) -> None:
mock_clock.autojump_threshold = 0
# if the above line didn't take affect immediately, then this would be
# bad:
await sleep(100000)
# ignore ASYNC116, not sleep_forever, trying to test a large but finite sleep
await sleep(100000) # noqa: ASYNC116


async def test_mock_clock_autojump_interference(mock_clock: MockClock) -> None:
Expand All @@ -109,7 +110,8 @@ async def test_mock_clock_autojump_interference(mock_clock: MockClock) -> None:
await wait_all_tasks_blocked(0.015)

# but the 0.02 limit does apply
await sleep(100000)
# ignore ASYNC116, not sleep_forever, trying to test a large but finite sleep
await sleep(100000) # noqa: ASYNC116


def test_mock_clock_autojump_preset() -> None:
Expand Down
4 changes: 2 additions & 2 deletions src/trio/_core/_tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ async def test_current_time_with_mock_clock(mock_clock: _core.MockClock) -> None
start = mock_clock.current_time()
assert mock_clock.current_time() == _core.current_time()
assert mock_clock.current_time() == _core.current_time()
mock_clock.jump(3.14)
assert start + 3.14 == mock_clock.current_time() == _core.current_time()
mock_clock.jump(3.15)
assert start + 3.15 == mock_clock.current_time() == _core.current_time()


async def test_current_clock(mock_clock: _core.MockClock) -> None:
Expand Down
5 changes: 2 additions & 3 deletions src/trio/_dtls.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,10 +561,9 @@ def _current_cookie_tick() -> int:
# Simple deterministic and invertible serializer -- i.e., a useful tool for converting
# structured data into something we can cryptographically sign.
def _signable(*fields: bytes) -> bytes:
out = []
out: list[bytes] = []
for field in fields:
out.append(struct.pack("!Q", len(field)))
out.append(field)
out.extend((struct.pack("!Q", len(field)), field))
return b"".join(out)


Expand Down
4 changes: 3 additions & 1 deletion src/trio/_socket.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# ruff: noqa: PYI063 # Several cases throughout file where
# argument names with __ used because of typeshed, see comment for recv in _SocketType
jakkdl marked this conversation as resolved.
Show resolved Hide resolved

from __future__ import annotations

import os
Expand Down Expand Up @@ -676,7 +679,6 @@ async def accept(self) -> tuple[SocketType, AddressFormat]:
async def connect(self, address: AddressFormat) -> None:
raise NotImplementedError

# argument names with __ used because of typeshed, see comment for recv in _SocketType
def recv(__self, __buflen: int, __flags: int = 0) -> Awaitable[bytes]:
raise NotImplementedError

Expand Down
3 changes: 2 additions & 1 deletion src/trio/_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,8 @@ async def _open_process(
return Process._create(popen, trio_stdin, trio_stdout, trio_stderr)


async def _windows_deliver_cancel(p: Process) -> None:
# async function missing await
async def _windows_deliver_cancel(p: Process) -> None: # noqa: RUF029
try:
p.terminate()
except OSError as exc:
Expand Down
2 changes: 1 addition & 1 deletion src/trio/_tests/test_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ async def receive_block(r: trio.MemoryReceiveChannel[int]) -> None:
with pytest.raises(trio.ClosedResourceError):
await r.receive()

s2, r2 = open_memory_channel[int](0)
_s2, r2 = open_memory_channel[int](0)
async with trio.open_nursery() as nursery:
nursery.start_soon(receive_block, r2)
await wait_all_tasks_blocked()
Expand Down
6 changes: 3 additions & 3 deletions src/trio/_tests/test_dtls.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async def echo_handler(dtls_channel: DTLSChannel) -> None:

@parametrize_ipv6
async def test_smoke(ipv6: bool) -> None:
async with dtls_echo_server(ipv6=ipv6) as (server_endpoint, address):
async with dtls_echo_server(ipv6=ipv6) as (_server_endpoint, address):
with endpoint(ipv6=ipv6) as client_endpoint:
client_channel = client_endpoint.connect(address, client_ctx)
with pytest.raises(trio.NeedHandshakeError):
Expand Down Expand Up @@ -260,7 +260,7 @@ async def test_channel_closing() -> None:


async def test_serve_exits_cleanly_on_close() -> None:
async with dtls_echo_server(autocancel=False) as (server_endpoint, address):
async with dtls_echo_server(autocancel=False) as (server_endpoint, _address):
server_endpoint.close()
# Testing that the nursery exits even without being cancelled
# close is idempotent
Expand Down Expand Up @@ -679,7 +679,7 @@ def route_packet(packet: UDPPacket) -> None:

fn.route_packet = route_packet # type: ignore[assignment] # TODO add type annotations for FakeNet

async with dtls_echo_server(mtu=MTU) as (server, address):
async with dtls_echo_server(mtu=MTU) as (_server, address):
with endpoint() as client:
channel = client.connect(address, client_ctx)
channel.set_ciphertext_mtu(MTU)
Expand Down
4 changes: 2 additions & 2 deletions src/trio/_tests/test_exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,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 @@ -243,7 +243,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
2 changes: 1 addition & 1 deletion src/trio/_tests/test_fakenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async def test_msg_trunc() -> None:
s2 = trio.socket.socket(type=trio.socket.SOCK_DGRAM)
await s1.bind(("127.0.0.1", 0))
await s2.sendto(b"xyz", s1.getsockname())
data, addr = await s1.recvfrom(10)
await s1.recvfrom(10)


async def test_recv_methods() -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/trio/_tests/test_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ async def test_async_methods_wrap(
wrapped.reset_mock()


async def test_async_methods_match_wrapper(
def test_async_methods_match_wrapper(
async_file: AsyncIOWrapper[mock.Mock],
wrapped: mock.Mock,
) -> None:
Expand Down
4 changes: 2 additions & 2 deletions src/trio/_tests/test_highlevel_open_tcp_listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ async def test_open_tcp_listeners_some_address_families_unavailable(
) -> None:
fsf = FakeSocketFactory(
10,
raise_on_family={family: errno.EAFNOSUPPORT for family in fail_families},
raise_on_family=dict.fromkeys(fail_families, errno.EAFNOSUPPORT),
)
tsocket.set_custom_socket_factory(fsf)
tsocket.set_custom_hostname_resolver(
Expand Down Expand Up @@ -402,7 +402,7 @@ async def test_open_tcp_listeners_backlog() -> None:
async def test_open_tcp_listeners_backlog_float_error() -> None:
fsf = FakeSocketFactory(99)
tsocket.set_custom_socket_factory(fsf)
for should_fail in (0.0, 2.18, 3.14, 9.75):
for should_fail in (0.0, 2.18, 3.15, 9.75):
with pytest.raises(
TypeError,
match=f"backlog must be an int or None, not {should_fail!r}",
Expand Down
12 changes: 6 additions & 6 deletions src/trio/_tests/test_highlevel_open_tcp_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,21 +382,21 @@ async def run_scenario(


async def test_one_host_quick_success(autojump_clock: MockClock) -> None:
sock, scenario = await run_scenario(80, [("1.2.3.4", 0.123, "success")])
sock, _scenario = await run_scenario(80, [("1.2.3.4", 0.123, "success")])
assert isinstance(sock, FakeSocket)
assert sock.ip == "1.2.3.4"
assert trio.current_time() == 0.123


async def test_one_host_slow_success(autojump_clock: MockClock) -> None:
sock, scenario = await run_scenario(81, [("1.2.3.4", 100, "success")])
sock, _scenario = await run_scenario(81, [("1.2.3.4", 100, "success")])
assert isinstance(sock, FakeSocket)
assert sock.ip == "1.2.3.4"
assert trio.current_time() == 100


async def test_one_host_quick_fail(autojump_clock: MockClock) -> None:
exc, scenario = await run_scenario(
exc, _scenario = await run_scenario(
82,
[("1.2.3.4", 0.123, "error")],
expect_error=OSError,
Expand All @@ -406,7 +406,7 @@ async def test_one_host_quick_fail(autojump_clock: MockClock) -> None:


async def test_one_host_slow_fail(autojump_clock: MockClock) -> None:
exc, scenario = await run_scenario(
exc, _scenario = await run_scenario(
83,
[("1.2.3.4", 100, "error")],
expect_error=OSError,
Expand All @@ -416,7 +416,7 @@ async def test_one_host_slow_fail(autojump_clock: MockClock) -> None:


async def test_one_host_failed_after_connect(autojump_clock: MockClock) -> None:
exc, scenario = await run_scenario(
exc, _scenario = await run_scenario(
83,
[("1.2.3.4", 1, "postconnect_fail")],
expect_error=KeyboardInterrupt,
Expand Down Expand Up @@ -656,7 +656,7 @@ async def test_handles_no_ipv6(autojump_clock: MockClock) -> None:


async def test_no_hosts(autojump_clock: MockClock) -> None:
exc, scenario = await run_scenario(80, [], expect_error=OSError)
exc, _scenario = await run_scenario(80, [], expect_error=OSError)
assert "no results found" in str(exc)


Expand Down
Loading
Loading