-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
flake8-pyi
] Teach various rules that annotations might be stringized
- Loading branch information
1 parent
96802d6
commit 5bf9327
Showing
15 changed files
with
202 additions
and
52 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
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,31 @@ | ||
use ruff_python_ast as ast; | ||
use ruff_python_parser::typing::parse_type_annotation; | ||
use ruff_source_file::Locator; | ||
|
||
/// Apply a test to an annotation expression, | ||
/// abstracting over the fact that the annotation expression might be "stringized". | ||
/// | ||
/// A stringized annotation is one enclosed in string quotes: | ||
/// `foo: "typing.Any"` means the same thing to a type checker as `foo: typing.Any`. | ||
pub(super) fn match_maybe_stringized_annotation( | ||
expr: &ast::Expr, | ||
locator: &Locator, | ||
match_fn: impl FnOnce(&ast::Expr) -> bool, | ||
) -> bool { | ||
if let ast::Expr::StringLiteral(string_annotation) = expr { | ||
let Ok((parsed_annotation, _)) = | ||
parse_type_annotation(string_annotation, locator.contents()) | ||
else { | ||
return false; | ||
}; | ||
if !parsed_annotation.errors().is_empty() { | ||
return false; | ||
} | ||
let ast::ModExpression { | ||
body: annotation, .. | ||
} = parsed_annotation.into_syntax(); | ||
match_fn(&annotation) | ||
} else { | ||
match_fn(expr) | ||
} | ||
} |
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
Oops, something went wrong.