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

Add all PEP-585 names to UP006 rule #5454

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pyupgrade/UP006_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,22 @@ 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:
...
16 changes: 16 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pyupgrade/UP006_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,19 @@

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:
...
15 changes: 15 additions & 0 deletions crates/ruff_linter/src/rules/pyupgrade/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,21 @@ 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ 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
Expand Down Expand Up @@ -81,6 +96,9 @@ 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;
};
Expand Down Expand Up @@ -138,3 +156,11 @@ 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")
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: crates/ruff_linter/src/rules/pyupgrade/mod.rs
snapshot_kind: text
---
UP006_0.py:4:10: UP006 [*] Use `list` instead of `typing.List` for type annotation
|
Expand Down Expand Up @@ -281,3 +280,5 @@ 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 |
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: crates/ruff_linter/src/rules/pyupgrade/mod.rs
snapshot_kind: text
---
UP006_1.py:9:10: UP006 [*] Use `collections.defaultdict` instead of `typing.DefaultDict` for type annotation
|
Expand All @@ -17,3 +16,5 @@ 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 |
Loading
Loading