mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 20:42:10 +00:00
Parse triple quoted string annotations as if parenthesized (#15387)
## Summary Resolves #9467 Parse quoted annotations as if the string content is inside parenthesis. With this logic `x` and `y` in this example are equal: ```python y: """ int | str """ z: """( int | str ) """ ``` Also this rule only applies to triple quotes([link](https://github.com/python/typing-council/issues/9#issuecomment-1890808610)). This PR is based on the [comments](https://github.com/astral-sh/ruff/issues/9467#issuecomment-2579180991) on the issue. I did one extra change, since we don't want any indentation tokens I am setting the `State::Other` as the initial state of the Lexer. Remaining work: - [x] Add a test case for red-knot. - [x] Add more tests. ## Test Plan Added a test which previously failed because quoted annotation contained indentation. Added an mdtest for red-knot. Updated previous test. Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com> Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
parent
d2656e88a3
commit
cf4ab7cba1
10 changed files with 295 additions and 31 deletions
|
@ -2,10 +2,10 @@
|
|||
|
||||
use ruff_python_ast::relocate::relocate_expr;
|
||||
use ruff_python_ast::str::raw_contents;
|
||||
use ruff_python_ast::{Expr, ExprStringLiteral, ModExpression, StringFlags, StringLiteral};
|
||||
use ruff_python_ast::{Expr, ExprStringLiteral, ModExpression, StringLiteral};
|
||||
use ruff_text_size::Ranged;
|
||||
|
||||
use crate::{parse_expression, parse_expression_range, ParseError, Parsed};
|
||||
use crate::{parse_expression, parse_string_annotation, ParseError, Parsed};
|
||||
|
||||
type AnnotationParseResult = Result<ParsedAnnotation, ParseError>;
|
||||
|
||||
|
@ -81,12 +81,8 @@ fn parse_simple_type_annotation(
|
|||
string_literal: &StringLiteral,
|
||||
source: &str,
|
||||
) -> AnnotationParseResult {
|
||||
let range_excluding_quotes = string_literal
|
||||
.range()
|
||||
.add_start(string_literal.flags.opener_len())
|
||||
.sub_end(string_literal.flags.closer_len());
|
||||
Ok(ParsedAnnotation {
|
||||
parsed: parse_expression_range(source, range_excluding_quotes)?,
|
||||
parsed: parse_string_annotation(source, string_literal)?,
|
||||
kind: AnnotationKind::Simple,
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue