-
-
Notifications
You must be signed in to change notification settings - Fork 343
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
Enable ruff flake8-simplify rule #2823
Conversation
Codecov Report
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
|
There was a problem hiding this 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.
@@ -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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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/_subprocess.py
Outdated
@@ -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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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: off
ing 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the change
There was a problem hiding this 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:
- They suppress coverage info
- 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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL. Me like
trio/_subprocess.py
Outdated
@@ -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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the change
Reverted as of 76d7413 |
Collapsable if check is a good thing, but care should be used to check that it doesn't make something confusing, which is why it's a `--unsafe-fixes` option
There was a problem hiding this 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~
trio/_subprocess.py
Outdated
@@ -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 |
There was a problem hiding this comment.
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: off
ing then I think fd)
should be moved onto line 176, kinda silly to split it up this much.
I think we can merge this unless @A5rocks or others have any comments. |
There was a problem hiding this 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.
This PR enables the ruff flake8-simplify rule (SIM) and fixes all of the new errors from enabling it.