mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:25:17 +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
|
@ -173,3 +173,40 @@ p: "call()"
|
|||
r: "[1, 2]"
|
||||
s: "(1, 2)"
|
||||
```
|
||||
|
||||
## Multi line annotation
|
||||
|
||||
Quoted type annotations should be parsed as if surrounded by parentheses.
|
||||
|
||||
```py
|
||||
def valid(
|
||||
a1: """(
|
||||
int |
|
||||
str
|
||||
)
|
||||
""",
|
||||
a2: """
|
||||
int |
|
||||
str
|
||||
""",
|
||||
):
|
||||
reveal_type(a1) # revealed: int | str
|
||||
reveal_type(a2) # revealed: int | str
|
||||
|
||||
def invalid(
|
||||
# error: [invalid-syntax-in-forward-annotation]
|
||||
a1: """
|
||||
int |
|
||||
str)
|
||||
""",
|
||||
# error: [invalid-syntax-in-forward-annotation]
|
||||
a2: """
|
||||
int) |
|
||||
str
|
||||
""",
|
||||
# error: [invalid-syntax-in-forward-annotation]
|
||||
a3: """
|
||||
(int)) """,
|
||||
):
|
||||
pass
|
||||
```
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use ruff_db::source::source_text;
|
||||
use ruff_python_ast::str::raw_contents;
|
||||
use ruff_python_ast::{self as ast, ModExpression, StringFlags};
|
||||
use ruff_python_parser::{parse_expression_range, Parsed};
|
||||
use ruff_python_ast::{self as ast, ModExpression};
|
||||
use ruff_python_parser::Parsed;
|
||||
use ruff_text_size::Ranged;
|
||||
|
||||
use crate::declare_lint;
|
||||
|
@ -153,19 +153,9 @@ pub(crate) fn parse_string_annotation(
|
|||
} else if raw_contents(node_text)
|
||||
.is_some_and(|raw_contents| raw_contents == string_literal.as_str())
|
||||
{
|
||||
let range_excluding_quotes = string_literal
|
||||
.range()
|
||||
.add_start(string_literal.flags.opener_len())
|
||||
.sub_end(string_literal.flags.closer_len());
|
||||
|
||||
// TODO: Support multiline strings like:
|
||||
// ```py
|
||||
// x: """
|
||||
// int
|
||||
// | float
|
||||
// """ = 1
|
||||
// ```
|
||||
match parse_expression_range(source.as_str(), range_excluding_quotes) {
|
||||
let parsed =
|
||||
ruff_python_parser::parse_string_annotation(source.as_str(), string_literal);
|
||||
match parsed {
|
||||
Ok(parsed) => return Some(parsed),
|
||||
Err(parse_error) => context.report_lint(
|
||||
&INVALID_SYNTAX_IN_FORWARD_ANNOTATION,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue