Skip to content

Commit eaa10ad

Browse files
authored
Fix deprecated-import false positives (#5291)
## Summary Remove recommendations to replace `typing_extensions.dataclass_transform` and `typing_extensions.SupportsIndex` with their `typing` library counterparts. Closes #5112. ## Test Plan Added extra checks to the test fixture. `cargo test`
1 parent 84259f5 commit eaa10ad

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,12 @@
4848

4949
# OK
5050
from a import b
51+
52+
# Ok: `typing_extensions` contains backported improvements.
53+
from typing_extensions import SupportsIndex
54+
55+
# Ok: `typing_extensions` contains backported improvements.
56+
from typing_extensions import NamedTuple
57+
58+
# Ok: `typing_extensions` supports `frozen_default` (backported from 3.12).
59+
from typing_extensions import dataclass_transform

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

+18-8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ enum Deprecation {
4343
/// Deprecated imports may be removed in future versions of Python, and
4444
/// should be replaced with their new equivalents.
4545
///
46+
/// Note that, in some cases, it may be preferable to continue importing
47+
/// members from `typing_extensions` even after they're added to the Python
48+
/// standard library, as `typing_extensions` can backport bugfixes and
49+
/// optimizations from later Python versions. This rule thus avoids flagging
50+
/// imports from `typing_extensions` in such cases.
51+
///
4652
/// ## Example
4753
/// ```python
4854
/// from collections import Sequence
@@ -139,10 +145,12 @@ const TYPING_EXTENSIONS_TO_TYPING: &[&str] = &[
139145
"ContextManager",
140146
"Coroutine",
141147
"DefaultDict",
142-
"NewType",
143148
"TYPE_CHECKING",
144149
"Text",
145150
"Type",
151+
// Introduced in Python 3.5.2, but `typing_extensions` contains backported bugfixes and
152+
// optimizations,
153+
// "NewType",
146154
];
147155

148156
// Python 3.7+
@@ -168,11 +176,13 @@ const MYPY_EXTENSIONS_TO_TYPING_38: &[&str] = &["TypedDict"];
168176
// Members of `typing_extensions` that were moved to `typing`.
169177
const TYPING_EXTENSIONS_TO_TYPING_38: &[&str] = &[
170178
"Final",
171-
"Literal",
172179
"OrderedDict",
173-
"Protocol",
174-
"SupportsIndex",
175180
"runtime_checkable",
181+
// Introduced in Python 3.8, but `typing_extensions` contains backported bugfixes and
182+
// optimizations.
183+
// "Literal",
184+
// "Protocol",
185+
// "SupportsIndex",
176186
];
177187

178188
// Python 3.9+
@@ -243,6 +253,8 @@ const TYPING_TO_COLLECTIONS_ABC_310: &[&str] = &["Callable"];
243253
// Members of `typing_extensions` that were moved to `typing`.
244254
const TYPING_EXTENSIONS_TO_TYPING_310: &[&str] = &[
245255
"Concatenate",
256+
"Literal",
257+
"NewType",
246258
"ParamSpecArgs",
247259
"ParamSpecKwargs",
248260
"TypeAlias",
@@ -258,21 +270,19 @@ const TYPING_EXTENSIONS_TO_TYPING_310: &[&str] = &[
258270
const TYPING_EXTENSIONS_TO_TYPING_311: &[&str] = &[
259271
"Any",
260272
"LiteralString",
261-
"NamedTuple",
262273
"Never",
263274
"NotRequired",
264275
"Required",
265276
"Self",
266-
"TypedDict",
267-
"Unpack",
268277
"assert_never",
269278
"assert_type",
270279
"clear_overloads",
271-
"dataclass_transform",
272280
"final",
273281
"get_overloads",
274282
"overload",
275283
"reveal_type",
284+
// Introduced in Python 3.11, but `typing_extensions` backports the `frozen_default` argument.
285+
// "dataclass_transform",
276286
];
277287

278288
struct ImportReplacer<'a> {

0 commit comments

Comments
 (0)