Skip to content

Commit 33468cc

Browse files
authored
[pyupgrade] Apply UP035 only on py313+ for get_type_hints() (#18476)
1 parent 8531f4b commit 33468cc

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@
110110
# UP035 on py313+ only
111111
from typing_extensions import deprecated
112112

113+
# UP035 on py313+ only
114+
from typing_extensions import get_type_hints
113115

114116
# https://github.com/astral-sh/ruff/issues/15780
115117
from typing_extensions import is_typeddict

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ const TYPING_TO_RE_39: &[&str] = &["Match", "Pattern"];
271271
const TYPING_RE_TO_RE_39: &[&str] = &["Match", "Pattern"];
272272

273273
// Members of `typing_extensions` that were moved to `typing`.
274-
const TYPING_EXTENSIONS_TO_TYPING_39: &[&str] = &["Annotated", "get_type_hints"];
274+
const TYPING_EXTENSIONS_TO_TYPING_39: &[&str] = &["Annotated"];
275275

276276
// Members of `typing` that were moved _and_ renamed (and thus cannot be
277277
// automatically fixed).
@@ -373,6 +373,9 @@ const TYPING_EXTENSIONS_TO_TYPING_313: &[&str] = &[
373373
"NoDefault",
374374
"ReadOnly",
375375
"TypeIs",
376+
// Introduced in Python 3.5,
377+
// but typing_extensions backports features from py313:
378+
"get_type_hints",
376379
// Introduced in Python 3.6,
377380
// but typing_extensions backports features from py313:
378381
"ContextManager",

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,8 @@ UP035.py:111:1: UP035 [*] Import from `warnings` instead: `deprecated`
11791179
110 | # UP035 on py313+ only
11801180
111 | from typing_extensions import deprecated
11811181
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UP035
1182+
112 |
1183+
113 | # UP035 on py313+ only
11821184
|
11831185
= help: Import from `warnings`
11841186

@@ -1189,5 +1191,25 @@ UP035.py:111:1: UP035 [*] Import from `warnings` instead: `deprecated`
11891191
111 |-from typing_extensions import deprecated
11901192
111 |+from warnings import deprecated
11911193
112 112 |
1192-
113 113 |
1193-
114 114 | # https://github.com/astral-sh/ruff/issues/15780
1194+
113 113 | # UP035 on py313+ only
1195+
114 114 | from typing_extensions import get_type_hints
1196+
1197+
UP035.py:114:1: UP035 [*] Import from `typing` instead: `get_type_hints`
1198+
|
1199+
113 | # UP035 on py313+ only
1200+
114 | from typing_extensions import get_type_hints
1201+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UP035
1202+
115 |
1203+
116 | # https://github.com/astral-sh/ruff/issues/15780
1204+
|
1205+
= help: Import from `typing`
1206+
1207+
ℹ Safe fix
1208+
111 111 | from typing_extensions import deprecated
1209+
112 112 |
1210+
113 113 | # UP035 on py313+ only
1211+
114 |-from typing_extensions import get_type_hints
1212+
114 |+from typing import get_type_hints
1213+
115 115 |
1214+
116 116 | # https://github.com/astral-sh/ruff/issues/15780
1215+
117 117 | from typing_extensions import is_typeddict

0 commit comments

Comments
 (0)