Skip to content

Commit

Permalink
parse stringized annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack committed Apr 13, 2024
1 parent e9870fe commit 7cf1043
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
25 changes: 25 additions & 0 deletions crates/ruff_python_parser/src/parser.rs
Expand Up @@ -1515,6 +1515,31 @@ a,b
()
(a,)
((a,b))
"#
.trim(),
)
.unwrap();
insta::assert_debug_snapshot!(parse_ast);
}

#[test]
fn test_stringized_annotation() {
let parse_ast = parse_suite(
r#"
from typing_extensions import TypeAlias
Alias1 = (
int
| str
| bool
) # parentheses required
Alias2: TypeAlias = """
int
| str
| bool
"""
"#
.trim(),
)
Expand Down
@@ -0,0 +1,117 @@
---
source: crates/ruff_python_parser/src/parser.rs
expression: parse_ast
---
[
ImportFrom(
StmtImportFrom {
range: 0..39,
module: Some(
Identifier {
id: "typing_extensions",
range: 5..22,
},
),
names: [
Alias {
range: 30..39,
name: Identifier {
id: "TypeAlias",
range: 30..39,
},
asname: None,
},
],
level: Some(
0,
),
},
),
Assign(
StmtAssign {
range: 42..83,
targets: [
Name(
ExprName {
range: 42..48,
id: "Alias1",
ctx: Store,
},
),
],
value: BinOp(
ExprBinOp {
range: 57..81,
left: BinOp(
ExprBinOp {
range: 57..70,
left: Name(
ExprName {
range: 57..60,
id: "int",
ctx: Load,
},
),
op: BitOr,
right: Name(
ExprName {
range: 67..70,
id: "str",
ctx: Load,
},
),
},
),
op: BitOr,
right: Name(
ExprName {
range: 77..81,
id: "bool",
ctx: Load,
},
),
},
),
},
),
AnnAssign(
StmtAnnAssign {
range: 109..165,
target: Name(
ExprName {
range: 109..115,
id: "Alias2",
ctx: Store,
},
),
annotation: Name(
ExprName {
range: 117..126,
id: "TypeAlias",
ctx: Load,
},
),
value: Some(
StringLiteral(
ExprStringLiteral {
range: 129..165,
value: StringLiteralValue {
inner: Single(
StringLiteral {
range: 129..165,
value: "\n int\n | str\n | bool\n",
flags: StringLiteralFlags {
quote_style: Double,
prefix: Empty,
triple_quoted: true,
},
},
),
},
},
),
),
simple: true,
},
),
]

0 comments on commit 7cf1043

Please sign in to comment.