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

Conversation

CoolCat467
Copy link
Member

This PR enables the ruff flake8-simplify rule (SIM) and fixes all of the new errors from enabling it.

@codecov
Copy link

codecov bot commented Oct 19, 2023

Codecov Report

Merging #2823 (69cc316) into master (8b15548) will increase coverage by 0.02%.
The diff coverage is 95.65%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2823      +/-   ##
==========================================
+ Coverage   99.16%   99.18%   +0.02%     
==========================================
  Files         115      115              
  Lines       17514    17466      -48     
  Branches     3125     3140      +15     
==========================================
- Hits        17368    17324      -44     
+ Misses        101       97       -4     
  Partials       45       45              
Files Coverage Δ
trio/_core/_io_epoll.py 100.00% <100.00%> (ø)
trio/_core/_multierror.py 100.00% <ø> (ø)
trio/_core/_run.py 100.00% <100.00%> (ø)
trio/_core/_tests/test_asyncgen.py 99.08% <100.00%> (-0.01%) ⬇️
trio/_core/_tests/test_guest_mode.py 99.69% <100.00%> (-0.01%) ⬇️
trio/_core/_tests/test_multierror.py 100.00% <100.00%> (ø)
trio/_core/_tests/test_run.py 100.00% <100.00%> (ø)
trio/_core/_wakeup_socketpair.py 100.00% <100.00%> (ø)
trio/_dtls.py 96.76% <100.00%> (-0.02%) ⬇️
trio/_highlevel_open_tcp_stream.py 97.59% <100.00%> (-0.06%) ⬇️
... and 14 more

Copy link
Contributor

@A5rocks A5rocks left a comment

Choose a reason for hiding this comment

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

I think overall this set of lints is kinda silly. Horizontal space really really shouldn't matter, especially with an autoformatter.

I can kinda see how a few might be helpful (e.g. suppress... and maybe multiple with statements) but talking about the multiple with statements thing, the stance here is super inconsistent.

Finally, I really don't get the rule that reorders the == or >= things.

trio/_core/_multierror.py Show resolved Hide resolved
trio/_core/_run.py Outdated Show resolved Hide resolved
@@ -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.

trio/_core/_tests/test_multierror.py Outdated Show resolved Hide resolved
trio/_core/_tests/test_run.py Outdated Show resolved Hide resolved
@@ -171,7 +172,11 @@ def __init__(
else:
# It worked! Wrap the raw fd up in a Python file object to
# make sure it'll get closed.
self._pidfd = open(fd)
# fmt: off
self._pidfd = open( # noqa: SIM115 # open-file-with-context-handler
Copy link
Contributor

Choose a reason for hiding this comment

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

I think ruff shouldn't error on this case, this is just silly. Not every class should be constructable solely using a context manager.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah this one could be reported upstream. Why is the # fmt: off needed ?

Copy link
Member Author

Choose a reason for hiding this comment

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

# fmt: off needed because basically ruff and black end up fighting over the formatting if I remember correctly. I think it's because of differences in the way the two handle trying to make it not exceed a line number limit.

Copy link
Member

Choose a reason for hiding this comment

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

that also sounds like something that should be reported upstream 😅

if we're fmt: offing then I think fd) should be moved onto line 176, kinda silly to split it up this much.

@@ -250,7 +245,7 @@ def __init__(
assert len(set(ip_order)) == len(ip_list)
ip_dict: dict[str | int, tuple[float, str]] = {}
for ip, delay, result in ip_list:
assert 0 <= delay
assert delay >= 0
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 this.

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 the change

trio/_tests/test_path.py Outdated Show resolved Hide resolved
Copy link
Member

@jakkdl jakkdl left a comment

Choose a reason for hiding this comment

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

I think the combine-with and combine-if checks should be disabled and the changes reverted:

  1. They suppress coverage info
  2. when the ifs/withs are doing completely different types of things, squashing them together makes them seem related - which they aren't. This is especially bad with pytest.raises + testing a with statement, and just in general with a library like Trio where careful thinking about scopes is really important.

@@ -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
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.

@@ -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
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.

@@ -593,10 +594,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

@@ -171,7 +172,11 @@ def __init__(
else:
# It worked! Wrap the raw fd up in a Python file object to
# make sure it'll get closed.
self._pidfd = open(fd)
# fmt: off
self._pidfd = open( # noqa: SIM115 # open-file-with-context-handler
Copy link
Member

Choose a reason for hiding this comment

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

Yeah this one could be reported upstream. Why is the # fmt: off needed ?

@@ -173,7 +170,7 @@ async def route_packet(packet: UDPPacket) -> None:
break

def route_packet_wrapper(packet: UDPPacket) -> None:
try:
try: # noqa: SIM105 # suppressible-exception
Copy link
Member

Choose a reason for hiding this comment

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

loss of coverage information on whether a suppressed except is caught or not is actually a pretty good reason not to enable this check.

trio/_tests/test_dtls.py Outdated Show resolved Hide resolved
@@ -250,7 +245,7 @@ def __init__(
assert len(set(ip_order)) == len(ip_list)
ip_dict: dict[str | int, tuple[float, str]] = {}
for ip, delay, result in ip_list:
assert 0 <= delay
assert delay >= 0
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 the change

trio/_core/_multierror.py Show resolved Hide resolved
@CoolCat467
Copy link
Member Author

I think the combine-with and combine-if checks should be disabled and the changes reverted:

1. They suppress coverage info

2. when the ifs/withs are doing completely different types of things, squashing them together makes them seem related - which they aren't. This is especially bad with `pytest.raises` + testing a with statement, and just in general with a library like Trio where careful thinking about scopes is really important.

Reverted as of 76d7413

trio/_core/_multierror.py Outdated Show resolved Hide resolved
Copy link
Member

@jakkdl jakkdl left a comment

Choose a reason for hiding this comment

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

Minor formatting change, and I think you should open upstream issues. Otherwise looks great~

@@ -171,7 +172,11 @@ def __init__(
else:
# It worked! Wrap the raw fd up in a Python file object to
# make sure it'll get closed.
self._pidfd = open(fd)
# fmt: off
self._pidfd = open( # noqa: SIM115 # open-file-with-context-handler
Copy link
Member

Choose a reason for hiding this comment

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

that also sounds like something that should be reported upstream 😅

if we're fmt: offing then I think fd) should be moved onto line 176, kinda silly to split it up this much.

@jakkdl
Copy link
Member

jakkdl commented Oct 26, 2023

I think we can merge this unless @A5rocks or others have any comments.

Copy link
Contributor

@TeamSpen210 TeamSpen210 left a comment

Choose a reason for hiding this comment

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

Looks like improvements, though we might want to use a bit of caution with contextlib.suppress. It's definitely tidier, but I imagine it could have a performance impact.

@jakkdl jakkdl merged commit 2a28313 into python-trio:master Oct 30, 2023
28 of 30 checks passed
@CoolCat467 CoolCat467 deleted the enable-flake8-simplify branch October 30, 2023 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants