-
Notifications
You must be signed in to change notification settings - Fork 10
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
Codemod to fix asyncio.Task
#248
Conversation
af18bad
to
5bdc3e8
Compare
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.
Just a small correction on converting tasks with eager_start
parameter.
""" | ||
import asyncio | ||
asyncio.Task(coro(1, 2), name='task', eager_start=True) | ||
""", | ||
""" | ||
import asyncio | ||
asyncio.create_task(coro(1, 2), name='task', eager_start=True) | ||
""", |
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.
This changes the intended behavior of the code. The eager_start
argument has seemingly no effect on create_task
.
I think the ayncio.eager_task_factory
is the intended way to create eager tasks.
https://docs.python.org/3/library/asyncio-task.html#eager-task-factory
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.
ah interesting. I only used eager_task as a kwarg test example to demonstrate we correctly handle kwargs. I'll see if I use another kwarg or if we need to special case for eager_task
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.
@andrecsilva could you double check my understanding of the docs, since it's a tiny bit confusing. Seems like eager_start is new so not a ton of use cases yet out there.
So if I understand correctly, if you want to create a Task()
with eager_start=True, the code would be
loop = asyncio.get_running_loop()
task = asyncio.Task(my_coroutine(), loop=loop, eager_start=True)
(must pass in a loop otherwise it won't work)
Our replacement could either be
loop = asyncio.get_running_loop()
loop.set_task_factory(asyncio.eager_task_factory)
task = loop.create_task(my_coroutine())
which is kinda what the docs suggest? but this also works:
loop = asyncio.get_running_loop()
task = asyncio.eager_task_factory(loop, my_coroutine())
though I"m not 100% sure they're totally equivalent.
I'm a tiny bit leaning to not support this right now if we aren't sure, but maybe I'm missing something today.
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 is also my understanding from reading the documentation.
I've asked @bdoyal to take a look at the test cases just as another sanity check to make sure we're producing correct code. |
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.
This is a very nice codemod overall. I agree with @andrecsilva that it looks like we need to handle eager tasks slightly differently. Hopefully that's not too difficult. Also I requested some small changes to the metadata.
class FixTaskInstantiation(SimpleCodemod, NameAndAncestorResolutionMixin): | ||
metadata = Metadata( | ||
name="fix-task-instantiation", | ||
summary="Use high-level `asyncio.create_task` API", |
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.
summary="Use high-level `asyncio.create_task` API", | |
summary="Use `asyncio.create_task` API to instantiate `asyncio.Task`", |
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.
@drdavella I don't think we should use your exact summary wording bc the codemod may change to not only asyncio.create_task
, but also to some_loop.create_task
and now to asyncio.eager_task_factory
which is why I just said high-level API.
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.
How about Use high-level
asyncio APIs to instantiate tasks
?
Also I think our convention is to use title case for the summary.
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.
Nvm you basically did that already. I didn't even notice 🤦
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.
@drdavella asked me to take a look at the aysnc inputs/outputs in one of the test files, so I'm just here to leave comments on that.
5bdc3e8
to
07cfdb2
Compare
07cfdb2
to
5f2813b
Compare
latest commits add support for |
5f2813b
to
5190758
Compare
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.
Just asking for one more very minor change but otherwise looks 👍
), | ||
], | ||
) | ||
change_description = "Replace instantiation of `asyncio.Task` higher-level functions to create tasks." |
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.
change_description = "Replace instantiation of `asyncio.Task` higher-level functions to create tasks." | |
change_description = "Replace instantiation of `asyncio.Task` with higher-level functions to create tasks." |
5190758
to
6ce9160
Compare
Quality Gate passedIssues Measures |
Overview
Replace manual init of asyncio.Task with
create_task
Description
loop=not-a-loop
in which I left without changes, I can update accordingly if we do want to change it.