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 ruff flake8-simplify rule #2823

Merged
merged 16 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
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
35 changes: 17 additions & 18 deletions notes-to-self/file-read-latency.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@
# ns per call, instead of ~500 ns/call for the syscall and related overhead.
# That's probably more fair -- the BufferedIOBase code can't service random
# accesses, even if your working set fits entirely in RAM.
f = open("/etc/passwd", "rb") # , buffering=0)
with open("/etc/passwd", "rb") as f: # , buffering=0)
while True:
start = time.perf_counter()
for _ in range(COUNT):
f.seek(0)
f.read(1)
between = time.perf_counter()
for _ in range(COUNT):
f.seek(0)
end = time.perf_counter()

while True:
start = time.perf_counter()
for _ in range(COUNT):
f.seek(0)
f.read(1)
between = time.perf_counter()
for _ in range(COUNT):
f.seek(0)
end = time.perf_counter()

both = (between - start) / COUNT * 1e9
seek = (end - between) / COUNT * 1e9
read = both - seek
print(
"{:.2f} ns/(seek+read), {:.2f} ns/seek, estimate ~{:.2f} ns/read".format(
both, seek, read
both = (between - start) / COUNT * 1e9
seek = (end - between) / COUNT * 1e9
read = both - seek
print(
"{:.2f} ns/(seek+read), {:.2f} ns/seek, estimate ~{:.2f} ns/read".format(
both, seek, read
)
)
)
42 changes: 21 additions & 21 deletions notes-to-self/proxy-benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,24 +156,24 @@ def check(cls):
for cls in classes:
check(cls)

f = open("/etc/passwd")
objs = [c(f) for c in classes]

COUNT = 1000000
try:
import __pypy__ # noqa: F401 # __pypy__ imported but unused
except ImportError:
pass
else:
COUNT *= 10

while True:
print("-------")
for obj in objs:
start = time.perf_counter()
for _ in range(COUNT):
obj.fileno()
# obj.fileno
end = time.perf_counter()
per_usec = COUNT / (end - start) / 1e6
print("{:7.2f} / us: {} ({})".format(per_usec, obj.strategy, obj.works_for))
with open("/etc/passwd") as f:
objs = [c(f) for c in classes]

COUNT = 1000000
try:
import __pypy__ # noqa: F401 # __pypy__ imported but unused
except ImportError:
pass
else:
COUNT *= 10

while True:
print("-------")
for obj in objs:
start = time.perf_counter()
for _ in range(COUNT):
obj.fileno()
# obj.fileno
end = time.perf_counter()
per_usec = COUNT / (end - start) / 1e6
print("{:7.2f} / us: {} ({})".format(per_usec, obj.strategy, obj.works_for))
5 changes: 3 additions & 2 deletions notes-to-self/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,6 @@ async def parent():
print("parent: all done!")


t = Trace(open("/tmp/t.json", "w"))
trio.run(parent, instruments=[t])
with open("/tmp/t.json", "w") as t_json:
t = Trace(t_json)
trio.run(parent, instruments=[t])
5 changes: 2 additions & 3 deletions notes-to-self/win-waitable-timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
# make this fairly straightforward, but you obviously need to use a separate
# time source

import contextlib
from datetime import datetime, timedelta, timezone

import cffi
import trio
from trio._core._windows_cffi import ffi, kernel32, raise_winerror

try:
with contextlib.suppress(cffi.CDefError):
ffi.cdef(
"""
typedef struct _PROCESS_LEAP_SECOND_INFO {
Expand All @@ -50,8 +51,6 @@
} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;
"""
)
except cffi.CDefError:
pass

ffi.cdef(
"""
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ select = [
"B", # flake8-bugbear
"YTT", # flake8-2020
"ASYNC", # flake8-async
"SIM", # flake8-simplify
]
extend-ignore = [
'F403', # undefined-local-with-import-star
'F405', # undefined-local-with-import-star-usage
'E402', # module-import-not-at-top-of-file (usually OS-specific)
'E501', # line-too-long
'SIM117', # multiple-with-statements (messes up lots of context-based stuff and looks bad)
]

include = ["*.py", "*.pyi", "**/pyproject.toml"]
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

__version__ = "0.0.0" # Overwritten from _version.py below, needed for linter to identify that this variable is defined.

exec(open("trio/_version.py", encoding="utf-8").read())
with open("trio/_version.py", encoding="utf-8") as version_code:
exec(version_code.read())

LONG_DESC = """\
.. image:: https://raw.githubusercontent.com/python-trio/trio/9b0bec646a31e0d0f67b8b6ecc6939726faf3e17/logo/logo-with-background.svg
Expand Down
5 changes: 2 additions & 3 deletions trio/_core/_io_epoll.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import contextlib
import select
import sys
from collections import defaultdict
Expand Down Expand Up @@ -324,7 +325,5 @@ def notify_closing(self, fd: int | _HasFileNo) -> None:
_core.ClosedResourceError("another task closed this fd"),
)
del self._registered[fd]
try:
with contextlib.suppress(OSError, ValueError):
self._epoll.unregister(fd)
except (OSError, ValueError):
pass
17 changes: 10 additions & 7 deletions trio/_core/_multierror.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def catch(
return MultiErrorCatcher(handler)


if TYPE_CHECKING:
if TYPE_CHECKING: # noqa: SIM108
CoolCat467 marked this conversation as resolved.
Show resolved Hide resolved
_ExceptionGroup = ExceptionGroup[Exception]
else:
_ExceptionGroup = ExceptionGroup
Expand Down Expand Up @@ -432,12 +432,15 @@ def controller(operation: tputil.ProxyOperation) -> Any | None:
# 'opname' that isn't __getattr__ or __getattribute__. So there's
# no missing test we could add, and no value in coverage nagging
# us about adding one.
if operation.opname in [
"__getattribute__",
"__getattr__",
]: # pragma: no cover
if operation.args[0] == "tb_next":
return tb_next
if (
operation.opname
in {
"__getattribute__",
"__getattr__",
}
and operation.args[0] == "tb_next"
): # pragma: no cover
return tb_next
return operation.delegate() # Deligate is reverting to original behaviour

return cast(
Expand Down
15 changes: 6 additions & 9 deletions trio/_core/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
Iterator,
Sequence,
)
from contextlib import AbstractAsyncContextManager, contextmanager
from contextlib import AbstractAsyncContextManager, contextmanager, suppress
from contextvars import copy_context
from heapq import heapify, heappop, heappush
from math import inf
Expand Down Expand Up @@ -792,7 +792,9 @@ def cancel_called(self) -> bool:
cancelled, then :attr:`cancelled_caught` is usually more
appropriate.
"""
if self._cancel_status is not None or not self._has_been_entered:
if ( # noqa: SIM102 # collapsible-if but this way is nicer
self._cancel_status is not None or not self._has_been_entered
):
# Scope is active or not yet entered: make sure cancel_called
# is true if the deadline has passed. This shouldn't
# be able to actually change behavior, since we check for
Expand Down Expand Up @@ -1708,10 +1710,7 @@ def spawn_impl(
# Propagate contextvars
######
if context is None:
if system_task:
context = self.system_context.copy()
else:
context = copy_context()
context = self.system_context.copy() if system_task else copy_context()

######
# Call the function and get the coroutine object, while giving helpful
Expand Down Expand Up @@ -1943,10 +1942,8 @@ def current_trio_token(self) -> TrioToken:
# This gets called from signal context
def deliver_ki(self) -> None:
self.ki_pending = True
try:
with suppress(RunFinishedError):
self.entry_queue.run_sync_soon(self._deliver_ki_cb)
except RunFinishedError:
pass

def _deliver_ki_cb(self) -> None:
if not self.ki_pending:
Expand Down
21 changes: 10 additions & 11 deletions trio/_core/_tests/test_asyncgen.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import contextlib
import sys
import weakref
from collections.abc import AsyncGenerator
Expand All @@ -17,10 +18,8 @@ def test_asyncgen_basics() -> None:

async def example(cause: str) -> AsyncGenerator[int, None]:
try:
try:
with contextlib.suppress(GeneratorExit):
yield 42
except GeneratorExit:
pass
await _core.checkpoint()
except _core.Cancelled:
assert "exhausted" not in cause
Expand All @@ -44,14 +43,14 @@ async def async_main() -> None:
with pytest.warns(
ResourceWarning, match="Async generator.*collected before.*exhausted"
):
assert 42 == await example("abandoned").asend(None)
assert await example("abandoned").asend(None) == 42
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't get how this simplifies anything?

Copy link
Member

Choose a reason for hiding this comment

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

I like this as a general style thing, though I think the developers of this repo already adheres to it decently well. E.g. code like

if (a == 3 || 4 == b):
  ...

is very silly, and in spirit feels like something Black would enforce - except they are explicitly AST-neutral. So I think there's good that there's a tool that enforces it.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I actually very much prefer the changes to this set of comparisons. Because they go against the standard, I was thinking maybe it's because the developer wants to place an emphasis on the left term because the right term is constant ... but no the literal is just a random magic number and the expression on the right hand side very much does change.

gc_collect_harder()
await _core.wait_all_tasks_blocked()
assert collected.pop() == "abandoned"

aiter = example("exhausted 1")
try:
assert 42 == await aiter.asend(None)
assert await aiter.asend(None) == 42
finally:
await aiter.aclose()
assert collected.pop() == "exhausted 1"
Expand All @@ -67,7 +66,7 @@ async def async_main() -> None:
aiter = example("exhausted 3")
try:
saved.append(aiter)
assert 42 == await aiter.asend(None)
assert await aiter.asend(None) == 42
finally:
await aiter.aclose()
assert collected.pop() == "exhausted 3"
Expand All @@ -83,7 +82,7 @@ async def async_main() -> None:
collected.append("outlived run")
else:
saved.append(example("outlived run"))
assert 42 == await saved[-1].asend(None)
assert await saved[-1].asend(None) == 42
assert collected == []

_core.run(async_main)
Expand Down Expand Up @@ -137,8 +136,8 @@ async def funky_agen() -> AsyncGenerator[int, None]:
async def async_main() -> None:
aiter = funky_agen()
saved.append(aiter)
assert 1 == await aiter.asend(None)
assert 2 == await aiter.asend(None)
assert await aiter.asend(None) == 1
assert await aiter.asend(None) == 2

_core.run(async_main)
assert record == ["cleanup 2", "cleanup 1"]
Expand Down Expand Up @@ -178,7 +177,7 @@ async def async_main() -> None:
for idx in range(100):
ag_chain = agen(idx, ag_chain)
saved.append(ag_chain)
assert 1 == await ag_chain.asend(None)
assert await ag_chain.asend(None) == 1
assert record == []

_core.run(async_main)
Expand Down Expand Up @@ -320,7 +319,7 @@ async def example(arg: str) -> AsyncGenerator[int, None]:

async def async_main() -> None:
await step_outside_async_context(example("theirs"))
assert 42 == await example("ours").asend(None)
assert await example("ours").asend(None) == 42
gc_collect_harder()
assert record == ["firstiter theirs", "finalizer theirs"]
record[:] = []
Expand Down
5 changes: 2 additions & 3 deletions trio/_core/_tests/test_guest_mode.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import contextlib
import contextvars
import queue
import signal
Expand Down Expand Up @@ -596,10 +597,8 @@ async def agen(label):
yield 1
finally:
library = sniffio.current_async_library()
try:
with contextlib.suppress(trio.Cancelled):
Copy link
Member

Choose a reason for hiding this comment

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

TIL. Me like

await sys.modules[library].sleep(0)
except trio.Cancelled:
pass
record.add((label, library))

async def iterate_in_aio():
Expand Down
3 changes: 1 addition & 2 deletions trio/_core/_tests/test_multierror.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,9 @@ def run_script(name: str) -> subprocess.CompletedProcess[bytes]:

env = dict(os.environ)
print("parent PYTHONPATH:", env.get("PYTHONPATH"))
pp = []
if "PYTHONPATH" in env: # pragma: no cover
pp = env["PYTHONPATH"].split(os.pathsep)
else:
pp = []
pp.insert(0, str(trio_path))
pp.insert(0, str(script_path.parent))
env["PYTHONPATH"] = os.pathsep.join(pp)
Expand Down
14 changes: 4 additions & 10 deletions trio/_core/_tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
Callable,
Generator,
)
from contextlib import ExitStack, contextmanager
from contextlib import ExitStack, contextmanager, suppress
from math import inf
from typing import Any, NoReturn, TypeVar, cast

Expand Down Expand Up @@ -311,10 +311,8 @@ async def test_current_statistics(mock_clock: _core.MockClock) -> None:

# A child that sticks around to make some interesting stats:
async def child() -> None:
try:
with suppress(_core.Cancelled):
await sleep_forever()
except _core.Cancelled:
pass

stats = _core.current_statistics()
print(stats)
Expand Down Expand Up @@ -1089,10 +1087,8 @@ async def child() -> None:
try:
raise KeyError
except Exception:
try:
with suppress(Exception):
await sleep_forever()
except Exception:
pass
raise

with pytest.raises(KeyError):
Expand Down Expand Up @@ -1215,10 +1211,8 @@ def test_TrioToken_run_sync_soon_idempotent_requeue() -> None:

def redo(token: _core.TrioToken) -> None:
record.append(None)
try:
with suppress(_core.RunFinishedError):
token.run_sync_soon(redo, token, idempotent=True)
except _core.RunFinishedError:
pass

async def main() -> None:
token = _core.current_trio_token()
Expand Down
Loading
Loading