mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:25:17 +00:00

## Summary Add UP041 to replace `TimeoutError` aliases: * Python 3.10+: `socket.timeout` * Python 3.11+: `asyncio.TimeoutError` Re: * https://github.com/asottile/pyupgrade#timeouterror-aliases * https://docs.python.org/3/library/asyncio-exceptions.html#asyncio.TimeoutError * https://docs.python.org/3/library/socket.html#socket.timeout Based on `os_error_alias.rs`. ## Test Plan <!-- How was it tested? --> By running: ``` cargo clippy --workspace --all-targets --all-features -- -D warnings # Rust linting RUFF_UPDATE_SCHEMA=1 cargo test # Rust testing and updating ruff.schema.json pre-commit run --all-files --show-diff-on-failure # Rust and Python formatting, Markdown and Python linting, etc. cargo insta review ``` And also running with different `--target-version` values: ```sh cargo run -p ruff_cli -- check crates/ruff_linter/resources/test/fixtures/pyupgrade/UP041.py --no-cache --select UP041 --target-version py37 --diff cargo run -p ruff_cli -- check crates/ruff_linter/resources/test/fixtures/pyupgrade/UP041.py --no-cache --select UP041 --target-version py310 --diff cargo run -p ruff_cli -- check crates/ruff_linter/resources/test/fixtures/pyupgrade/UP041.py --no-cache --select UP041 --target-version py311 --diff ```
68 lines
884 B
Python
68 lines
884 B
Python
import asyncio, socket
|
|
# These should be fixed
|
|
try:
|
|
pass
|
|
except asyncio.TimeoutError:
|
|
pass
|
|
|
|
try:
|
|
pass
|
|
except socket.timeout:
|
|
pass
|
|
|
|
# Should NOT be in parentheses when replaced
|
|
|
|
try:
|
|
pass
|
|
except (asyncio.TimeoutError,):
|
|
pass
|
|
|
|
try:
|
|
pass
|
|
except (socket.timeout,):
|
|
pass
|
|
|
|
try:
|
|
pass
|
|
except (asyncio.TimeoutError, socket.timeout,):
|
|
pass
|
|
|
|
# Should be kept in parentheses (because multiple)
|
|
|
|
try:
|
|
pass
|
|
except (asyncio.TimeoutError, socket.timeout, KeyError, TimeoutError):
|
|
pass
|
|
|
|
# First should change, second should not
|
|
|
|
from .mmap import error
|
|
try:
|
|
pass
|
|
except (asyncio.TimeoutError, error):
|
|
pass
|
|
|
|
# These should not change
|
|
|
|
from foo import error
|
|
|
|
try:
|
|
pass
|
|
except (TimeoutError, error):
|
|
pass
|
|
|
|
try:
|
|
pass
|
|
except:
|
|
pass
|
|
|
|
try:
|
|
pass
|
|
except TimeoutError:
|
|
pass
|
|
|
|
|
|
try:
|
|
pass
|
|
except (TimeoutError, KeyError):
|
|
pass
|