-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refine pyupgrade's TimeoutErrorAlias lint (UP041) to remove false pos…
…itives (#8587) Previously, this lint had its alias detection logic a little backwards. That is, for Python 3.11+, it would *only* detect asyncio.TimeoutError as an alias, but it should have also detected socket.timeout as an alias. And in Python <3.11, it would falsely detect asyncio.TimeoutError as an alias where it should have only detected socket.timeout as an alias. We fix it so that both asyncio.TimeoutError and socket.timeout are detected as aliases in Python 3.11+, and only socket.timeout is detected as an alias in Python 3.10. Fixes #8565 ## Test Plan I tested this by updating the existing snapshot test which had erroneously asserted that socket.timeout should not be replaced with TimeoutError in Python 3.11+. I also added a new regression test that targets Python 3.10 and ensures that the suggestion to replace asyncio.TimeoutError with TimeoutError does not occur.
- Loading branch information
1 parent
036b6bc
commit a7dbe9d
Showing
4 changed files
with
150 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...ts/ruff_linter__rules__pyupgrade__tests__async_timeout_error_alias_not_applied_py310.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pyupgrade/mod.rs | ||
--- | ||
UP041.py:10:8: UP041 [*] Replace aliased errors with `TimeoutError` | ||
| | ||
8 | try: | ||
9 | pass | ||
10 | except socket.timeout: | ||
| ^^^^^^^^^^^^^^ UP041 | ||
11 | pass | ||
| | ||
= help: Replace `socket.timeout` with builtin `TimeoutError` | ||
|
||
ℹ Safe fix | ||
7 7 | | ||
8 8 | try: | ||
9 9 | pass | ||
10 |-except socket.timeout: | ||
10 |+except TimeoutError: | ||
11 11 | pass | ||
12 12 | | ||
13 13 | # Should NOT be in parentheses when replaced | ||
|
||
UP041.py:22:8: UP041 [*] Replace aliased errors with `TimeoutError` | ||
| | ||
20 | try: | ||
21 | pass | ||
22 | except (socket.timeout,): | ||
| ^^^^^^^^^^^^^^^^^ UP041 | ||
23 | pass | ||
| | ||
= help: Replace with builtin `TimeoutError` | ||
|
||
ℹ Safe fix | ||
19 19 | | ||
20 20 | try: | ||
21 21 | pass | ||
22 |-except (socket.timeout,): | ||
22 |+except TimeoutError: | ||
23 23 | pass | ||
24 24 | | ||
25 25 | try: | ||
|
||
UP041.py:27:8: UP041 [*] Replace aliased errors with `TimeoutError` | ||
| | ||
25 | try: | ||
26 | pass | ||
27 | except (asyncio.TimeoutError, socket.timeout,): | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UP041 | ||
28 | pass | ||
| | ||
= help: Replace with builtin `TimeoutError` | ||
|
||
ℹ Safe fix | ||
24 24 | | ||
25 25 | try: | ||
26 26 | pass | ||
27 |-except (asyncio.TimeoutError, socket.timeout,): | ||
27 |+except (TimeoutError, asyncio.TimeoutError): | ||
28 28 | pass | ||
29 29 | | ||
30 30 | # Should be kept in parentheses (because multiple) | ||
|
||
UP041.py:34:8: UP041 [*] Replace aliased errors with `TimeoutError` | ||
| | ||
32 | try: | ||
33 | pass | ||
34 | except (asyncio.TimeoutError, socket.timeout, KeyError, TimeoutError): | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UP041 | ||
35 | pass | ||
| | ||
= help: Replace with builtin `TimeoutError` | ||
|
||
ℹ Safe fix | ||
31 31 | | ||
32 32 | try: | ||
33 33 | pass | ||
34 |-except (asyncio.TimeoutError, socket.timeout, KeyError, TimeoutError): | ||
34 |+except (asyncio.TimeoutError, KeyError, TimeoutError): | ||
35 35 | pass | ||
36 36 | | ||
37 37 | # First should change, second should not | ||
|
||
|