Skip to content

Commit

Permalink
Consider exactly _ for wildcard pattern (#10896)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Apr 16, 2024
1 parent 8784f76 commit ec25008
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
match foo:
case foo_bar: ...
case _: ...
6 changes: 5 additions & 1 deletion crates/ruff_python_parser/src/parser/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,10 +494,14 @@ impl<'src> Parser<'src> {
};
let range = self.node_range(start);

// test_ok match_as_pattern
// match foo:
// case foo_bar: ...
// case _: ...
Pattern::MatchAs(ast::PatternMatchAs {
range,
pattern: None,
name: if name.contains('_') {
name: if &*name == "_" {
None
} else {
Some(ast::Identifier {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/ok/match_as_pattern.py
---
## AST

```
Module(
ModModule {
range: 0..49,
body: [
Match(
StmtMatch {
range: 0..48,
subject: Name(
ExprName {
range: 6..9,
id: "foo",
ctx: Load,
},
),
cases: [
MatchCase {
range: 15..32,
pattern: MatchAs(
PatternMatchAs {
range: 20..27,
pattern: None,
name: Some(
Identifier {
id: "foo_bar",
range: 20..27,
},
),
},
),
guard: None,
body: [
Expr(
StmtExpr {
range: 29..32,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 29..32,
},
),
},
),
],
},
MatchCase {
range: 37..48,
pattern: MatchAs(
PatternMatchAs {
range: 42..43,
pattern: None,
name: None,
},
),
guard: None,
body: [
Expr(
StmtExpr {
range: 45..48,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 45..48,
},
),
},
),
],
},
],
},
),
],
},
)
```

0 comments on commit ec25008

Please sign in to comment.