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

Replace sync open() calls within async functions with aiofiles.open() in code examples #7383

Merged
merged 1 commit into from
Jul 24, 2023

Conversation

cclauss
Copy link
Contributor

@cclauss cclauss commented Jul 19, 2023

% ruff --select=ASYNC --statistics .

2 ASYNC101 [ ] Async functions should not call open, time.sleep, or subprocess methods

% ruff rule ASYNC101

open-sleep-or-subprocess-in-async-function (ASYNC101)

Derived from the flake8-async linter.

What it does

Checks that async functions do not contain calls to open, time.sleep, or subprocess methods.

Why is this bad?

Blocking an async function via a blocking call will block the entire event loop, preventing it from executing other tasks while waiting for the call to complete, negating the benefits of asynchronous programming.

Instead of making a blocking call, use an equivalent asynchronous library or function.

Example

async def foo():
    time.sleep(1000)

Use instead:

async def foo():
    await asyncio.sleep(1000)

What do these changes do?

Screenshot 2023-07-19 at 17 44 51

Are there changes in behavior for the user?

Related issue number

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES folder
    • name it <issue_id>.<type> for example (588.bugfix)
    • if you don't have an issue_id change it to the pr id after creating the pr
    • ensure type is one of the following:
      • .feature: Signifying a new feature.
      • .bugfix: Signifying a bug fix.
      • .doc: Signifying a documentation improvement.
      • .removal: Signifying a deprecation or removal of public API.
      • .misc: A ticket has been closed, but it is not interesting to users.
    • Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files."

@cclauss cclauss requested a review from webknjaz as a code owner July 19, 2023 15:43
@codecov
Copy link

codecov bot commented Jul 19, 2023

Codecov Report

Merging #7383 (156428e) into master (9721c65) will not change coverage.
The diff coverage is n/a.

❗ Current head 156428e differs from pull request most recent head 70a1413. Consider uploading reports for the commit 70a1413 to get more accurate results

@@           Coverage Diff           @@
##           master    #7383   +/-   ##
=======================================
  Coverage   97.28%   97.28%           
=======================================
  Files         106      106           
  Lines       31440    31440           
  Branches     3932     3932           
=======================================
  Hits        30585    30585           
  Misses        650      650           
  Partials      205      205           
Flag Coverage Δ
CI-GHA 97.22% <ø> (ø)
OS-Linux 96.89% <ø> (-0.01%) ⬇️
OS-Windows 95.35% <ø> (ø)
OS-macOS 96.58% <ø> (ø)
Py-3.10.11 95.27% <ø> (ø)
Py-3.10.12 96.79% <ø> (ø)
Py-3.11.0 96.51% <ø> (ø)
Py-3.8.10 95.24% <ø> (ø)
Py-3.8.17 96.72% <ø> (ø)
Py-3.9.13 95.24% <ø> (ø)
Py-3.9.17 96.75% <ø> (ø)
Py-pypy7.3.11 94.20% <ø> (-0.01%) ⬇️
VM-macos 96.58% <ø> (ø)
VM-ubuntu 96.89% <ø> (-0.01%) ⬇️
VM-windows 95.35% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@cclauss cclauss requested a review from asvetlov as a code owner July 19, 2023 16:06
Copy link
Member

@webknjaz webknjaz left a comment

Choose a reason for hiding this comment

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

This looks quite invasive and combines several separate pointless changes. For example, adding a dependency that is not actually used in the library.
Also, ruff can only be added to pre-commit, not a random workflow that only multiplies the maintenance burden.
I'm 👎 on the change as it is. Some small bits could be added if discussed and implemented correctly, but not as a bunch of commits that have no visible purpose.

@cclauss cclauss force-pushed the patch-1 branch 2 times, most recently from e027530 to aedb0bb Compare July 20, 2023 10:01
@cclauss cclauss changed the title GitHub Action to run the ruff ASYNC rules Fix async functions should not call open() Jul 20, 2023
@cclauss cclauss requested a review from webknjaz July 20, 2023 10:14
requirements/test.txt Outdated Show resolved Hide resolved
@webknjaz webknjaz requested review from Dreamsorcerer and removed request for asvetlov July 20, 2023 12:51
@webknjaz webknjaz changed the title Fix async functions should not call open() Replace sync open() calls within async functions with aiofiles.open() Jul 20, 2023
@webknjaz webknjaz changed the title Replace sync open() calls within async functions with aiofiles.open() Replace sync open() calls within async functions with aiofiles.open() in code examples Jul 20, 2023
@cclauss cclauss requested a review from webknjaz July 20, 2023 17:06
examples/web_ws.py Outdated Show resolved Hide resolved
tools/bench-asyncio-write.py Outdated Show resolved Hide resolved
@cclauss cclauss force-pushed the patch-1 branch 2 times, most recently from 95b11ef to 5ff6a7f Compare July 23, 2023 20:29
% [`ruff --select=ASYNC .`](https://beta.ruff.rs/docs/rules/open-sleep-or-subprocess-in-async-function)
2	ASYNC101	[ ] Async functions should not call `open`, `time.sleep`, or `subprocess` methods
@Dreamsorcerer
Copy link
Member

Not sure why the change to examples has disappeared?

@cclauss
Copy link
Contributor Author

cclauss commented Jul 23, 2023

I removed it because the current CI requires adding both aiofiles and aiofiles-types to various requirements files which became part of comments in #7383 (review) After this PR lands, I can re-create the other so the steps are clear.

@Dreamsorcerer Dreamsorcerer added backport-3.8 backport-3.9 Trigger automatic backporting to the 3.9 release branch by Patchback robot and removed backport-3.8 labels Jul 24, 2023
@Dreamsorcerer Dreamsorcerer enabled auto-merge (squash) July 24, 2023 22:30
@Dreamsorcerer Dreamsorcerer enabled auto-merge (squash) July 24, 2023 22:31
@Dreamsorcerer Dreamsorcerer added the bot:chronographer:skip This PR does not need to include a change note label Jul 24, 2023
@Dreamsorcerer Dreamsorcerer merged commit 0b34147 into aio-libs:master Jul 24, 2023
29 of 32 checks passed
@cclauss cclauss deleted the patch-1 branch July 25, 2023 01:50
@aio-libs aio-libs deleted a comment from patchback bot Jul 25, 2023
@Dreamsorcerer Dreamsorcerer added backport:skip Skip backport bot and removed backport-3.9 Trigger automatic backporting to the 3.9 release branch by Patchback robot labels Jul 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip Skip backport bot bot:chronographer:skip This PR does not need to include a change note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants