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

Re-code flake8-trio and flake8-async rules to match upstream #10416

Merged
merged 22 commits into from
Jun 26, 2024

Conversation

augustelalande
Copy link
Contributor

@augustelalande augustelalande commented Mar 15, 2024

Summary

Re-code flake8-trio and flake8-async rules to match upstream. Resolves #10303.

TRIO rules

  • TRIO has been renamed to ASYNC1
  • TRIO100 -> ASYNC100
  • TRIO105 -> ASYNC105
  • TRIO109 -> ASYNC109
  • TRIO110 -> ASYNC110
  • TRIO115 -> ASYNC115

ASYNC rules

  • ASYNC116 unchanged
  • ASYNC100..ASYNC102 has been renamed to ASYNC2
  • ASYNC100 moved to ASYNC210,
  • ASYNC101 was split into ASYNC220, ASYNC221, ASYNC222, ASYNC230, and ASYNC251
  • ASYNC102 was merged into ASYNC220 and ASYNC221

Behavior changes

  • ASYNC210 (previously ASYNC100): Now detects urllib3.request calls
  • ASYNC230: Now also detects calls to io.open, io.open_code

Test Plan

@augustelalande
Copy link
Contributor Author

augustelalande commented Mar 15, 2024

This is mostly done, except for the previous ASYNC101 and ASYNC102 which don't exactly match any the of the new ASYNC rules. The closest matches are ASYNC220 and ASYNC222, but the behaviors don't match exactly. Part of the problem is that the previous rules should be split into multiple rules (this is easy). The other problem which I'm not sure how to resolve is that it doesn't seem like any of the new ASYNC rules check for time.sleep or open (nevermind ASYNC230 covers open), but this is a useful check that we should keep, so the question is what should I do?

Copy link
Contributor

github-actions bot commented Mar 15, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@augustelalande
Copy link
Contributor Author

Ok so I modified the implementation of ASYNC220 and ASYNC222, as well as added ASYNC221 so that the three rules would match the behavior of flake8-async. For now I added a check for time.sleep as part of ASYNC222.

@jakkdl
Copy link

jakkdl commented Mar 15, 2024

Oh, I'd missed that we don't check for time.sleep anymore. It's a bad match for ASYNC222, as the recommended replacement is using [trio/anyio/asyncio].sleep(), and not to wrap it in to_thread_run_sync or run_in_executor. I'll implement it quickly as ASYNC251

@augustelalande
Copy link
Contributor Author

augustelalande commented Mar 15, 2024

Ok I moved the time.sleep check to a newly created ASYNC251

@Zac-HD
Copy link

Zac-HD commented Mar 15, 2024

We've just released ASYNC251 upstream, so this is back to matching the merged flake8-async plugin.

@zanieb
Copy link
Member

zanieb commented Mar 15, 2024

Note that we'll need to add "redirects" for all of these rules, see rule_redirects.rs. See #9756 for example.

We may be able to make use of #9691 as well, not sure yet.

@augustelalande
Copy link
Contributor Author

Ok I added redirects for the old TRIO rules. But as mentioned the old ASYNC rules don't directly correspond to new ASYNC rules.

@jakkdl
Copy link

jakkdl commented May 17, 2024

I've got some comments on the human-friendly rule names you have, and it might be good if we synced them to ease transition between flake8-async and ruff. See python-trio/flake8-async#248 (comment)

(I'm sorry we're moving so quickly as of late 😁)

@JelleZijlstra
Copy link
Contributor

This PR has some merge conflicts now, anything I can do to help?

I'm motivated to add (the new) ASYNC101 to Ruff because of the problems mentioned in the draft PEP 789.

@charliermarsh
Copy link
Member

The main issue is that it's going to be a breaking change for users (e.g., ASYNC100 continues to exist but is now a different rule). We may be able to include it in v0.5.0 though which will be ~soon.

@charliermarsh charliermarsh added this to the v0.5.0 milestone May 23, 2024
@charliermarsh
Copy link
Member

@zanieb - let's include this in v0.5.0 since it's starting to cause problems.

@charliermarsh
Copy link
Member

@augustelalande -- I'm happy to take responsibility for resolving any conflicts.

@augustelalande
Copy link
Contributor Author

@charliermarsh I'll do it now. I'm also gonna update the rules with the new names from python-trio/flake8-async#248 (comment)

@augustelalande
Copy link
Contributor Author

Nevermind the last part. Updating all the rule names is a significant effort so should be a separate PR.

@MichaReiser MichaReiser changed the base branch from main to ruff-0.5 June 25, 2024 12:13
@MichaReiser
Copy link
Member

Okay, I'm done with the rebase and I very much hope I didn't mess anything up. I now need to get a better understanding of the change itself to be able to review it :)

@MichaReiser
Copy link
Member

I'm trying to get my head around over all the changes and are having a hard time. What's unclear to me is:

  • What's the exact mapping from old rules to new rules
  • What I understand is that our rules don't precisely match flake8-async. Which rules are different and to what extend?

@MichaReiser
Copy link
Member

From the chanegelog: Added asyncio support for several error codes. I think that' the case for most of the TRIO rules. This isn't something we have to change as part of this PR but important for the changelog

@MichaReiser
Copy link
Member

Okay, I think I now have a good overview of what has changed. I plan on reviewing this PR tomorrow (and hopefully merge).

I don't think there's anything we can do about mapping the old ASYNC rules.

Trio to ASYNC

  • TRIO100 -> ASYNC100 :
    • No support for anyio or asyncio
    • Ruff: TrioTimeoutWithoutAwait
    • Flake8: cancel-scope-no-checkpoint
  • TRIO105 -> ASYNC105
    • Ruff: TrioSyncCall
    • Flake8: missing-await
    • Both only support trio
  • TRIO109 -> ASYNC109
    • I think the flake8 rule also supports anyio and asyncio.
    • Ruff: TrioAsyncFunctionWithTimeout
    • Flake8: async-function-with-timeout
  • TRIO110 -> ASYNC110
    • Ruff: TrioUnneededSleep
    • Flake8: async-busy-wait
    • Missing anyio support
  • TRIO115 -> ASYNC115
    • Ruff: TrioZeroSleepCall
    • Flake8: async-zero-sleep
    • Missing anyio support
  • TRIO116 -> ASYNC116
    • Ruff: SleepForeverCall
    • Flake8: long-sleep-not-forever
    • Missing anyio support

ASYNC rule changes

  • ASYNC100 -> ASYNC210:
    • Ruff: BlockingHttpCallInAsyncFunction
    • Flake8: blocking-http-call
    • Looks about right
  • ASYNC101 ->
    • Used to test for
      • open -> ASYNC230
      • time.sleep -> ASYNC251
      • subprocess:
        • subprocess.run, -> ASYNC221
        • subprocess.Popen -> ASYNC220
        • subprocess.call -> ASYNC221
        • subprocess.check_call -> ASYNC221
        • subprocess.check_output -> ASYNC221
        • subprocess.getoutput, -> ASYNC221
        • subprocess.getstatusoutput -> ASYNC221
      • os
        • wait -> ASYNC222
        • wait3 -> ASYNC222
        • wait4 -> ASYNC222
        • waitid -> ASYNC222
        • waitpid -> ASYNC222
    • ASYNC220:
      • Also tests for os.popen, os.spawn (if not p_wait)
      • flake8: blocking-create-subprocess
      • Ruff: CreateSubprocessInAsyncFunction
    • ASYNC221:
      • Also tests for os.system, os.posix_spawn, os.posix_spawnp
      • flake8: blocking-run-process
      • Ruff: RunProcessInAsyncFunction
      • Sometimes os.spawn (when not ASYNC220)
    • ASYNC222:
      • Ruff: WaitForProcessInAsyncFunction
      • Flake8: blocking-process-wait
    • ASYNC230:
      • Ruff: BlockingOpenCallInAsyncFunction
      • flake8: blocking-open-call
    • ASYNC251:
      • Ruff: BlockingSleepInAsyncFunction
      • flake8: blocking-sleep

@MichaReiser MichaReiser self-assigned this Jun 25, 2024
@MichaReiser
Copy link
Member

I manually tested the redirection rules and it all looks good. Let's merge this.

@MichaReiser MichaReiser merged commit 3e33b0e into astral-sh:ruff-0.5 Jun 26, 2024
20 checks passed
@MichaReiser MichaReiser mentioned this pull request Jun 26, 2024
@augustelalande augustelalande deleted the merge-trio-async branch June 26, 2024 14:08
@MichaReiser
Copy link
Member

I think one nice follow up after the release would be to rename the rules to match the upstream names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Breaking API change configuration Related to settings and configuration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Re-code flake8-trio and flake8-async rules to match upstream
7 participants