Use string expression for parsing type annotation (#11717)

## Summary

This PR updates the logic for parsing type annotation to accept a
`ExprStringLiteral` node instead of the string value and the range.

The main motivation of this change is to simplify the implementation of
`parse_type_annotation` function with:
* Use the `opener_len` and `closer_len` from the string flags to get the
raw contents range instead of extracting it via
	* `str::leading_quote(expression).unwrap().text_len()`
	* `str::trailing_quote(expression).unwrap().text_len()`
* Avoid comparing the string content if we already know that it's
implicitly concatenated

## Test Plan

`cargo insta test`
This commit is contained in:
Dhruv Manilawala 2024-06-03 18:34:03 +05:30 committed by GitHub
parent 4a155e2b22
commit f4e23d2dff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 77 additions and 55 deletions

View file

@ -1,13 +1,12 @@
use ruff_python_ast::Expr;
use ruff_python_ast::{Expr, ExprStringLiteral};
use ruff_python_semantic::{ScopeId, Snapshot};
use ruff_text_size::TextRange;
/// A collection of AST nodes that are deferred for later visitation. Used to, e.g., store
/// functions, whose bodies shouldn't be visited until all module-level definitions have been
/// visited.
#[derive(Debug, Default)]
pub(crate) struct Visit<'a> {
pub(crate) string_type_definitions: Vec<(TextRange, &'a str, Snapshot)>,
pub(crate) string_type_definitions: Vec<(&'a ExprStringLiteral, Snapshot)>,
pub(crate) future_type_definitions: Vec<(&'a Expr, Snapshot)>,
pub(crate) type_param_definitions: Vec<(&'a Expr, Snapshot)>,
pub(crate) functions: Vec<Snapshot>,

View file

@ -1011,12 +1011,10 @@ impl<'a> Visitor<'a> for Checker<'a> {
&& self.semantic.future_annotations_or_stub()
&& (self.semantic.in_annotation() || self.source_type.is_stub())
{
if let Expr::StringLiteral(ast::ExprStringLiteral { value, .. }) = expr {
self.visit.string_type_definitions.push((
expr.range(),
value.to_str(),
self.semantic.snapshot(),
));
if let Expr::StringLiteral(string_literal) = expr {
self.visit
.string_type_definitions
.push((string_literal, self.semantic.snapshot()));
} else {
self.visit
.future_type_definitions
@ -1426,13 +1424,11 @@ impl<'a> Visitor<'a> for Checker<'a> {
}
}
}
Expr::StringLiteral(ast::ExprStringLiteral { value, .. }) => {
Expr::StringLiteral(string_literal) => {
if self.semantic.in_type_definition() && !self.semantic.in_typing_literal() {
self.visit.string_type_definitions.push((
expr.range(),
value.to_str(),
self.semantic.snapshot(),
));
self.visit
.string_type_definitions
.push((string_literal, self.semantic.snapshot()));
}
}
Expr::FString(_) => {
@ -2156,22 +2152,25 @@ impl<'a> Checker<'a> {
let snapshot = self.semantic.snapshot();
while !self.visit.string_type_definitions.is_empty() {
let type_definitions = std::mem::take(&mut self.visit.string_type_definitions);
for (range, value, snapshot) in type_definitions {
if let Ok((expr, kind)) =
parse_type_annotation(value, range, self.locator.contents())
for (string_expr, snapshot) in type_definitions {
if let Ok((parsed_annotation, kind)) =
parse_type_annotation(string_expr, self.locator.contents())
{
let expr = allocator.alloc(expr);
let parsed_annotation = allocator.alloc(parsed_annotation);
let annotation = string_expr.value.to_str();
let range = string_expr.range();
self.semantic.restore(snapshot);
if self.semantic.in_annotation() && self.semantic.in_typing_only_annotation() {
if self.enabled(Rule::QuotedAnnotation) {
pyupgrade::rules::quoted_annotation(self, value, range);
pyupgrade::rules::quoted_annotation(self, annotation, range);
}
}
if self.source_type.is_stub() {
if self.enabled(Rule::QuotedAnnotationInStub) {
flake8_pyi::rules::quoted_annotation_in_stub(self, value, range);
flake8_pyi::rules::quoted_annotation_in_stub(self, annotation, range);
}
}
@ -2184,14 +2183,14 @@ impl<'a> Checker<'a> {
self.semantic.flags |=
SemanticModelFlags::TYPE_DEFINITION | type_definition_flag;
self.visit_expr(expr);
self.visit_expr(parsed_annotation);
} else {
if self.enabled(Rule::ForwardAnnotationSyntaxError) {
self.diagnostics.push(Diagnostic::new(
pyflakes::rules::ForwardAnnotationSyntaxError {
body: value.to_string(),
body: string_expr.value.to_string(),
},
range,
string_expr.range(),
));
}
}

View file

@ -512,10 +512,10 @@ fn check_dynamically_typed<F>(
) where
F: FnOnce() -> String,
{
if let Expr::StringLiteral(ast::ExprStringLiteral { range, value }) = annotation {
if let Expr::StringLiteral(string_expr) = annotation {
// Quoted annotations
if let Ok((parsed_annotation, _)) =
parse_type_annotation(value.to_str(), *range, checker.locator().contents())
parse_type_annotation(string_expr, checker.locator().contents())
{
if type_hint_resolves_to_any(
&parsed_annotation,

View file

@ -177,13 +177,13 @@ pub(crate) fn implicit_optional(checker: &mut Checker, parameters: &Parameters)
continue;
};
if let Expr::StringLiteral(ast::ExprStringLiteral { range, value }) = annotation.as_ref() {
if let Expr::StringLiteral(string_expr) = annotation.as_ref() {
// Quoted annotation.
if let Ok((annotation, kind)) =
parse_type_annotation(value.to_str(), *range, checker.locator().contents())
if let Ok((parsed_annotation, kind)) =
parse_type_annotation(string_expr, checker.locator().contents())
{
let Some(expr) = type_hint_explicitly_allows_none(
&annotation,
&parsed_annotation,
checker.semantic(),
checker.locator(),
checker.settings.target_version.minor(),

View file

@ -112,8 +112,8 @@ impl<'a> TypingTarget<'a> {
..
}) => Some(TypingTarget::PEP604Union(left, right)),
Expr::NoneLiteral(_) => Some(TypingTarget::None),
Expr::StringLiteral(ast::ExprStringLiteral { value, range }) => {
parse_type_annotation(value.to_str(), *range, locator.contents())
Expr::StringLiteral(string_expr) => {
parse_type_annotation(string_expr, locator.contents())
.map_or(None, |(expr, _)| Some(TypingTarget::ForwardReference(expr)))
}
_ => semantic.resolve_qualified_name(expr).map_or(