-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
pyupgrade
] - add PEP646 Unpack conversion to *
(UP044
)
- Loading branch information
1 parent
1607d88
commit ee80d22
Showing
8 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
crates/ruff_linter/resources/test/fixtures/pyupgrade/UP044.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from typing import Generic, TypeVarTuple, Unpack | ||
|
||
Shape = TypeVarTuple('Shape') | ||
|
||
class C(Generic[Unpack[Shape]]): | ||
pass | ||
|
||
class D(Generic[Unpack [Shape]]): | ||
pass | ||
|
||
def f(*args: Unpack[tuple[int, ...]]): pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
crates/ruff_linter/src/rules/pyupgrade/rules/use_pep646_unpack.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::ExprSubscript; | ||
|
||
use crate::{checkers::ast::Checker, settings::types::PythonVersion}; | ||
|
||
/// ## What it does | ||
/// Checks for uses of `Unpack[]` on Python 3.11 and above, and suggests | ||
/// using `*[]` instead. | ||
/// | ||
/// ## Why is this bad? | ||
/// [PEP 646] introduced a new syntax for unpacking sequences based on the `*` | ||
/// operator. This syntax is more concise and readable than the previous | ||
/// `typing.Unpack` syntax. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// from typing import Unpack | ||
/// | ||
/// def foo(*args: Unpack[tuple[int, ...]]) -> None: pass | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// def foo(*args: *[tuple[int, ...]]) -> None: pass | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [PEP 646](https://peps.python.org/pep-0646/#unpack-for-backwards-compatibility) | ||
#[violation] | ||
pub struct NonPEP646Unpack; | ||
|
||
impl Violation for NonPEP646Unpack { | ||
const FIX_AVAILABILITY: FixAvailability = FixAvailability::Always; | ||
|
||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Use `*[]` for unpacking") | ||
} | ||
|
||
fn fix_title(&self) -> Option<String> { | ||
Some("Convert to `*[]`".to_string()) | ||
} | ||
} | ||
|
||
/// UP044 | ||
pub(crate) fn use_pep646_unpack(checker: &mut Checker, expr: &ExprSubscript) { | ||
if checker.settings.target_version < PythonVersion::Py311 { | ||
return; | ||
} | ||
|
||
if !checker.semantic().seen_typing() { | ||
return; | ||
} | ||
|
||
let ExprSubscript { | ||
range, | ||
value, | ||
slice, | ||
.. | ||
} = expr; | ||
|
||
if !checker.semantic().match_typing_expr(value, "Unpack") { | ||
return; | ||
} | ||
|
||
let mut diagnostic = Diagnostic::new(NonPEP646Unpack, *range); | ||
|
||
let inner = checker.locator().slice(slice.as_ref()); | ||
|
||
if checker.settings.preview.is_enabled() { | ||
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement( | ||
format!("*[{inner}]"), | ||
*range, | ||
))); | ||
} | ||
|
||
checker.diagnostics.push(diagnostic); | ||
} |
58 changes: 58 additions & 0 deletions
58
...rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__unpack_pep_646_py311.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pyupgrade/mod.rs | ||
--- | ||
UP044.py:5:17: UP044 [*] Use `*[]` for unpacking | ||
| | ||
3 | Shape = TypeVarTuple('Shape') | ||
4 | | ||
5 | class C(Generic[Unpack[Shape]]): | ||
| ^^^^^^^^^^^^^ UP044 | ||
6 | pass | ||
| | ||
= help: Convert to `*[]` | ||
|
||
ℹ Safe fix | ||
2 2 | | ||
3 3 | Shape = TypeVarTuple('Shape') | ||
4 4 | | ||
5 |-class C(Generic[Unpack[Shape]]): | ||
5 |+class C(Generic[*[Shape]]): | ||
6 6 | pass | ||
7 7 | | ||
8 8 | class D(Generic[Unpack [Shape]]): | ||
|
||
UP044.py:8:17: UP044 [*] Use `*[]` for unpacking | ||
| | ||
6 | pass | ||
7 | | ||
8 | class D(Generic[Unpack [Shape]]): | ||
| ^^^^^^^^^^^^^^^ UP044 | ||
9 | pass | ||
| | ||
= help: Convert to `*[]` | ||
|
||
ℹ Safe fix | ||
5 5 | class C(Generic[Unpack[Shape]]): | ||
6 6 | pass | ||
7 7 | | ||
8 |-class D(Generic[Unpack [Shape]]): | ||
8 |+class D(Generic[*[Shape]]): | ||
9 9 | pass | ||
10 10 | | ||
11 11 | def f(*args: Unpack[tuple[int, ...]]): pass | ||
|
||
UP044.py:11:14: UP044 [*] Use `*[]` for unpacking | ||
| | ||
9 | pass | ||
10 | | ||
11 | def f(*args: Unpack[tuple[int, ...]]): pass | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ UP044 | ||
| | ||
= help: Convert to `*[]` | ||
|
||
ℹ Safe fix | ||
8 8 | class D(Generic[Unpack [Shape]]): | ||
9 9 | pass | ||
10 10 | | ||
11 |-def f(*args: Unpack[tuple[int, ...]]): pass | ||
11 |+def f(*args: *[tuple[int, ...]]): pass |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.