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

Remap ChainMap, Counter, and OrderedDict imports to collections #3392

Merged
merged 1 commit into from
Mar 7, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/ruff/resources/test/fixtures/pyupgrade/UP035.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
Good,
)

from typing import Callable, Match, Pattern, List
from typing import Callable, Match, Pattern, List, OrderedDict

if True: from collections import (
Mapping, Counter)
Expand Down
14 changes: 12 additions & 2 deletions crates/ruff/src/rules/pyupgrade/rules/import_replacements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,9 @@ const TYPING_TO_COLLECTIONS_ABC_39: &[&str] = &[
"AsyncIterator",
"Awaitable",
"ByteString",
"ChainMap",
"Collection",
"Container",
"Coroutine",
"Counter",
"Generator",
"Hashable",
"ItemsView",
Expand All @@ -156,6 +154,9 @@ const TYPING_TO_COLLECTIONS_ABC_39: &[&str] = &[
"ValuesView",
];

// Members of `typing` that have been moved to `collections`.
const TYPING_TO_COLLECTIONS_39: &[&str] = &["ChainMap", "Counter", "OrderedDict"];

// Members of `typing` that have been moved to `typing.re`.
const TYPING_TO_RE_39: &[&str] = &["Match", "Pattern"];

Expand Down Expand Up @@ -301,6 +302,15 @@ impl<'a> ImportReplacer<'a> {
replacements.push(replacement);
}

// `typing` to `collections`
let mut typing_to_collections = vec![];
if self.version >= PythonVersion::Py39 {
typing_to_collections.extend(TYPING_TO_COLLECTIONS_39);
}
if let Some(replacement) = self.try_replace(&typing_to_collections, "collections") {
replacements.push(replacement);
}

// `typing` to `re`
let mut typing_to_re = vec![];
if self.version >= PythonVersion::Py39 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,36 @@ expression: diagnostics
column: 0
end_location:
row: 44
column: 49
column: 62
fix:
content: "from typing import Match, Pattern, List\nfrom collections.abc import Callable"
content: "from typing import Match, Pattern, List, OrderedDict\nfrom collections.abc import Callable"
location:
row: 44
column: 0
end_location:
row: 44
column: 49
column: 62
parent: ~
- kind:
ImportReplacements:
module: collections
members:
- OrderedDict
fixable: true
location:
row: 44
column: 0
end_location:
row: 44
column: 62
fix:
content: "from typing import Callable, Match, Pattern, List\nfrom collections import OrderedDict"
location:
row: 44
column: 0
end_location:
row: 44
column: 62
parent: ~
- kind:
ImportReplacements:
Expand All @@ -332,15 +353,15 @@ expression: diagnostics
column: 0
end_location:
row: 44
column: 49
column: 62
fix:
content: "from typing import Callable, List\nfrom re import Match, Pattern"
content: "from typing import Callable, List, OrderedDict\nfrom re import Match, Pattern"
location:
row: 44
column: 0
end_location:
row: 44
column: 49
column: 62
parent: ~
- kind:
ImportReplacements:
Expand Down