Skip to content

Commit a51b0b0

Browse files
Treat os.error as an OSError alias (#7582)
Closes #7580.
1 parent 74dbd87 commit a51b0b0

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

crates/ruff_linter/resources/test/fixtures/pyupgrade/UP024_0.py

+9
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,12 @@ def get_owner_id_from_mac_address():
104104
mac_address = get_primary_mac_address()
105105
except(IOError, OSError) as ex:
106106
msg = 'Unable to query URL to get Owner ID: {u}\n{e}'.format(u=owner_id_url, e=ex)
107+
108+
109+
# Regression test for: https://github.com/astral-sh/ruff/issues/7580
110+
import os
111+
112+
try:
113+
pass
114+
except os.error:
115+
pass

crates/ruff_linter/src/rules/pyupgrade/rules/os_error_alias.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fn is_alias(expr: &Expr, semantic: &SemanticModel) -> bool {
6161
matches!(
6262
call_path.as_slice(),
6363
["", "EnvironmentError" | "IOError" | "WindowsError"]
64-
| ["mmap" | "select" | "socket", "error"]
64+
| ["mmap" | "select" | "socket" | "os", "error"]
6565
)
6666
})
6767
}

crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP024_0.py.snap

+20
Original file line numberDiff line numberDiff line change
@@ -281,5 +281,25 @@ UP024_0.py:105:11: UP024 [*] Replace aliased errors with `OSError`
281281
105 |- except(IOError, OSError) as ex:
282282
105 |+ except OSError as ex:
283283
106 106 | msg = 'Unable to query URL to get Owner ID: {u}\n{e}'.format(u=owner_id_url, e=ex)
284+
107 107 |
285+
108 108 |
286+
287+
UP024_0.py:114:8: UP024 [*] Replace aliased errors with `OSError`
288+
|
289+
112 | try:
290+
113 | pass
291+
114 | except os.error:
292+
| ^^^^^^^^ UP024
293+
115 | pass
294+
|
295+
= help: Replace `os.error` with builtin `OSError`
296+
297+
Fix
298+
111 111 |
299+
112 112 | try:
300+
113 113 | pass
301+
114 |-except os.error:
302+
114 |+except OSError:
303+
115 115 | pass
284304

285305

0 commit comments

Comments
 (0)