Skip to content

Commit

Permalink
Do not implicitly parenthesize single quote annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack committed Jan 13, 2025
1 parent edc7345 commit be3115f
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 15 deletions.
17 changes: 8 additions & 9 deletions crates/red_knot_python_semantic/src/types/string_annotation.rs
Original file line number Diff line number Diff line change
@@ -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_parenthesized_expression_range, Parsed};
use ruff_python_parser::{parse_expression_range, parse_parenthesized_expression_range, Parsed};
use ruff_text_size::Ranged;

use crate::declare_lint;
Expand Down Expand Up @@ -158,14 +158,13 @@ pub(crate) fn parse_string_annotation(
.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_parenthesized_expression_range(source.as_str(), range_excluding_quotes) {
let parsed = if string_literal.flags.is_triple_quoted() {
parse_parenthesized_expression_range(source.as_str(), range_excluding_quotes)
} else {
parse_expression_range(source.as_str(), range_excluding_quotes)
};

match parsed {
Ok(parsed) => return Some(parsed),
Err(parse_error) => context.report_lint(
&INVALID_SYNTAX_IN_FORWARD_ANNOTATION,
Expand Down
3 changes: 3 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pyflakes/F722.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ def g() -> "///":
str
)
"""

# single quotes are not implicitly parenthesized
invalid: "\n int"
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ a: '''\\
list[int]''' = [42]


# TODO: These are valid too. String annotations are assumed to be enclosed in parentheses.
# https://github.com/astral-sh/ruff/issues/9467

def f(a: '''
list[int]
''' = []): ...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ F722.py:13:4: F722 Syntax error in forward annotation: `List[int]☃`
14 |
15 | y: """
|

F722.py:29:10: F722 Syntax error in forward annotation: `
int`
|
28 | # single quotes are not implicitly parenthesized
29 | invalid: "\n int"
| ^^^^^^^^ F722
|
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,75 @@ UP037_2.pyi:32:4: UP037 [*] Remove quotes from type annotation
33 |+list[int]) = [42]
34 34 |
35 35 |
36 36 | # TODO: These are valid too. String annotations are assumed to be enclosed in parentheses.
36 36 | def f(a: '''
UP037_2.pyi:36:10: UP037 [*] Remove quotes from type annotation
|
36 | def f(a: '''
| __________^
37 | | list[int]
38 | | ''' = []): ...
| |_______^ UP037
|
= help: Remove quotes

Safe fix
33 33 | list[int]''' = [42]
34 34 |
35 35 |
36 |-def f(a: '''
36 |+def f(a:
37 37 | list[int]
38 |- ''' = []): ...
38 |+ = []): ...
39 39 |
40 40 |
41 41 | def f(a: Foo['''
UP037_2.pyi:41:14: UP037 [*] Remove quotes from type annotation
|
41 | def f(a: Foo['''
| ______________^
42 | | Bar
43 | | [
44 | | Multi |
45 | | Line
46 | | ] # Comment''']): ...
| |___________________^ UP037
|
= help: Remove quotes

Safe fix
38 38 | ''' = []): ...
39 39 |
40 40 |
41 |-def f(a: Foo['''
41 |+def f(a: Foo[(
42 42 | Bar
43 43 | [
44 44 | Multi |
45 45 | Line
46 |- ] # Comment''']): ...
46 |+ ] # Comment
47 |+)]): ...
47 48 |
48 49 |
49 50 | a: '''list

UP037_2.pyi:49:4: UP037 [*] Remove quotes from type annotation
|
49 | a: '''list
| ____^
50 | | [int]''' = [42]
| |________^ UP037
|
= help: Remove quotes

Safe fix
46 46 | ] # Comment''']): ...
47 47 |
48 48 |
49 |-a: '''list
50 |-[int]''' = [42]
49 |+a: (list
50 |+[int]) = [42]
12 changes: 10 additions & 2 deletions crates/ruff_python_parser/src/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use ruff_python_ast::str::raw_contents;
use ruff_python_ast::{Expr, ExprStringLiteral, ModExpression, StringFlags, StringLiteral};
use ruff_text_size::Ranged;

use crate::{parse_expression, parse_parenthesized_expression_range, ParseError, Parsed};
use crate::{
parse_expression, parse_expression_range, parse_parenthesized_expression_range, ParseError,
Parsed,
};

type AnnotationParseResult = Result<ParsedAnnotation, ParseError>;

Expand Down Expand Up @@ -85,8 +88,13 @@ fn parse_simple_type_annotation(
.range()
.add_start(string_literal.flags.opener_len())
.sub_end(string_literal.flags.closer_len());
let parsed = if string_literal.flags.is_triple_quoted() {
parse_parenthesized_expression_range(source, range_excluding_quotes)?
} else {
parse_expression_range(source, range_excluding_quotes)?
};
Ok(ParsedAnnotation {
parsed: parse_parenthesized_expression_range(source, range_excluding_quotes)?,
parsed,
kind: AnnotationKind::Simple,
})
}
Expand Down

0 comments on commit be3115f

Please sign in to comment.