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
[flake8-pyi
] Implement PYI053
#4770
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1ba6c14
Implement PYI053
density 1b9f4e6
remove period
density d84be3c
Remove check for long literals from PYI015 since those are now covere…
density 1217413
update reasoning
density dee1561
improved comment
density e590b59
Merge branch 'main' into PYI053
charliermarsh 08369f3
Rename rule
charliermarsh ee47f3e
Rename file
charliermarsh 1296c02
Merge branch 'main' into PYI053
charliermarsh File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 stringggggggggggggggggggggggggggggg\U0001f600", # OK | ||
) -> None: ... | ||
def f4( | ||
x: str = "51 character stringgggggggggggggggggggggggggggggg\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"50 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
43 changes: 43 additions & 0 deletions
43
crates/ruff/src/rules/flake8_pyi/rules/long_string_or_bytes_in_stub.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,43 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use rustpython_parser::ast::{self, Constant, Expr, Ranged}; | ||
|
||
use ruff_macros::{derive_message_formats, violation}; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
#[violation] | ||
pub struct LongStringOrBytesInStub; | ||
|
||
/// ## What it does | ||
/// Checks for `str` or `bytes` literals longer than 50 characters in stubs. | ||
/// | ||
/// ## Why is this bad? | ||
/// Extremely long strings used as argument defaults or otherwise are unlikely to be useful for | ||
/// users. Consider replacing them with ellipses. | ||
charliermarsh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
impl Violation for LongStringOrBytesInStub { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("`str` and `bytes` literals longer than 50 characters should not be used in stubs.") | ||
} | ||
} | ||
|
||
/// PYI053 | ||
pub(crate) fn long_string_or_bytes_in_stub(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 { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(LongStringOrBytesInStub, expr.range())); | ||
} | ||
} |
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
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 | ||
--- | ||
|
41 changes: 41 additions & 0 deletions
41
...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,41 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
--- | ||
PYI053.pyi:3:14: PYI053 `str` and `bytes` literals longer than 50 characters should not be used in stubs. | ||
| | ||
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:21:16: PYI053 `str` and `bytes` literals longer than 50 characters should not be used in stubs. | ||
| | ||
21 | ) -> None: ... | ||
22 | def f8( | ||
23 | x: bytes = b"50 character byte stringgggggggggggggggggggggggggg\xff", # Error: PYI053 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI053 | ||
24 | ) -> None: ... | ||
| | ||
|
||
PYI053.pyi:26:12: PYI053 `str` and `bytes` literals longer than 50 characters should not be used in stubs. | ||
| | ||
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 `str` and `bytes` literals longer than 50 characters should not be used in stubs. | ||
| | ||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_in_stub
feels redundant in the name given that it's inflake8_pyi
. lmk if you prefer a different name.