-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add flake-pyi PYI033 "Do not use type comments in stubs" #3302
Merged
charliermarsh
merged 3 commits into
astral-sh:main
from
konstin:flake8-pyi-Y033-type-comments-in-stub-files
Mar 3, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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,33 @@ | ||
# From https://github.com/PyCQA/flake8-pyi/blob/4212bec43dbc4020a59b90e2957c9488575e57ba/tests/type_comments.pyi | ||
|
||
from collections.abc import Sequence | ||
from typing import TypeAlias | ||
|
||
A: TypeAlias = None # type: int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int") | ||
B: TypeAlias = None # type: str # And here's an extra comment about why it's that type # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int") | ||
C: TypeAlias = None #type: int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int") | ||
D: TypeAlias = None # type: int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int") | ||
E: TypeAlias = None# type: int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int") | ||
F: TypeAlias = None#type:int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int") | ||
|
||
def func( | ||
arg1, # type: dict[str, int] # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int") | ||
arg2 # type: Sequence[bytes] # And here's some more info about this arg # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int") | ||
): ... | ||
|
||
class Foo: | ||
Attr: TypeAlias = None # type: set[str] # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int") | ||
|
||
G: TypeAlias = None # type: ignore | ||
H: TypeAlias = None # type: ignore[attr-defined] | ||
I: TypeAlias = None #type: ignore | ||
J: TypeAlias = None # type: ignore | ||
K: TypeAlias = None# type: ignore | ||
L: TypeAlias = None#type:ignore | ||
|
||
# Whole line commented out # type: int | ||
M: TypeAlias = None # type: can't parse me! | ||
|
||
class Bar: | ||
N: TypeAlias = None # type: can't parse me either! | ||
# This whole line is commented out and indented # type: 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,33 @@ | ||
# From https://github.com/PyCQA/flake8-pyi/blob/4212bec43dbc4020a59b90e2957c9488575e57ba/tests/type_comments.pyi | ||
|
||
from collections.abc import Sequence | ||
from typing import TypeAlias | ||
|
||
A: TypeAlias = None # type: int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int") | ||
B: TypeAlias = None # type: str # And here's an extra comment about why it's that type # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int") | ||
C: TypeAlias = None #type: int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int") | ||
D: TypeAlias = None # type: int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int") | ||
E: TypeAlias = None# type: int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int") | ||
F: TypeAlias = None#type:int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int") | ||
|
||
def func( | ||
arg1, # type: dict[str, int] # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int") | ||
arg2 # type: Sequence[bytes] # And here's some more info about this arg # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int") | ||
): ... | ||
|
||
class Foo: | ||
Attr: TypeAlias = None # type: set[str] # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int") | ||
|
||
G: TypeAlias = None # type: ignore | ||
H: TypeAlias = None # type: ignore[attr-defined] | ||
I: TypeAlias = None #type: ignore | ||
J: TypeAlias = None # type: ignore | ||
K: TypeAlias = None# type: ignore | ||
L: TypeAlias = None#type:ignore | ||
|
||
# Whole line commented out # type: int | ||
M: TypeAlias = None # type: can't parse me! | ||
|
||
class Bar: | ||
N: TypeAlias = None # type: can't parse me either! | ||
# This whole line is commented out and indented # type: 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
64 changes: 64 additions & 0 deletions
64
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,64 @@ | ||
use once_cell::sync::Lazy; | ||
use regex::Regex; | ||
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 | ||
/// Checks for the use of type comments (e.g., `x = 1 # type: int`) in stub | ||
/// files. | ||
/// | ||
/// ## Why is this bad? | ||
/// Stub (`.pyi`) files should use type annotations directly, rather | ||
/// than type comments, even if they're intended to support Python 2, since | ||
/// stub files are not executed at runtime. The one exception is `# type: ignore`. | ||
/// | ||
/// ## 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") | ||
} | ||
} | ||
|
||
static TYPE_COMMENT_REGEX: Lazy<Regex> = | ||
Lazy::new(|| Regex::new(r"^#\s*type:\s*([^#]+)(\s*#.*?)?$").unwrap()); | ||
static TYPE_IGNORE_REGEX: Lazy<Regex> = | ||
Lazy::new(|| Regex::new(r"^#\s*type:\s*ignore([^#]+)?(\s*#.*?)?$").unwrap()); | ||
|
||
/// 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 { | ||
if TYPE_COMMENT_REGEX.is_match(comment) && !TYPE_IGNORE_REGEX.is_match(comment) { | ||
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 | ||
--- | ||
[] | ||
|
115 changes: 115 additions & 0 deletions
115
...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,115 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
TypeCommentInStub: ~ | ||
location: | ||
row: 6 | ||
column: 21 | ||
end_location: | ||
row: 6 | ||
column: 127 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
TypeCommentInStub: ~ | ||
location: | ||
row: 7 | ||
column: 21 | ||
end_location: | ||
row: 7 | ||
column: 183 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
TypeCommentInStub: ~ | ||
location: | ||
row: 8 | ||
column: 21 | ||
end_location: | ||
row: 8 | ||
column: 126 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
TypeCommentInStub: ~ | ||
location: | ||
row: 9 | ||
column: 21 | ||
end_location: | ||
row: 9 | ||
column: 132 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
TypeCommentInStub: ~ | ||
location: | ||
row: 10 | ||
column: 19 | ||
end_location: | ||
row: 10 | ||
column: 128 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
TypeCommentInStub: ~ | ||
location: | ||
row: 11 | ||
column: 19 | ||
end_location: | ||
row: 11 | ||
column: 123 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
TypeCommentInStub: ~ | ||
location: | ||
row: 14 | ||
column: 11 | ||
end_location: | ||
row: 14 | ||
column: 128 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
TypeCommentInStub: ~ | ||
location: | ||
row: 15 | ||
column: 10 | ||
end_location: | ||
row: 15 | ||
column: 172 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
TypeCommentInStub: ~ | ||
location: | ||
row: 19 | ||
column: 28 | ||
end_location: | ||
row: 19 | ||
column: 139 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
TypeCommentInStub: ~ | ||
location: | ||
row: 29 | ||
column: 21 | ||
end_location: | ||
row: 29 | ||
column: 44 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
TypeCommentInStub: ~ | ||
location: | ||
row: 32 | ||
column: 25 | ||
end_location: | ||
row: 32 | ||
column: 55 | ||
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.
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.
not sure how this got in here, should this be here?
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.
Oh strange. Builds should fail if
cargo dev generate-all
produces any changed files. Lemme look into it.