Skip to content

Commit

Permalink
Fix deprecated-import false positives (#5291)
Browse files Browse the repository at this point in the history
## 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`
  • Loading branch information
tjkuson committed Jun 22, 2023
1 parent 84259f5 commit eaa10ad
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
9 changes: 9 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP035.py
Expand Up @@ -48,3 +48,12 @@

# OK
from a import b

# Ok: `typing_extensions` contains backported improvements.
from typing_extensions import SupportsIndex

# Ok: `typing_extensions` contains backported improvements.
from typing_extensions import NamedTuple

# Ok: `typing_extensions` supports `frozen_default` (backported from 3.12).
from typing_extensions import dataclass_transform
26 changes: 18 additions & 8 deletions crates/ruff/src/rules/pyupgrade/rules/deprecated_import.rs
Expand Up @@ -43,6 +43,12 @@ enum Deprecation {
/// Deprecated imports may be removed in future versions of Python, and
/// should be replaced with their new equivalents.
///
/// Note that, in some cases, it may be preferable to continue importing
/// members from `typing_extensions` even after they're added to the Python
/// standard library, as `typing_extensions` can backport bugfixes and
/// optimizations from later Python versions. This rule thus avoids flagging
/// imports from `typing_extensions` in such cases.
///
/// ## Example
/// ```python
/// from collections import Sequence
Expand Down Expand Up @@ -139,10 +145,12 @@ const TYPING_EXTENSIONS_TO_TYPING: &[&str] = &[
"ContextManager",
"Coroutine",
"DefaultDict",
"NewType",
"TYPE_CHECKING",
"Text",
"Type",
// Introduced in Python 3.5.2, but `typing_extensions` contains backported bugfixes and
// optimizations,
// "NewType",
];

// Python 3.7+
Expand All @@ -168,11 +176,13 @@ const MYPY_EXTENSIONS_TO_TYPING_38: &[&str] = &["TypedDict"];
// Members of `typing_extensions` that were moved to `typing`.
const TYPING_EXTENSIONS_TO_TYPING_38: &[&str] = &[
"Final",
"Literal",
"OrderedDict",
"Protocol",
"SupportsIndex",
"runtime_checkable",
// Introduced in Python 3.8, but `typing_extensions` contains backported bugfixes and
// optimizations.
// "Literal",
// "Protocol",
// "SupportsIndex",
];

// Python 3.9+
Expand Down Expand Up @@ -243,6 +253,8 @@ const TYPING_TO_COLLECTIONS_ABC_310: &[&str] = &["Callable"];
// Members of `typing_extensions` that were moved to `typing`.
const TYPING_EXTENSIONS_TO_TYPING_310: &[&str] = &[
"Concatenate",
"Literal",
"NewType",
"ParamSpecArgs",
"ParamSpecKwargs",
"TypeAlias",
Expand All @@ -258,21 +270,19 @@ const TYPING_EXTENSIONS_TO_TYPING_310: &[&str] = &[
const TYPING_EXTENSIONS_TO_TYPING_311: &[&str] = &[
"Any",
"LiteralString",
"NamedTuple",
"Never",
"NotRequired",
"Required",
"Self",
"TypedDict",
"Unpack",
"assert_never",
"assert_type",
"clear_overloads",
"dataclass_transform",
"final",
"get_overloads",
"overload",
"reveal_type",
// Introduced in Python 3.11, but `typing_extensions` backports the `frozen_default` argument.
// "dataclass_transform",
];

struct ImportReplacer<'a> {
Expand Down

0 comments on commit eaa10ad

Please sign in to comment.