mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 04:45:01 +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
|
@ -13,4 +13,68 @@ F722.py:13:4: F722 Syntax error in forward annotation: `List[int]☃`
|
|||
|
|
||||
13 | X: """List[int]"""'☃' = []
|
||||
| ^^^^^^^^^^^^^^^^^^ F722
|
||||
14 |
|
||||
15 | # Type annotations with triple quotes can contain newlines and indentation
|
||||
|
|
||||
|
||||
F722.py:30:11: F722 Syntax error in forward annotation: `
|
||||
int |
|
||||
str)
|
||||
`
|
||||
|
|
||||
28 | """
|
||||
29 |
|
||||
30 | invalid1: """
|
||||
| ___________^
|
||||
31 | | int |
|
||||
32 | | str)
|
||||
33 | | """
|
||||
| |___^ F722
|
||||
34 |
|
||||
35 | invalid2: """
|
||||
|
|
||||
|
||||
F722.py:35:11: F722 Syntax error in forward annotation: `
|
||||
int) |
|
||||
str
|
||||
`
|
||||
|
|
||||
33 | """
|
||||
34 |
|
||||
35 | invalid2: """
|
||||
| ___________^
|
||||
36 | | int) |
|
||||
37 | | str
|
||||
38 | | """
|
||||
| |___^ F722
|
||||
39 | invalid3: """
|
||||
40 | ((int)
|
||||
|
|
||||
|
||||
F722.py:39:11: F722 Syntax error in forward annotation: `
|
||||
((int)
|
||||
`
|
||||
|
|
||||
37 | str
|
||||
38 | """
|
||||
39 | invalid3: """
|
||||
| ___________^
|
||||
40 | | ((int)
|
||||
41 | | """
|
||||
| |___^ F722
|
||||
42 | invalid4: """
|
||||
43 | (int
|
||||
|
|
||||
|
||||
F722.py:42:11: F722 Syntax error in forward annotation: `
|
||||
(int
|
||||
`
|
||||
|
|
||||
40 | ((int)
|
||||
41 | """
|
||||
42 | invalid4: """
|
||||
| ___________^
|
||||
43 | | (int
|
||||
44 | | """
|
||||
| |___^ F722
|
||||
|
|
||||
|
|
|
@ -158,4 +158,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]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue