Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deprecated-import false positives #5291

Merged
merged 5 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP035.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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