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's flake8-trio rule #2947

Closed
wants to merge 3 commits into from

Conversation

CoolCat467
Copy link
Member

          Probably should enable the TRIO rules? Maybe in a separate PR.

Originally posted by @TeamSpen210 in #2946 (comment)

This pull request enables the flake8-trio ruff rule (but in ruff's system it's marked as TRIO).

Copy link

codecov bot commented Feb 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (70c8b76) 99.44% compared to head (9d0008f) 99.64%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2947      +/-   ##
==========================================
+ Coverage   99.44%   99.64%   +0.19%     
==========================================
  Files         117      117              
  Lines       17564    17565       +1     
  Branches     3166     3166              
==========================================
+ Hits        17467    17503      +36     
+ Misses         79       43      -36     
- Partials       18       19       +1     
Files Coverage Δ
src/trio/_core/_tests/test_guest_mode.py 100.00% <100.00%> (ø)
src/trio/_tests/test_scheduler_determinism.py 100.00% <100.00%> (ø)
src/trio/_tests/test_threads.py 100.00% <100.00%> (ø)

... and 9 files with indirect coverage changes

@CoolCat467
Copy link
Member Author

At the moment the tests will fail until #2946 gets merged, and even then there will be a failure detailed in astral-sh/ruff#9855 that is a bug.

@jakkdl
Copy link
Member

jakkdl commented Feb 6, 2024

Disclaimer: I'm the one that wrote 99% of the code in flake8-trio.

Ruffs implementation of flake8-trio is only a small set (6 out of ~30) of the full rules: https://github.com/Zac-HD/flake8-trio Although a few of them are handled under other rules.

The plugin is intended for users of trio or anyio*, so some rules might not make sense in trio itself, but can ofc then just disable those.

It is also possible to run flake8-trio as a standalone*, so we don't have to bring back flake8. (because we wanted to implement autofix with libcst, which isn't possible when running with flake8). But there might be reasons to run it through flake8, I don't fully remember.

* yes, both parts of the flake8-trio name are incorrect... python-trio/flake8-async#124 (comment)

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.

Just quick thing I think isn't great

@@ -165,7 +167,7 @@ def external_thread_fn() -> None:
thread.start()
print("waiting")
while thread.is_alive():
await sleep(0.01)
await lowlevel.checkpoint()
Copy link
Contributor

@A5rocks A5rocks Feb 10, 2024

Choose a reason for hiding this comment

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

I think it's probably better to use sleep here because otherwise we'll be busy looping I think?

Like this isn't a matter of "trio just needs another cycle or two of the available tasks" this is "we need to wait for the thread to start and do its stuff"

This happens 2 other places too.

Copy link
Member

@jakkdl jakkdl Feb 12, 2024

Choose a reason for hiding this comment

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

this is also a bug in ruffs implementation, flake8-trio only triggers when the literal is exactly 0

Copy link
Member

Choose a reason for hiding this comment

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

wait no, ruff gave TRIO110, which suggests to await an Event instead of looping a sleep. Resolving that by replacing with a checkpoint is ... definitely not the way to go.
Maybe I should widen TRIO110 to also catch checkpoints to make sure people don't confuse them and thinks that because it stops warning on checkpoint that it "solves" it

Copy link
Member Author

@CoolCat467 CoolCat467 Feb 12, 2024

Choose a reason for hiding this comment

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

I manually changed the 0.02 to a checkpoint because an error was being raised from the sleep there, mostly because the Event part didn't make a lot of sense to me. Should have just done a noqa really.

Copy link
Member

Choose a reason for hiding this comment

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

yeah an Event could plausibly not be possible or make sense in a context, in which case noqa would be the solution :)

@mikenerone
Copy link
Member

mikenerone commented Feb 11, 2024

I just want to mention that ruff's TRIO rules are significantly behind flake8-trio, and produce some false positives as a result. See astral-sh/ruff#9934 and astral-sh/ruff#9935.

@CoolCat467
Copy link
Member Author

Given that ruff's version has a few issues and other points mentioned, I don't think this particular rule is fit for Trio at the moment.

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