Skip to content

Conversation

julianoes
Copy link
Collaborator

Closes #771.

@cclauss

Copy link
Contributor

@cclauss cclauss left a comment

Choose a reason for hiding this comment

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

Nice! LGTM.

Some OPTIONAL microoptimizations for your consideration.

@cclauss
Copy link
Contributor

cclauss commented Sep 4, 2025

astral-sh/ruff#20122 Was merged into the just released ˋruff` v0.12.12.

@julianoes julianoes force-pushed the pr-ruff-fixes branch 2 times, most recently from 7a8fc35 to 2903da1 Compare September 4, 2025 20:15
@@ -24,7 +24,7 @@ async def run():
drone = System()
await drone.connect(system_address="udpin://0.0.0.0:14540")

tasks = [
_tasks = [
Copy link
Contributor

@cclauss cclauss Sep 5, 2025

Choose a reason for hiding this comment

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

It is unusual to assemble these tasks but not await them. Are we certain that they will be run at some point?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well, so this code was working fine but ruff complained that the tasks were dangling: https://docs.astral.sh/ruff/rules/asyncio-dangling-task/.

So, the advice was to create a strong reference to the tasks. However, now ruff complains that tasks are assigned but never used. It feels like you can't win.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@cclauss you need to actually do a read&write conversation, not just write only please.

Copy link
Contributor

@cclauss cclauss Sep 9, 2025

Choose a reason for hiding this comment

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

I was wrong... This usage is correct.

>>> import asyncio
>>> async def print_char(char: str) -> None:
...     print(char)
...
>>> async def run(chars: str) -> None:
...     _tasks = [asyncio.create_task(print_char(char)) for char in chars]
...
>>> asyncio.run(run("abcdefg"))
a
b
c
d
e
f
g

@@ -27,7 +27,7 @@ jobs:
- name: Check style
run: |
pipx run pycodestyle examples/*
pipx run ruff check --select=ASYNC,RUF006
pipx run ruff check
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
pipx run ruff check
pipx run ruff check # default rules
pipx run ruff check --select=ASYNC,RUF006 # Special rules for asyncio

This changes things. Normally, ASYNC and RUF are off by default, so this change removes those rules and adds the default rules. https://docs.astral.sh/ruff/rules Let's run both just to be safe.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, I see. Thanks.

@cclauss cclauss mentioned this pull request Sep 9, 2025
Comment on lines +27 to +31
_tasks = [
asyncio.create_task(observe_current_settings(drone)),
asyncio.create_task(observe_camera_mode(drone)),
asyncio.create_task(observe_possible_setting_options(drone)),
]
Copy link
Contributor

Choose a reason for hiding this comment

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

This syntax would delete tasks as they are completed, as discussed in ruff rule RUF006.

Suggested change
_tasks = [
asyncio.create_task(observe_current_settings(drone)),
asyncio.create_task(observe_camera_mode(drone)),
asyncio.create_task(observe_possible_setting_options(drone)),
]
tasks = {
asyncio.create_task(observe_current_settings(drone)),
asyncio.create_task(observe_camera_mode(drone)),
asyncio.create_task(observe_possible_setting_options(drone)),
}
for task in tasks:
task.add_done_callback(tasks.discard)

Copy link
Collaborator Author

@julianoes julianoes 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 that's an improvement, let's get it in before I have to fix more conflicts.

@julianoes julianoes merged commit 50def24 into main Sep 9, 2025
9 checks passed
@julianoes julianoes deleted the pr-ruff-fixes branch September 9, 2025 23:07
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.

Linting issues for asyncio
2 participants