From 1a67d10b01307db628bdd949ff00dfcd7fe1ae50 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Wed, 20 Sep 2023 18:47:59 -0500 Subject: [PATCH] Enable `Error` rules --- notes-to-self/how-does-windows-so-reuseaddr-work.py | 8 ++++---- pyproject.toml | 4 ++++ trio/_core/_instrumentation.py | 2 +- trio/_core/_run.py | 5 ++++- trio/_core/_tests/test_run.py | 2 +- trio/_subprocess_platform/kqueue.py | 7 ++++--- trio/_sync.py | 2 +- trio/_tests/test_highlevel_socket.py | 12 ++++++------ trio/_tests/test_signals.py | 2 +- trio/_tests/test_subprocess.py | 11 ++++++++--- 10 files changed, 34 insertions(+), 21 deletions(-) diff --git a/notes-to-self/how-does-windows-so-reuseaddr-work.py b/notes-to-self/how-does-windows-so-reuseaddr-work.py index 3189d4d594..c6d356f072 100644 --- a/notes-to-self/how-does-windows-so-reuseaddr-work.py +++ b/notes-to-self/how-does-windows-so-reuseaddr-work.py @@ -62,11 +62,11 @@ def table_entry(mode1, bind_type1, mode2, bind_type2): # default | wildcard | INUSE | Success | ACCESS | Success | INUSE | Success ) -for i, mode1 in enumerate(modes): - for j, bind_type1 in enumerate(bind_types): +for mode1 in modes: + for bind_type1 in bind_types: row = [] - for k, mode2 in enumerate(modes): - for l, bind_type2 in enumerate(bind_types): + for mode2 in modes: + for bind_type2 in bind_types: entry = table_entry(mode1, bind_type1, mode2, bind_type2) row.append(entry) # print(mode1, bind_type1, mode2, bind_type2, entry) diff --git a/pyproject.toml b/pyproject.toml index 60a9ab41cf..f3911adfa5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,11 +15,14 @@ target-version = "py38" respect-gitignore = true fix = true +line-length = 170 + # The directories to consider when resolving first vs. third-party imports. # Does not control what files to include/exclude! src = ["trio", "notes-to-self"] select = [ + "E", # Error "F", # pyflakes "I", # isort "YTT", # flake8-2020 @@ -28,6 +31,7 @@ extend-ignore = [ 'F403', # undefined-local-with-import-star 'F405', # undefined-local-with-import-star-usage 'F821', # undefined-name + 'E402', # module-import-not-at-top-of-file (usually OS-specific) ] include = ["*.py", "*.pyi", "**/pyproject.toml"] diff --git a/trio/_core/_instrumentation.py b/trio/_core/_instrumentation.py index a0757a5b83..c1063b0e3e 100644 --- a/trio/_core/_instrumentation.py +++ b/trio/_core/_instrumentation.py @@ -98,7 +98,7 @@ def call(self, hookname: str, *args: Any) -> None: for instrument in list(self[hookname]): try: getattr(instrument, hookname)(*args) - except: + except BaseException: self.remove_instrument(instrument) INSTRUMENT_LOGGER.exception( "Exception raised when calling %r on instrument %r. " diff --git a/trio/_core/_run.py b/trio/_core/_run.py index e5289233bc..5c39e6e756 100644 --- a/trio/_core/_run.py +++ b/trio/_core/_run.py @@ -1485,7 +1485,10 @@ class GuestState: run_sync_soon_not_threadsafe: Callable[[Callable[[], object]], object] = attr.ib() done_callback: Callable[[Outcome[Any]], object] = attr.ib() unrolled_run_gen: Generator[float, EventResult, None] = attr.ib() - _value_factory: Callable[[], Value[Any]] = lambda: Value(None) + + def _value_factory() -> Value[Any]: + return Value(None) + unrolled_run_next_send: Outcome[Any] = attr.ib(factory=_value_factory) def guest_tick(self) -> None: diff --git a/trio/_core/_tests/test_run.py b/trio/_core/_tests/test_run.py index 5c45cf828f..f67f83a4b8 100644 --- a/trio/_core/_tests/test_run.py +++ b/trio/_core/_tests/test_run.py @@ -2015,7 +2015,7 @@ async def test_Nursery_init() -> None: async def test_Nursery_private_init() -> None: # context manager creation should not raise async with _core.open_nursery() as nursery: - assert False == nursery._closed + assert not nursery._closed def test_Nursery_subclass() -> None: diff --git a/trio/_subprocess_platform/kqueue.py b/trio/_subprocess_platform/kqueue.py index efd0562fc2..a65045670f 100644 --- a/trio/_subprocess_platform/kqueue.py +++ b/trio/_subprocess_platform/kqueue.py @@ -19,9 +19,10 @@ async def wait_child_exiting(process: "_subprocess.Process") -> None: # I verified this value against both Darwin and FreeBSD KQ_NOTE_EXIT = 0x80000000 - make_event = lambda flags: select.kevent( - process.pid, filter=select.KQ_FILTER_PROC, flags=flags, fflags=KQ_NOTE_EXIT - ) + def make_event(flags: int) -> select.kevent: + return select.kevent( + process.pid, filter=select.KQ_FILTER_PROC, flags=flags, fflags=KQ_NOTE_EXIT + ) try: kqueue.control([make_event(select.KQ_EV_ADD | select.KQ_EV_ONESHOT)], 0) diff --git a/trio/_sync.py b/trio/_sync.py index df0a44c3dc..58a265cd97 100644 --- a/trio/_sync.py +++ b/trio/_sync.py @@ -748,7 +748,7 @@ class Condition(AsyncContextManagerMixin): def __init__(self, lock: Lock | None = None): if lock is None: lock = Lock() - if not type(lock) is Lock: + if type(lock) is not Lock: raise TypeError("lock must be a trio.Lock") self._lock = lock self._lot = trio.lowlevel.ParkingLot() diff --git a/trio/_tests/test_highlevel_socket.py b/trio/_tests/test_highlevel_socket.py index 1a987df3f3..1c560d60f3 100644 --- a/trio/_tests/test_highlevel_socket.py +++ b/trio/_tests/test_highlevel_socket.py @@ -242,21 +242,21 @@ async def accept(self): ] ) - l = SocketListener(fake_listen_sock) + listener = SocketListener(fake_listen_sock) with assert_checkpoints(): - s = await l.accept() - assert s.socket is fake_server_sock + stream = await listener.accept() + assert stream.socket is fake_server_sock for code in [errno.EMFILE, errno.EFAULT, errno.ENOBUFS]: with assert_checkpoints(): with pytest.raises(OSError) as excinfo: - await l.accept() + await listener.accept() assert excinfo.value.errno == code with assert_checkpoints(): - s = await l.accept() - assert s.socket is fake_server_sock + stream = await listener.accept() + assert stream.socket is fake_server_sock async def test_socket_stream_works_when_peer_has_already_closed(): diff --git a/trio/_tests/test_signals.py b/trio/_tests/test_signals.py index 1e42239e35..d0f1bd1c74 100644 --- a/trio/_tests/test_signals.py +++ b/trio/_tests/test_signals.py @@ -108,7 +108,7 @@ async def test_open_signal_receiver_no_starvation() -> None: # Clear out the last signal so that it doesn't get redelivered while get_pending_signal_count(receiver) != 0: await receiver.__anext__() - except: # pragma: no cover + except BaseException: # pragma: no cover # If there's an unhandled exception above, then exiting the # open_signal_receiver block might cause the signal to be # redelivered and give us a core dump instead of a traceback... diff --git a/trio/_tests/test_subprocess.py b/trio/_tests/test_subprocess.py index 7986dfd71e..17cf740012 100644 --- a/trio/_tests/test_subprocess.py +++ b/trio/_tests/test_subprocess.py @@ -44,7 +44,7 @@ # Since Windows has very few command-line utilities generally available, # all of our subprocesses are Python processes running short bits of # (mostly) cross-platform code. -def python(code): +def python(code: str) -> list[str]: return [sys.executable, "-u", "-c", "import sys; " + code] @@ -53,9 +53,14 @@ def python(code): CAT = python("sys.stdout.buffer.write(sys.stdin.buffer.read())") if posix: - SLEEP = lambda seconds: ["/bin/sleep", str(seconds)] + + def SLEEP(seconds: int) -> list[str]: + return ["/bin/sleep", str(seconds)] + else: - SLEEP = lambda seconds: python(f"import time; time.sleep({seconds})") + + def SLEEP(seconds: int) -> list[str]: + return python(f"import time; time.sleep({seconds})") def got_signal(proc, sig):