-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement "native literals" check from pyupgrade
- Loading branch information
Showing
9 changed files
with
218 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# These remain unchanged | ||
str(1) | ||
str(*a) | ||
str("foo", *a) | ||
str(**k) | ||
str("foo", **k) | ||
str("foo", encoding="UTF-8") | ||
str("foo" | ||
"bar") | ||
bytes("foo", encoding="UTF-8") | ||
bytes(*a) | ||
bytes("foo", *a) | ||
bytes("foo", **a) | ||
bytes(b"foo" | ||
b"bar") | ||
|
||
# These become string or byte literals | ||
str() | ||
str("foo") | ||
str(""" | ||
foo""") | ||
bytes() | ||
bytes(b"foo") | ||
bytes(b""" | ||
foo""") |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use rustpython_ast::{Constant, Expr, ExprKind, Keyword}; | ||
use rustpython_parser::lexer; | ||
use rustpython_parser::lexer::Tok; | ||
|
||
use crate::ast::types::Range; | ||
use crate::autofix::Fix; | ||
use crate::checkers::ast::Checker; | ||
use crate::checks::{Check, CheckCode, CheckKind}; | ||
|
||
/// UP018 | ||
pub fn native_literals( | ||
checker: &mut Checker, | ||
expr: &Expr, | ||
func: &Expr, | ||
args: &[Expr], | ||
keywords: &[Keyword], | ||
) { | ||
let ExprKind::Name { id, .. } = &func.node else { return; }; | ||
|
||
if (id == "str" || id == "bytes") | ||
&& checker.is_builtin(id) | ||
&& keywords.is_empty() | ||
&& args.len() <= 1 | ||
{ | ||
let Some(arg) = args.get(0) else { | ||
let mut check = Check::new(CheckKind::NativeLiterals, Range::from_located(expr)); | ||
if checker.patch(&CheckCode::UP018) { | ||
check.amend(Fix::replacement( | ||
format!("{}\"\"", if id == "bytes" { "b" } else { "" }), | ||
expr.location, | ||
expr.end_location.unwrap(), | ||
)); | ||
} | ||
checker.add_check(check); | ||
return; | ||
}; | ||
|
||
if !matches!( | ||
&arg.node, | ||
ExprKind::Constant { | ||
value: Constant::Str(_) | Constant::Bytes(_), | ||
.. | ||
} | ||
) { | ||
return; | ||
} | ||
|
||
// rust-python merges adjacent string/bytes literals into one node, but we can't | ||
// safely remove the outer call in this situation. We're following pyupgrade | ||
// here and skip. | ||
let arg_code = checker | ||
.locator | ||
.slice_source_code_range(&Range::from_located(arg)); | ||
if lexer::make_tokenizer(&arg_code) | ||
.flatten() | ||
.filter(|(_, tok, _)| matches!(tok, Tok::String { .. } | Tok::Bytes { .. })) | ||
.count() | ||
> 1 | ||
{ | ||
return; | ||
} | ||
|
||
let mut check = Check::new(CheckKind::NativeLiterals, Range::from_located(expr)); | ||
if checker.patch(&CheckCode::UP018) { | ||
check.amend(Fix::replacement( | ||
arg_code.to_string(), | ||
expr.location, | ||
expr.end_location.unwrap(), | ||
)); | ||
} | ||
checker.add_check(check); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
src/pyupgrade/snapshots/ruff__pyupgrade__tests__UP018_UP018.py.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,95 @@ | ||
--- | ||
source: src/pyupgrade/mod.rs | ||
expression: checks | ||
--- | ||
- kind: NativeLiterals | ||
location: | ||
row: 18 | ||
column: 0 | ||
end_location: | ||
row: 18 | ||
column: 5 | ||
fix: | ||
content: "\"\"" | ||
location: | ||
row: 18 | ||
column: 0 | ||
end_location: | ||
row: 18 | ||
column: 5 | ||
- kind: NativeLiterals | ||
location: | ||
row: 19 | ||
column: 0 | ||
end_location: | ||
row: 19 | ||
column: 10 | ||
fix: | ||
content: "\"foo\"" | ||
location: | ||
row: 19 | ||
column: 0 | ||
end_location: | ||
row: 19 | ||
column: 10 | ||
- kind: NativeLiterals | ||
location: | ||
row: 20 | ||
column: 0 | ||
end_location: | ||
row: 21 | ||
column: 7 | ||
fix: | ||
content: "\"\"\"\nfoo\"\"\"" | ||
location: | ||
row: 20 | ||
column: 0 | ||
end_location: | ||
row: 21 | ||
column: 7 | ||
- kind: NativeLiterals | ||
location: | ||
row: 22 | ||
column: 0 | ||
end_location: | ||
row: 22 | ||
column: 7 | ||
fix: | ||
content: "b\"\"" | ||
location: | ||
row: 22 | ||
column: 0 | ||
end_location: | ||
row: 22 | ||
column: 7 | ||
- kind: NativeLiterals | ||
location: | ||
row: 23 | ||
column: 0 | ||
end_location: | ||
row: 23 | ||
column: 13 | ||
fix: | ||
content: "b\"foo\"" | ||
location: | ||
row: 23 | ||
column: 0 | ||
end_location: | ||
row: 23 | ||
column: 13 | ||
- kind: NativeLiterals | ||
location: | ||
row: 24 | ||
column: 0 | ||
end_location: | ||
row: 25 | ||
column: 7 | ||
fix: | ||
content: "b\"\"\"\nfoo\"\"\"" | ||
location: | ||
row: 24 | ||
column: 0 | ||
end_location: | ||
row: 25 | ||
column: 7 | ||
|