Skip to content

Commit

Permalink
Add Applicability to pyupgrade (#5162)
Browse files Browse the repository at this point in the history
## Summary

Fixes some of #4184.
  • Loading branch information
evanrittenhouse authored Jun 17, 2023
1 parent 95448ba commit 653a0eb
Show file tree
Hide file tree
Showing 18 changed files with 56 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ where
let mut diagnostic = Diagnostic::new(DeprecatedCElementTree, node.range());
if checker.patch(diagnostic.kind.rule()) {
let contents = checker.locator.slice(node.range());
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
contents.replacen("cElementTree", "ElementTree", 1),
node.range(),
)));
Expand Down
3 changes: 1 addition & 2 deletions crates/ruff/src/rules/pyupgrade/rules/deprecated_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,7 @@ pub(crate) fn deprecated_import(
);
if checker.patch(Rule::DeprecatedImport) {
if let Some(content) = fix {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
content,
stmt.range(),
)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,7 @@ pub(crate) fn deprecated_mock_attribute(checker: &mut Checker, expr: &Expr) {
value.range(),
);
if checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
"mock".to_string(),
value.range(),
)));
Expand Down Expand Up @@ -307,8 +306,7 @@ pub(crate) fn deprecated_mock_import(checker: &mut Checker, stmt: &Stmt) {
name.range(),
);
if let Some(content) = content.as_ref() {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
content.clone(),
stmt.range(),
)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ pub(crate) fn extraneous_parentheses(
if settings.rules.should_fix(Rule::ExtraneousParentheses) {
let contents =
locator.slice(TextRange::new(start_range.start(), end_range.end()));
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::replacement(
diagnostic.set_fix(Fix::automatic(Edit::replacement(
contents[1..contents.len() - 1].to_string(),
start_range.start(),
end_range.end(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ pub(crate) fn lru_cache_with_maxsize_none(checker: &mut Checker, decorator_list:
)?;
let reference_edit =
Edit::range_replacement(binding, decorator.expression.range());
#[allow(deprecated)]
Ok(Fix::unspecified_edits(import_edit, [reference_edit]))
Ok(Fix::automatic_edits(import_edit, [reference_edit]))
});
}
checker.diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ pub(crate) fn lru_cache_without_parameters(checker: &mut Checker, decorator_list
TextRange::new(func.end(), decorator.end()),
);
if checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
diagnostic.set_fix(Fix::automatic(Edit::range_replacement(
checker.generator().expr(func),
decorator.expression.range(),
)));
Expand Down
6 changes: 2 additions & 4 deletions crates/ruff/src/rules/pyupgrade/rules/native_literals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ pub(crate) fn native_literals(
Constant::Str(String::new())
};
let content = checker.generator().constant(&constant);
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
diagnostic.set_fix(Fix::automatic(Edit::range_replacement(
content,
expr.range(),
)));
Expand Down Expand Up @@ -153,8 +152,7 @@ pub(crate) fn native_literals(
expr.range(),
);
if checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
diagnostic.set_fix(Fix::automatic(Edit::range_replacement(
arg_code.to_string(),
expr.range(),
)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,7 @@ pub(crate) fn printf_string_formatting(

let mut diagnostic = Diagnostic::new(PrintfStringFormatting, expr.range());
if checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
contents,
expr.range(),
)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ fn generate_fix(
} else {
(stderr, stdout)
};
#[allow(deprecated)]
Ok(Fix::unspecified_edits(
Ok(Fix::suggested_edits(
Edit::range_replacement("capture_output=True".to_string(), first.range()),
[remove_argument(
locator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ pub(crate) fn use_pep604_annotation(
Pep604Operator::Optional => {
let mut diagnostic = Diagnostic::new(NonPEP604Annotation, expr.range());
if fixable && checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
diagnostic.set_fix(Fix::manual(Edit::range_replacement(
checker.generator().expr(&optional(slice)),
expr.range(),
)));
Expand All @@ -104,16 +103,14 @@ pub(crate) fn use_pep604_annotation(
// Invalid type annotation.
}
Expr::Tuple(ast::ExprTuple { elts, .. }) => {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
diagnostic.set_fix(Fix::manual(Edit::range_replacement(
checker.generator().expr(&union(elts)),
expr.range(),
)));
}
_ => {
// Single argument.
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
diagnostic.set_fix(Fix::manual(Edit::range_replacement(
checker.generator().expr(slice),
expr.range(),
)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ UP007.py:6:10: UP007 [*] Use `X | Y` for type annotations
|
= help: Convert to `X | Y`

Suggested fix
Possible fix
3 3 | from typing import Union
4 4 |
5 5 |
Expand All @@ -27,7 +27,7 @@ UP007.py:10:10: UP007 [*] Use `X | Y` for type annotations
|
= help: Convert to `X | Y`

Suggested fix
Possible fix
7 7 | ...
8 8 |
9 9 |
Expand All @@ -45,7 +45,7 @@ UP007.py:14:10: UP007 [*] Use `X | Y` for type annotations
|
= help: Convert to `X | Y`

Suggested fix
Possible fix
11 11 | ...
12 12 |
13 13 |
Expand All @@ -63,7 +63,7 @@ UP007.py:14:26: UP007 [*] Use `X | Y` for type annotations
|
= help: Convert to `X | Y`

Suggested fix
Possible fix
11 11 | ...
12 12 |
13 13 |
Expand All @@ -81,7 +81,7 @@ UP007.py:18:10: UP007 [*] Use `X | Y` for type annotations
|
= help: Convert to `X | Y`

Suggested fix
Possible fix
15 15 | ...
16 16 |
17 17 |
Expand All @@ -99,7 +99,7 @@ UP007.py:22:10: UP007 [*] Use `X | Y` for type annotations
|
= help: Convert to `X | Y`

Suggested fix
Possible fix
19 19 | ...
20 20 |
21 21 |
Expand All @@ -117,7 +117,7 @@ UP007.py:26:10: UP007 [*] Use `X | Y` for type annotations
|
= help: Convert to `X | Y`

Suggested fix
Possible fix
23 23 | ...
24 24 |
25 25 |
Expand All @@ -135,7 +135,7 @@ UP007.py:30:11: UP007 [*] Use `X | Y` for type annotations
|
= help: Convert to `X | Y`

Suggested fix
Possible fix
27 27 | ...
28 28 |
29 29 |
Expand All @@ -153,7 +153,7 @@ UP007.py:30:27: UP007 [*] Use `X | Y` for type annotations
|
= help: Convert to `X | Y`

Suggested fix
Possible fix
27 27 | ...
28 28 |
29 29 |
Expand All @@ -171,7 +171,7 @@ UP007.py:34:11: UP007 [*] Use `X | Y` for type annotations
|
= help: Convert to `X | Y`

Suggested fix
Possible fix
31 31 | ...
32 32 |
33 33 |
Expand All @@ -190,7 +190,7 @@ UP007.py:47:8: UP007 [*] Use `X | Y` for type annotations
|
= help: Convert to `X | Y`

Suggested fix
Possible fix
44 44 |
45 45 |
46 46 | def f() -> None:
Expand Down Expand Up @@ -232,7 +232,7 @@ UP007.py:52:8: UP007 [*] Use `X | Y` for type annotations
|
= help: Convert to `X | Y`

Suggested fix
Possible fix
49 49 |
50 50 | x = Union[str, int]
51 51 | x = Union["str", "int"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ UP011.py:5:21: UP011 [*] Unnecessary parentheses to `functools.lru_cache`
|
= help: Remove unnecessary parentheses

Suggested fix
Fix
2 2 | from functools import lru_cache
3 3 |
4 4 |
Expand All @@ -29,7 +29,7 @@ UP011.py:10:11: UP011 [*] Unnecessary parentheses to `functools.lru_cache`
|
= help: Remove unnecessary parentheses

Suggested fix
Fix
7 7 | pass
8 8 |
9 9 |
Expand All @@ -49,7 +49,7 @@ UP011.py:16:21: UP011 [*] Unnecessary parentheses to `functools.lru_cache`
|
= help: Remove unnecessary parentheses

Suggested fix
Fix
13 13 |
14 14 |
15 15 | @other_decorator
Expand All @@ -68,7 +68,7 @@ UP011.py:21:21: UP011 [*] Unnecessary parentheses to `functools.lru_cache`
|
= help: Remove unnecessary parentheses

Suggested fix
Fix
18 18 | pass
19 19 |
20 20 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ UP018.py:21:1: UP018 [*] Unnecessary call to `str`
|
= help: Replace with empty string

Suggested fix
Fix
18 18 | f"{f'{str()}'}"
19 19 |
20 20 | # These become string or byte literals
Expand All @@ -32,7 +32,7 @@ UP018.py:22:1: UP018 [*] Unnecessary call to `str`
|
= help: Replace with empty string

Suggested fix
Fix
19 19 |
20 20 | # These become string or byte literals
21 21 | str()
Expand All @@ -54,7 +54,7 @@ UP018.py:23:1: UP018 [*] Unnecessary call to `str`
|
= help: Replace with empty string

Suggested fix
Fix
20 20 | # These become string or byte literals
21 21 | str()
22 22 | str("foo")
Expand All @@ -77,7 +77,7 @@ UP018.py:25:1: UP018 [*] Unnecessary call to `bytes`
|
= help: Replace with empty bytes

Suggested fix
Fix
22 22 | str("foo")
23 23 | str("""
24 24 | foo""")
Expand All @@ -98,7 +98,7 @@ UP018.py:26:1: UP018 [*] Unnecessary call to `bytes`
|
= help: Replace with empty bytes

Suggested fix
Fix
23 23 | str("""
24 24 | foo""")
25 25 | bytes()
Expand All @@ -119,7 +119,7 @@ UP018.py:27:1: UP018 [*] Unnecessary call to `bytes`
|
= help: Replace with empty bytes

Suggested fix
Fix
24 24 | foo""")
25 25 | bytes()
26 26 | bytes(b"foo")
Expand All @@ -138,7 +138,7 @@ UP018.py:29:4: UP018 [*] Unnecessary call to `str`
|
= help: Replace with empty string

Suggested fix
Fix
26 26 | bytes(b"foo")
27 27 | bytes(b"""
28 28 | foo""")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ UP033_0.py:4:21: UP033 [*] Use `@functools.cache` instead of `@functools.lru_cac
|
= help: Rewrite with `@functools.cache
Suggested fix
Fix
1 1 | import functools
2 2 |
3 3 |
Expand All @@ -30,7 +30,7 @@ UP033_0.py:10:21: UP033 [*] Use `@functools.cache` instead of `@functools.lru_ca
|
= help: Rewrite with `@functools.cache

Suggested fix
Fix
7 7 |
8 8 |
9 9 | @other_decorator
Expand All @@ -49,7 +49,7 @@ UP033_0.py:15:21: UP033 [*] Use `@functools.cache` instead of `@functools.lru_ca
|
= help: Rewrite with `@functools.cache
Suggested fix
Fix
12 12 | pass
13 13 |
14 14 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ UP033_1.py:4:11: UP033 [*] Use `@functools.cache` instead of `@functools.lru_cac
|
= help: Rewrite with `@functools.cache
Suggested fix
Fix
1 |-from functools import lru_cache
1 |+from functools import lru_cache, cache
2 2 |
Expand All @@ -31,7 +31,7 @@ UP033_1.py:10:11: UP033 [*] Use `@functools.cache` instead of `@functools.lru_ca
|
= help: Rewrite with `@functools.cache

Suggested fix
Fix
1 |-from functools import lru_cache
1 |+from functools import lru_cache, cache
2 2 |
Expand All @@ -56,7 +56,7 @@ UP033_1.py:15:11: UP033 [*] Use `@functools.cache` instead of `@functools.lru_ca
|
= help: Rewrite with `@functools.cache
Suggested fix
Fix
1 |-from functools import lru_cache
1 |+from functools import lru_cache, cache
2 2 |
Expand Down
Loading

0 comments on commit 653a0eb

Please sign in to comment.