Treat match and case as soft keywords in lambda assignments (#4623)

This commit is contained in:
Charlie Marsh 2023-03-04 12:42:05 -05:00 committed by GitHub
parent 713dd2b91e
commit 55fc0e83f3
3 changed files with 243 additions and 1 deletions

View file

@ -604,6 +604,8 @@ match match:
case 1: pass
case 2:
pass
match = lambda query: query == event
print(match(12))
"#,
"<test>",
)

View file

@ -1671,4 +1671,240 @@ expression: parse_ast
],
},
},
Located {
location: Location {
row: 21,
column: 0,
},
end_location: Some(
Location {
row: 21,
column: 36,
},
),
custom: (),
node: Assign {
targets: [
Located {
location: Location {
row: 21,
column: 0,
},
end_location: Some(
Location {
row: 21,
column: 5,
},
),
custom: (),
node: Name {
id: "match",
ctx: Store,
},
},
],
value: Located {
location: Location {
row: 21,
column: 8,
},
end_location: Some(
Location {
row: 21,
column: 36,
},
),
custom: (),
node: Lambda {
args: Arguments {
posonlyargs: [],
args: [
Located {
location: Location {
row: 21,
column: 15,
},
end_location: Some(
Location {
row: 21,
column: 20,
},
),
custom: (),
node: ArgData {
arg: "query",
annotation: None,
type_comment: None,
},
},
],
vararg: None,
kwonlyargs: [],
kw_defaults: [],
kwarg: None,
defaults: [],
},
body: Located {
location: Location {
row: 21,
column: 22,
},
end_location: Some(
Location {
row: 21,
column: 36,
},
),
custom: (),
node: Compare {
left: Located {
location: Location {
row: 21,
column: 22,
},
end_location: Some(
Location {
row: 21,
column: 27,
},
),
custom: (),
node: Name {
id: "query",
ctx: Load,
},
},
ops: [
Eq,
],
comparators: [
Located {
location: Location {
row: 21,
column: 31,
},
end_location: Some(
Location {
row: 21,
column: 36,
},
),
custom: (),
node: Name {
id: "event",
ctx: Load,
},
},
],
},
},
},
},
type_comment: None,
},
},
Located {
location: Location {
row: 22,
column: 0,
},
end_location: Some(
Location {
row: 22,
column: 16,
},
),
custom: (),
node: Expr {
value: Located {
location: Location {
row: 22,
column: 0,
},
end_location: Some(
Location {
row: 22,
column: 16,
},
),
custom: (),
node: Call {
func: Located {
location: Location {
row: 22,
column: 0,
},
end_location: Some(
Location {
row: 22,
column: 5,
},
),
custom: (),
node: Name {
id: "print",
ctx: Load,
},
},
args: [
Located {
location: Location {
row: 22,
column: 6,
},
end_location: Some(
Location {
row: 22,
column: 15,
},
),
custom: (),
node: Call {
func: Located {
location: Location {
row: 22,
column: 6,
},
end_location: Some(
Location {
row: 22,
column: 11,
},
),
custom: (),
node: Name {
id: "match",
ctx: Load,
},
},
args: [
Located {
location: Location {
row: 22,
column: 12,
},
end_location: Some(
Location {
row: 22,
column: 14,
},
),
custom: (),
node: Constant {
value: Int(
12,
),
kind: None,
},
},
],
keywords: [],
},
},
],
keywords: [],
},
},
},
},
]

View file

@ -59,11 +59,15 @@ where
let mut nesting = 0;
let mut first = true;
let mut seen_colon = false;
let mut seen_lambda = false;
while let Some(Ok((_, tok, _))) = self.underlying.peek() {
match tok {
Tok::Newline => break,
Tok::Lambda if nesting == 0 => seen_lambda = true,
Tok::Colon if nesting == 0 => {
if !first {
if seen_lambda {
seen_lambda = false;
} else if !first {
seen_colon = true;
}
}