Revert "Add all PEP-585 names to UP006 rule" (#15250)

This commit is contained in:
Micha Reiser 2025-01-04 12:23:53 +01:00 committed by GitHub
parent baf0d660eb
commit e4d9fe036a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 19 additions and 649 deletions

View file

@ -2126,67 +2126,3 @@ unfixable = ["RUF"]
Ok(())
}
#[test]
fn verbose_show_failed_fix_errors() {
let mut cmd = RuffCheck::default()
.args(["--select", "UP006", "--preview", "-v"])
.build();
insta::with_settings!(
{
// the logs have timestamps we need to remove
filters => vec![(
r"\[[\d:-]+]",
""
)]
},{
assert_cmd_snapshot!(cmd
.pass_stdin("import typing\nCallable = 'abc'\ndef g() -> typing.Callable: ..."),
@r###"
success: false
exit_code: 1
----- stdout -----
-:3:12: UP006 Use `collections.abc.Callable` instead of `typing.Callable` for type annotation
|
1 | import typing
2 | Callable = 'abc'
3 | def g() -> typing.Callable: ...
| ^^^^^^^^^^^^^^^ UP006
|
= help: Replace with `collections.abc.Callable`
Found 1 error.
----- stderr -----
[ruff::resolve][DEBUG] Isolated mode, not reading any pyproject.toml
[ruff_diagnostics::diagnostic][DEBUG] Failed to create fix for NonPEP585Annotation: Unable to insert `Callable` into scope due to name conflict
"###); }
);
}
#[test]
fn no_verbose_hide_failed_fix_errors() {
let mut cmd = RuffCheck::default()
.args(["--select", "UP006", "--preview"])
.build();
assert_cmd_snapshot!(cmd
.pass_stdin("import typing\nCallable = 'abc'\ndef g() -> typing.Callable: ..."),
@r###"
success: false
exit_code: 1
----- stdout -----
-:3:12: UP006 Use `collections.abc.Callable` instead of `typing.Callable` for type annotation
|
1 | import typing
2 | Callable = 'abc'
3 | def g() -> typing.Callable: ...
| ^^^^^^^^^^^^^^^ UP006
|
= help: Replace with `collections.abc.Callable`
Found 1 error.
----- stderr -----
"###);
}

View file

@ -64,22 +64,3 @@ def f(x: typing.Deque[str]) -> None:
def f(x: typing.DefaultDict[str, str]) -> None:
...
def f(x: typing.AbstractSet[str]) -> None:
...
def f(x: typing.Pattern[str]) -> None:
...
def f(x: typing.Sequence[str]) -> None:
...
from typing import Collection
def f(x: typing.Collection[str]) -> None:
...

View file

@ -8,19 +8,3 @@ if typing.TYPE_CHECKING:
def f(x: typing.DefaultDict[str, str]) -> None:
...
from collections.abc import Set
from typing_extensions import Awaitable
def f(x: typing.AbstractSet[str]) -> None:
...
def f(x: Set) -> None:
...
def f(x: Awaitable) -> None:
...

View file

@ -111,21 +111,6 @@ mod tests {
Ok(())
}
#[test_case(Rule::NonPEP585Annotation, Path::new("UP006_0.py"))]
#[test_case(Rule::NonPEP585Annotation, Path::new("UP006_1.py"))]
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("preview__{}", path.to_string_lossy());
let diagnostics = test_path(
Path::new("pyupgrade").join(path).as_path(),
&settings::LinterSettings {
preview: PreviewMode::Enabled,
..settings::LinterSettings::for_rule(rule_code)
},
)?;
assert_messages!(snapshot, diagnostics);
Ok(())
}
#[test]
fn async_timeout_error_alias_not_applied_py310() -> Result<()> {
let diagnostics = test_path(

View file

@ -14,21 +14,6 @@ use crate::settings::types::PythonVersion;
/// Checks for the use of generics that can be replaced with standard library
/// variants based on [PEP 585].
///
/// Under [preview mode](https://docs.astral.sh/ruff/preview),
/// this rule triggers for all replacements listed
/// in [PEP 585]. Otherwise, this rule only triggers for the following
/// commonly occurring instances of modules present in the
/// `typing` or `typing_extensions` package:
///
/// - `Dict`
/// - `FrozenSet`
/// - `List`
/// - `Set`
/// - `Tuple`
/// - `Type`
/// - `Deque`
/// - `DefaultDict`
///
/// ## Why is this bad?
/// [PEP 585] enabled collections in the Python standard library (like `list`)
/// to be used as generics directly, instead of importing analogous members
@ -96,9 +81,6 @@ pub(crate) fn use_pep585_annotation(
expr: &Expr,
replacement: &ModuleMember,
) {
if !checker.settings.preview.is_enabled() && !is_restricted_pep585_generic(replacement) {
return;
}
let Some(from) = UnqualifiedName::from_expr(expr) else {
return;
};
@ -156,11 +138,3 @@ pub(crate) fn use_pep585_annotation(
}
checker.diagnostics.push(diagnostic);
}
fn is_restricted_pep585_generic(module_member: &ModuleMember) -> bool {
matches!(
module_member,
ModuleMember::BuiltIn("dict" | "frozenset" | "list" | "set" | "tuple" | "type")
| ModuleMember::Member("collections", "deque" | "defaultdict")
)
}

View file

@ -281,5 +281,3 @@ UP006_0.py:65:10: UP006 [*] Use `collections.defaultdict` instead of `typing.Def
65 |-def f(x: typing.DefaultDict[str, str]) -> None:
66 |+def f(x: defaultdict[str, str]) -> None:
66 67 | ...
67 68 |
68 69 |

View file

@ -17,5 +17,3 @@ UP006_1.py:9:10: UP006 [*] Use `collections.defaultdict` instead of `typing.Defa
9 |-def f(x: typing.DefaultDict[str, str]) -> None:
9 |+def f(x: defaultdict[str, str]) -> None:
10 10 | ...
11 11 |
12 12 |

View file

@ -1,370 +0,0 @@
---
source: crates/ruff_linter/src/rules/pyupgrade/mod.rs
---
UP006_0.py:4:10: UP006 [*] Use `list` instead of `typing.List` for type annotation
|
4 | def f(x: typing.List[str]) -> None:
| ^^^^^^^^^^^ UP006
5 | ...
|
= help: Replace with `list`
Safe fix
1 1 | import typing
2 2 |
3 3 |
4 |-def f(x: typing.List[str]) -> None:
4 |+def f(x: list[str]) -> None:
5 5 | ...
6 6 |
7 7 |
UP006_0.py:11:10: UP006 [*] Use `list` instead of `List` for type annotation
|
11 | def f(x: List[str]) -> None:
| ^^^^ UP006
12 | ...
|
= help: Replace with `list`
Safe fix
8 8 | from typing import List
9 9 |
10 10 |
11 |-def f(x: List[str]) -> None:
11 |+def f(x: list[str]) -> None:
12 12 | ...
13 13 |
14 14 |
UP006_0.py:18:10: UP006 [*] Use `list` instead of `t.List` for type annotation
|
18 | def f(x: t.List[str]) -> None:
| ^^^^^^ UP006
19 | ...
|
= help: Replace with `list`
Safe fix
15 15 | import typing as t
16 16 |
17 17 |
18 |-def f(x: t.List[str]) -> None:
18 |+def f(x: list[str]) -> None:
19 19 | ...
20 20 |
21 21 |
UP006_0.py:25:10: UP006 [*] Use `list` instead of `IList` for type annotation
|
25 | def f(x: IList[str]) -> None:
| ^^^^^ UP006
26 | ...
|
= help: Replace with `list`
Safe fix
22 22 | from typing import List as IList
23 23 |
24 24 |
25 |-def f(x: IList[str]) -> None:
25 |+def f(x: list[str]) -> None:
26 26 | ...
27 27 |
28 28 |
UP006_0.py:29:11: UP006 [*] Use `list` instead of `List` for type annotation
|
29 | def f(x: "List[str]") -> None:
| ^^^^ UP006
30 | ...
|
= help: Replace with `list`
Safe fix
26 26 | ...
27 27 |
28 28 |
29 |-def f(x: "List[str]") -> None:
29 |+def f(x: "list[str]") -> None:
30 30 | ...
31 31 |
32 32 |
UP006_0.py:33:12: UP006 [*] Use `list` instead of `List` for type annotation
|
33 | def f(x: r"List[str]") -> None:
| ^^^^ UP006
34 | ...
|
= help: Replace with `list`
Safe fix
30 30 | ...
31 31 |
32 32 |
33 |-def f(x: r"List[str]") -> None:
33 |+def f(x: r"list[str]") -> None:
34 34 | ...
35 35 |
36 36 |
UP006_0.py:37:11: UP006 [*] Use `list` instead of `List` for type annotation
|
37 | def f(x: "List[str]") -> None:
| ^^^^ UP006
38 | ...
|
= help: Replace with `list`
Safe fix
34 34 | ...
35 35 |
36 36 |
37 |-def f(x: "List[str]") -> None:
37 |+def f(x: "list[str]") -> None:
38 38 | ...
39 39 |
40 40 |
UP006_0.py:41:13: UP006 [*] Use `list` instead of `List` for type annotation
|
41 | def f(x: """List[str]""") -> None:
| ^^^^ UP006
42 | ...
|
= help: Replace with `list`
Safe fix
38 38 | ...
39 39 |
40 40 |
41 |-def f(x: """List[str]""") -> None:
41 |+def f(x: """list[str]""") -> None:
42 42 | ...
43 43 |
44 44 |
UP006_0.py:45:10: UP006 Use `list` instead of `List` for type annotation
|
45 | def f(x: "Li" "st[str]") -> None:
| ^^^^^^^^^^^^^^ UP006
46 | ...
|
= help: Replace with `list`
UP006_0.py:49:11: UP006 [*] Use `list` instead of `List` for type annotation
|
49 | def f(x: "List['List[str]']") -> None:
| ^^^^ UP006
50 | ...
|
= help: Replace with `list`
Safe fix
46 46 | ...
47 47 |
48 48 |
49 |-def f(x: "List['List[str]']") -> None:
49 |+def f(x: "list['List[str]']") -> None:
50 50 | ...
51 51 |
52 52 |
UP006_0.py:49:17: UP006 [*] Use `list` instead of `List` for type annotation
|
49 | def f(x: "List['List[str]']") -> None:
| ^^^^ UP006
50 | ...
|
= help: Replace with `list`
Safe fix
46 46 | ...
47 47 |
48 48 |
49 |-def f(x: "List['List[str]']") -> None:
49 |+def f(x: "List['list[str]']") -> None:
50 50 | ...
51 51 |
52 52 |
UP006_0.py:53:11: UP006 [*] Use `list` instead of `List` for type annotation
|
53 | def f(x: "List['Li' 'st[str]']") -> None:
| ^^^^ UP006
54 | ...
|
= help: Replace with `list`
Safe fix
50 50 | ...
51 51 |
52 52 |
53 |-def f(x: "List['Li' 'st[str]']") -> None:
53 |+def f(x: "list['Li' 'st[str]']") -> None:
54 54 | ...
55 55 |
56 56 |
UP006_0.py:53:16: UP006 Use `list` instead of `List` for type annotation
|
53 | def f(x: "List['Li' 'st[str]']") -> None:
| ^^^^^^^^^^^^^^ UP006
54 | ...
|
= help: Replace with `list`
UP006_0.py:57:10: UP006 Use `list` instead of `List` for type annotation
|
57 | def f(x: "Li" "st['List[str]']") -> None:
| ^^^^^^^^^^^^^^^^^^^^^^ UP006
58 | ...
|
= help: Replace with `list`
UP006_0.py:57:10: UP006 Use `list` instead of `List` for type annotation
|
57 | def f(x: "Li" "st['List[str]']") -> None:
| ^^^^^^^^^^^^^^^^^^^^^^ UP006
58 | ...
|
= help: Replace with `list`
UP006_0.py:61:10: UP006 [*] Use `collections.deque` instead of `typing.Deque` for type annotation
|
61 | def f(x: typing.Deque[str]) -> None:
| ^^^^^^^^^^^^ UP006
62 | ...
|
= help: Replace with `collections.deque`
Safe fix
20 20 |
21 21 |
22 22 | from typing import List as IList
23 |+from collections import deque
23 24 |
24 25 |
25 26 | def f(x: IList[str]) -> None:
--------------------------------------------------------------------------------
58 59 | ...
59 60 |
60 61 |
61 |-def f(x: typing.Deque[str]) -> None:
62 |+def f(x: deque[str]) -> None:
62 63 | ...
63 64 |
64 65 |
UP006_0.py:65:10: UP006 [*] Use `collections.defaultdict` instead of `typing.DefaultDict` for type annotation
|
65 | def f(x: typing.DefaultDict[str, str]) -> None:
| ^^^^^^^^^^^^^^^^^^ UP006
66 | ...
|
= help: Replace with `collections.defaultdict`
Safe fix
20 20 |
21 21 |
22 22 | from typing import List as IList
23 |+from collections import defaultdict
23 24 |
24 25 |
25 26 | def f(x: IList[str]) -> None:
--------------------------------------------------------------------------------
62 63 | ...
63 64 |
64 65 |
65 |-def f(x: typing.DefaultDict[str, str]) -> None:
66 |+def f(x: defaultdict[str, str]) -> None:
66 67 | ...
67 68 |
68 69 |
UP006_0.py:69:10: UP006 [*] Use `collections.abc.Set` instead of `typing.AbstractSet` for type annotation
|
69 | def f(x: typing.AbstractSet[str]) -> None:
| ^^^^^^^^^^^^^^^^^^ UP006
70 | ...
|
= help: Replace with `collections.abc.Set`
Safe fix
20 20 |
21 21 |
22 22 | from typing import List as IList
23 |+from collections.abc import Set
23 24 |
24 25 |
25 26 | def f(x: IList[str]) -> None:
--------------------------------------------------------------------------------
66 67 | ...
67 68 |
68 69 |
69 |-def f(x: typing.AbstractSet[str]) -> None:
70 |+def f(x: Set[str]) -> None:
70 71 | ...
71 72 |
72 73 |
UP006_0.py:73:10: UP006 [*] Use `re.Pattern` instead of `typing.Pattern` for type annotation
|
73 | def f(x: typing.Pattern[str]) -> None:
| ^^^^^^^^^^^^^^ UP006
74 | ...
|
= help: Replace with `re.Pattern`
Safe fix
20 20 |
21 21 |
22 22 | from typing import List as IList
23 |+from re import Pattern
23 24 |
24 25 |
25 26 | def f(x: IList[str]) -> None:
--------------------------------------------------------------------------------
70 71 | ...
71 72 |
72 73 |
73 |-def f(x: typing.Pattern[str]) -> None:
74 |+def f(x: Pattern[str]) -> None:
74 75 | ...
75 76 |
76 77 |
UP006_0.py:77:10: UP006 [*] Use `collections.abc.Sequence` instead of `typing.Sequence` for type annotation
|
77 | def f(x: typing.Sequence[str]) -> None:
| ^^^^^^^^^^^^^^^ UP006
78 | ...
|
= help: Replace with `collections.abc.Sequence`
Safe fix
20 20 |
21 21 |
22 22 | from typing import List as IList
23 |+from collections.abc import Sequence
23 24 |
24 25 |
25 26 | def f(x: IList[str]) -> None:
--------------------------------------------------------------------------------
74 75 | ...
75 76 |
76 77 |
77 |-def f(x: typing.Sequence[str]) -> None:
78 |+def f(x: Sequence[str]) -> None:
78 79 | ...
79 80 |
80 81 |
UP006_0.py:84:10: UP006 Use `collections.abc.Collection` instead of `typing.Collection` for type annotation
|
84 | def f(x: typing.Collection[str]) -> None:
| ^^^^^^^^^^^^^^^^^ UP006
85 | ...
|
= help: Replace with `collections.abc.Collection`

View file

@ -1,46 +0,0 @@
---
source: crates/ruff_linter/src/rules/pyupgrade/mod.rs
---
UP006_1.py:9:10: UP006 [*] Use `collections.defaultdict` instead of `typing.DefaultDict` for type annotation
|
9 | def f(x: typing.DefaultDict[str, str]) -> None:
| ^^^^^^^^^^^^^^^^^^ UP006
10 | ...
|
= help: Replace with `collections.defaultdict`
Safe fix
6 6 | from collections import defaultdict
7 7 |
8 8 |
9 |-def f(x: typing.DefaultDict[str, str]) -> None:
9 |+def f(x: defaultdict[str, str]) -> None:
10 10 | ...
11 11 |
12 12 |
UP006_1.py:17:10: UP006 [*] Use `collections.abc.Set` instead of `typing.AbstractSet` for type annotation
|
17 | def f(x: typing.AbstractSet[str]) -> None:
| ^^^^^^^^^^^^^^^^^^ UP006
18 | ...
|
= help: Replace with `collections.abc.Set`
Safe fix
14 14 | from typing_extensions import Awaitable
15 15 |
16 16 |
17 |-def f(x: typing.AbstractSet[str]) -> None:
17 |+def f(x: Set[str]) -> None:
18 18 | ...
19 19 |
20 20 |
UP006_1.py:25:10: UP006 Use `collections.abc.Awaitable` instead of `Awaitable` for type annotation
|
25 | def f(x: Awaitable) -> None:
| ^^^^^^^^^ UP006
26 | ...
|
= help: Replace with `collections.abc.Awaitable`

View file

@ -121,7 +121,10 @@ pub fn to_pep585_generic(expr: &Expr, semantic: &SemanticModel) -> Option<Module
.then(|| semantic.resolve_qualified_name(expr))
.flatten()
.and_then(|qualified_name| {
as_pep_585_generic(qualified_name.segments()).map(|(module, member)| {
let [module, member] = qualified_name.segments() else {
return None;
};
as_pep_585_generic(module, member).map(|(module, member)| {
if module.is_empty() {
ModuleMember::BuiltIn(member)
} else {

View file

@ -337,61 +337,19 @@ type ModuleMember = (&'static str, &'static str);
/// library (e.g., `list` for `typing.List`), if such a generic was introduced by [PEP 585].
///
/// [PEP 585]: https://peps.python.org/pep-0585/
pub fn as_pep_585_generic(call_path: &[&str]) -> Option<ModuleMember> {
let ["typing" | "typing_extensions", member @ ..] = call_path else {
return None;
};
match member {
// Builtins
["Tuple"] => Some(("", "tuple")),
["List"] => Some(("", "list")),
["Dict"] => Some(("", "dict")),
["Set"] => Some(("", "set")),
["FrozenSet"] => Some(("", "frozenset")),
["Type"] => Some(("", "type")),
// collections
["Deque"] => Some(("collections", "deque")),
["DefaultDict"] => Some(("collections", "defaultdict")),
["OrderedDict"] => Some(("collections", "OrderedDict")),
["Counter"] => Some(("collections", "Counter")),
["ChainMap"] => Some(("collections", "ChainMap")),
// collections.abc
["Awaitable"] => Some(("collections.abc", "Awaitable")),
["Coroutine"] => Some(("collections.abc", "Coroutine")),
["AsyncIterable"] => Some(("collections.abc", "AsyncIterable")),
["AsyncGenerator"] => Some(("collections.abc", "AsyncGenerator")),
["Iterable"] => Some(("collections.abc", "Iterable")),
["Iterator"] => Some(("collections.abc", "Iterator")),
["Generator"] => Some(("collections.abc", "Generator")),
["Reversible"] => Some(("collections.abc", "Reversible")),
["Container"] => Some(("collections.abc", "Container")),
["Collection"] => Some(("collections.abc", "Collection")),
["Callable"] => Some(("collections.abc", "Callable")),
["AbstractSet"] => Some(("collections.abc", "Set")),
["MutableSet"] => Some(("collections.abc", "MutableSet")),
["Mapping"] => Some(("collections.abc", "Mapping")),
["MutableMapping"] => Some(("collections.abc", "MutableMapping")),
["Sequence"] => Some(("collections.abc", "Sequence")),
["MutableSequence"] => Some(("collections.abc", "MutableSequence")),
["ByteString"] => Some(("collections.abc", "ByteString")),
["MappingView"] => Some(("collections.abc", "MappingView")),
["KeysView"] => Some(("collections.abc", "KeysView")),
["ItemsView"] => Some(("collections.abc", "ItemsView")),
["ValuesView"] => Some(("collections.abc", "ValuesView")),
// contextlib
["ContextManager"] => Some(("contextlib", "AbstractContextManager")),
["AsyncContextManager"] => Some(("contextlib", "AbstractAsyncContextManager")),
// re
["Pattern"] => Some(("re", "Pattern")),
["Match"] => Some(("re", "Match")),
["re", "Pattern"] => Some(("re", "Pattern")),
["re", "Match"] => Some(("re", "Match")),
pub fn as_pep_585_generic(module: &str, member: &str) -> Option<ModuleMember> {
match (module, member) {
("typing", "Dict") => Some(("", "dict")),
("typing", "FrozenSet") => Some(("", "frozenset")),
("typing", "List") => Some(("", "list")),
("typing", "Set") => Some(("", "set")),
("typing", "Tuple") => Some(("", "tuple")),
("typing", "Type") => Some(("", "type")),
("typing_extensions", "Type") => Some(("", "type")),
("typing", "Deque") => Some(("collections", "deque")),
("typing_extensions", "Deque") => Some(("collections", "deque")),
("typing", "DefaultDict") => Some(("collections", "defaultdict")),
("typing_extensions", "DefaultDict") => Some(("collections", "defaultdict")),
_ => None,
}
}
@ -405,39 +363,8 @@ pub fn has_pep_585_generic(module: &str, member: &str) -> bool {
// the last element in each pattern, and de-duplicating the values.
matches!(
(module, member),
("", "tuple" | "list" | "dict" | "set" | "frozenset" | "type")
| (
"collections",
"deque" | "defaultdict" | "OrderedDict" | "Counter" | "ChainMap"
)
| (
"collections.abc",
"Awaitable"
| "Coroutine"
| "Iterable"
| "Iterator"
| "Generator"
| "Reversible"
| "Container"
| "Collection"
| "Callable"
| "Set"
| "MutableSet"
| "Mapping"
| "MutableMapping"
| "Sequence"
| "MutableSequence"
| "ByteString"
| "MappingView"
| "KeysView"
| "ItemsView"
| "ValuesView"
)
| (
"contextlib",
"AbstractContextManager" | "AbstractAsyncContextManager"
)
| ("re", "Pattern" | "Match")
("", "dict" | "frozenset" | "list" | "set" | "tuple" | "type")
| ("collections", "deque" | "defaultdict")
)
}