Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
flake8-pyi
] Implement PYI053 (#4770)
- Loading branch information
Showing
12 changed files
with
209 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
def f1(x: str = "50 character stringggggggggggggggggggggggggggggggg") -> None: | ||
... | ||
|
||
|
||
def f2(x: str = "51 character stringgggggggggggggggggggggggggggggggg") -> None: | ||
... | ||
|
||
|
||
def f3(x: str = "50 character stringggggggggggggggggggggggggggggg\U0001f600") -> None: | ||
... | ||
|
||
|
||
def f4(x: str = "51 character stringgggggggggggggggggggggggggggggg\U0001f600") -> None: | ||
... | ||
|
||
|
||
def f5(x: bytes = b"50 character byte stringgggggggggggggggggggggggggg") -> None: | ||
... | ||
|
||
|
||
def f6(x: bytes = b"51 character byte stringgggggggggggggggggggggggggg") -> None: | ||
... | ||
|
||
|
||
def f7(x: bytes = b"50 character byte stringggggggggggggggggggggggggg\xff") -> None: | ||
... | ||
|
||
|
||
def f8(x: bytes = b"50 character byte stringgggggggggggggggggggggggggg\xff") -> None: | ||
... | ||
|
||
|
||
foo: str = "50 character stringggggggggggggggggggggggggggggggg" | ||
bar: str = "51 character stringgggggggggggggggggggggggggggggggg" | ||
|
||
baz: bytes = b"50 character byte stringgggggggggggggggggggggggggg" | ||
|
||
qux: bytes = b"51 character byte stringggggggggggggggggggggggggggg\xff" |
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,30 @@ | ||
def f1(x: str = "50 character stringggggggggggggggggggggggggggggggg") -> None: ... # OK | ||
def f2( | ||
x: str = "51 character stringgggggggggggggggggggggggggggggggg", # Error: PYI053 | ||
) -> None: ... | ||
def f3( | ||
x: str = "50 character stringgggggggggggggggggggggggggggggg\U0001f600", # OK | ||
) -> None: ... | ||
def f4( | ||
x: str = "51 character stringggggggggggggggggggggggggggggggg\U0001f600", # Error: PYI053 | ||
) -> None: ... | ||
def f5( | ||
x: bytes = b"50 character byte stringgggggggggggggggggggggggggg", # OK | ||
) -> None: ... | ||
def f6( | ||
x: bytes = b"51 character byte stringgggggggggggggggggggggggggg", # Error: PYI053 | ||
) -> None: ... | ||
def f7( | ||
x: bytes = b"50 character byte stringggggggggggggggggggggggggg\xff", # OK | ||
) -> None: ... | ||
def f8( | ||
x: bytes = b"51 character byte stringgggggggggggggggggggggggggg\xff", # Error: PYI053 | ||
) -> None: ... | ||
|
||
foo: str = "50 character stringggggggggggggggggggggggggggggggg" # OK | ||
|
||
bar: str = "51 character stringgggggggggggggggggggggggggggggggg" # Error: PYI053 | ||
|
||
baz: bytes = b"50 character byte stringgggggggggggggggggggggggggg" # OK | ||
|
||
qux: bytes = b"51 character byte stringggggggggggggggggggggggggggg\xff" # Error: PYI053 |
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
66 changes: 66 additions & 0 deletions
66
crates/ruff/src/rules/flake8_pyi/rules/string_or_bytes_too_long.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,66 @@ | ||
use rustpython_parser::ast::{self, Constant, Expr, Ranged}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Edit, Fix, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
|
||
use crate::checkers::ast::Checker; | ||
use crate::registry::AsRule; | ||
|
||
#[violation] | ||
pub struct StringOrBytesTooLong; | ||
|
||
/// ## What it does | ||
/// Checks for the use of string and bytes literals longer than 50 characters. | ||
/// | ||
/// ## Why is this bad? | ||
/// If a function has a default value where the string or bytes representation | ||
/// is greater than 50 characters, it is likely to be an implementation detail | ||
/// or a constant that varies depending on the system you're running on. | ||
/// | ||
/// Consider replacing such constants with ellipses (`...`). | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// def foo(arg: str = "51 character stringgggggggggggggggggggggggggggggggg") -> None: ... | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// def foo(arg: str = ...) -> None: ... | ||
/// ``` | ||
impl Violation for StringOrBytesTooLong { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("String and bytes literals longer than 50 characters are not permitted") | ||
} | ||
} | ||
|
||
/// PYI053 | ||
pub(crate) fn string_or_bytes_too_long(checker: &mut Checker, expr: &Expr) { | ||
let length = match expr { | ||
Expr::Constant(ast::ExprConstant { | ||
value: Constant::Str(s), | ||
.. | ||
}) => s.chars().count(), | ||
Expr::Constant(ast::ExprConstant { | ||
value: Constant::Bytes(bytes), | ||
.. | ||
}) => bytes.len(), | ||
_ => return, | ||
}; | ||
|
||
if length <= 50 { | ||
return; | ||
} | ||
|
||
let mut diagnostic = Diagnostic::new(StringOrBytesTooLong, expr.range()); | ||
if checker.patch(diagnostic.kind.rule()) { | ||
diagnostic.set_fix(Fix::suggested(Edit::range_replacement( | ||
"...".to_string(), | ||
expr.range(), | ||
))); | ||
} | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(StringOrBytesTooLong, expr.range())); | ||
} |
4 changes: 4 additions & 0 deletions
4
...ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI053_PYI053.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,4 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
--- | ||
|
51 changes: 51 additions & 0 deletions
51
...uff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI053_PYI053.pyi.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,51 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
--- | ||
PYI053.pyi:3:14: PYI053 String and bytes literals longer than 50 characters are not permitted | ||
| | ||
3 | def f1(x: str = "50 character stringggggggggggggggggggggggggggggggg") -> None: ... # OK | ||
4 | def f2( | ||
5 | x: str = "51 character stringgggggggggggggggggggggggggggggggg", # Error: PYI053 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI053 | ||
6 | ) -> None: ... | ||
7 | def f3( | ||
| | ||
|
||
PYI053.pyi:9:14: PYI053 String and bytes literals longer than 50 characters are not permitted | ||
| | ||
9 | ) -> None: ... | ||
10 | def f4( | ||
11 | x: str = "51 character stringggggggggggggggggggggggggggggggg\U0001f600", # Error: PYI053 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI053 | ||
12 | ) -> None: ... | ||
13 | def f5( | ||
| | ||
|
||
PYI053.pyi:21:16: PYI053 String and bytes literals longer than 50 characters are not permitted | ||
| | ||
21 | ) -> None: ... | ||
22 | def f8( | ||
23 | x: bytes = b"51 character byte stringgggggggggggggggggggggggggg\xff", # Error: PYI053 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI053 | ||
24 | ) -> None: ... | ||
| | ||
|
||
PYI053.pyi:26:12: PYI053 String and bytes literals longer than 50 characters are not permitted | ||
| | ||
26 | foo: str = "50 character stringggggggggggggggggggggggggggggggg" # OK | ||
27 | | ||
28 | bar: str = "51 character stringgggggggggggggggggggggggggggggggg" # Error: PYI053 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI053 | ||
29 | | ||
30 | baz: bytes = b"50 character byte stringgggggggggggggggggggggggggg" # OK | ||
| | ||
|
||
PYI053.pyi:30:14: PYI053 String and bytes literals longer than 50 characters are not permitted | ||
| | ||
30 | baz: bytes = b"50 character byte stringgggggggggggggggggggggggggg" # OK | ||
31 | | ||
32 | qux: bytes = b"51 character byte stringggggggggggggggggggggggggggg\xff" # Error: PYI053 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI053 | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.