Disallow f-strings in match pattern literal (#7857)
## Summary
This PR fixes a bug to disallow f-strings in match pattern literal.
```
literal_pattern ::= signed_number
| signed_number "+" NUMBER
| signed_number "-" NUMBER
| strings
| "None"
| "True"
| "False"
| signed_number: NUMBER | "-" NUMBER
```
Source:
https://docs.python.org/3/reference/compound_stmts.html#grammar-token-python-grammar-literal_pattern
Also,
```console
$ python /tmp/t.py
File "/tmp/t.py", line 4
case "hello " f"{name}":
^^^^^^^^^^^^^^^^^^
SyntaxError: patterns may only match literals and attribute lookups
```
## Test Plan
Update existing test case and accordingly the snapshots. Also, add a new
test case to verify that the parser does raise an error.
This commit is contained in:
@@ -1143,6 +1143,24 @@ match x:
|
||||
insta::assert_debug_snapshot!(parse_ast);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_match_pattern_fstring_literal() {
|
||||
// F-string literal is not allowed in match pattern.
|
||||
let parse_error = parse_suite(
|
||||
r#"
|
||||
match x:
|
||||
case f"{y}":
|
||||
pass
|
||||
"#,
|
||||
"<test>",
|
||||
)
|
||||
.err();
|
||||
assert!(
|
||||
parse_error.is_some(),
|
||||
"expected parse error when f-string literal is used in match pattern"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_variadic_generics() {
|
||||
let parse_ast = parse_suite(
|
||||
@@ -1285,7 +1303,9 @@ f'{f"{3.1415=:.1f}":*^20}'
|
||||
|
||||
{"foo " f"bar {x + y} " "baz": 10}
|
||||
match foo:
|
||||
case "foo " f"bar {x + y} " "baz":
|
||||
case "one":
|
||||
pass
|
||||
case "implicitly " "concatenated":
|
||||
pass
|
||||
|
||||
f"\{foo}\{bar:\}"
|
||||
|
||||
Reference in New Issue
Block a user