-
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.
Add flake-pyi PYI033 "Do not use type comments in stubs"
I think this is also a case where people like want this for .py files, too, at least on 3.7+ (https://docs.python.org/3/whatsnew/3.7.html#pep-563-postponed-evaluation-of-annotations) On that note, does none of the tools upgrade all type comments to type annotations automatically? I looked in pyupgrade and googled but couldn't find anything
- Loading branch information
Showing
13 changed files
with
134 additions
and
4 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,7 @@ | ||
# OK | ||
x = 1 # type: int | ||
|
||
|
||
# OK | ||
def reverse(x): # type:(str) -> str | ||
... |
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,7 @@ | ||
# ERROR: PYI033 Don't use type comments in stub file | ||
x = 1 # type: int | ||
|
||
|
||
# ERROR: PYI033 Don't use type comments in stub file | ||
def reverse(x): # type:(str) -> str | ||
... |
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
59 changes: 59 additions & 0 deletions
59
crates/ruff/src/rules/flake8_pyi/rules/type_comment_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,59 @@ | ||
use rustpython_parser::lexer::LexResult; | ||
use rustpython_parser::Tok; | ||
|
||
use ruff_macros::{define_violation, derive_message_formats}; | ||
|
||
use crate::registry::Diagnostic; | ||
use crate::violation::Violation; | ||
use crate::Range; | ||
|
||
define_violation!( | ||
/// ## What it does | ||
/// Do not use type comments (e.g. `x = 1 # type: int`) in stubs, even if | ||
/// the stub supports Python 2. Always use annotations instead (e.g. | ||
/// `x: int = 1`). | ||
/// | ||
/// ## Why is this bad? | ||
/// You should use type annotation directly in pyi files. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// x = 1 # type: int | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// x: int = 1 | ||
/// ``` | ||
pub struct TypeCommentInStub; | ||
); | ||
impl Violation for TypeCommentInStub { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Don't use type comments in stub file") | ||
} | ||
} | ||
|
||
/// PYI033 | ||
pub fn type_comment_in_stub(tokens: &[LexResult]) -> Vec<Diagnostic> { | ||
let mut diagnostics = vec![]; | ||
|
||
for token in tokens.iter().flatten() { | ||
if let (location, Tok::Comment(comment), end_location) = token { | ||
// I couldn't find any PEP on the exact syntax (the closest being | ||
// https://peps.python.org/pep-0484/#type-comments), but every case I saw used | ||
// `# type:` verbatim so this seems to be the right thing to pick | ||
if comment.starts_with("# type:") { | ||
diagnostics.push(Diagnostic::new( | ||
TypeCommentInStub, | ||
Range { | ||
location: *location, | ||
end_location: *end_location, | ||
}, | ||
)); | ||
} | ||
} | ||
} | ||
|
||
diagnostics | ||
} |
6 changes: 6 additions & 0 deletions
6
...ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI033_PYI033.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,6 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
expression: diagnostics | ||
--- | ||
[] | ||
|
25 changes: 25 additions & 0 deletions
25
...uff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI033_PYI033.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,25 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
TypeCommentInStub: ~ | ||
location: | ||
row: 2 | ||
column: 6 | ||
end_location: | ||
row: 2 | ||
column: 17 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
TypeCommentInStub: ~ | ||
location: | ||
row: 6 | ||
column: 17 | ||
end_location: | ||
row: 6 | ||
column: 36 | ||
fix: ~ | ||
parent: ~ | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.