[RUF071] Add rule for asyncio.gather#23612
Conversation
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| RUF071 | 1 | 1 | 0 | 0 | 0 |
|
Sry about the test fail. It seems with a global |
|
Thanks for working on this! Is this related to any open issue/discussion? We may need to decide if we want to include this rule before implementing it. I'm slightly surprised to see that flake8-async doesn't have a rule for this if it's a common async anti-pattern, but maybe Amy can speak better to that. |
|
@ntBre No I don't have a discussion/issue on this. This came basically from one of my clients where they used it everywhere (without understanding it really) and the result you see in this MR. I don't know about "common" though, I personally would be surprised if specifically |
|
I didn't think this would be a common pattern, but it looks 30k+ results on github search: https://github.com/search?q=%2Fgather%28.*return_exceptions%3DTrue.*%29%2F+AND+NOT+%2F%3D.*gather%2F&type=code That said, I do believe there are many cases where this pattern would be ok, eg, when the tasks/coroutines need to finish to completion even when one of them fails, but nothing needs to explicitly check their results. What the caller does with the results is purely dependent on the context of the tasks being executed, and IMO, if it's ok to await a regular gather without checking results, then it should be equally ok to await a gather with |
|
I agree there are situations where it's absolutely okay to ignore exceptions. My best example for this would be the shutdown of an application and finishing 15 async-tasks. And even there one could argue that the Personally I think that to enforce adding In situations where it shouldn't be ignored, it can be really hard to debug. Also because the syntax is nothing that stands out when skim code compared to a For me the benefit is higher as the cost here. Furthermore I fear that going forward vibe coded slop will become more prevalent and (also without vibe coding) there will be people who have no clue what the details of this api are. In regards to what ruff also already has I see this rule as a extension of rules that it has regarding exceptions, especially S110/S112 which both basically amount to "don't ignore exceptions". |
This MR adds a new rule,
RUF071,IgnoredAsyncioGatherResultsWithException. A name which I'm not really happy with, but I cannot think of a better one. Maybe you have ideas?Summary
The rule should help users from accidentally ignoring exceptions when using
asyncio.gather(..., return_exceptions=True).Example
The rule specifically checks for
return_exceptions=True(i.e. is constantTrue). An alternative would be!Falsebut I think of the parameter more as a "Developer decides at implementation" and can't think of a reason why anyone would want "sometimes with exception-as-values" and sometimes not. Therefore I opted for the "less false-positives" option, I'm open for that discussion though.Test Plan
Added snapshots for
RUF071