mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-18 01:20:24 +00:00
[red-knot] Add control flow support for match statement (#13241)
## Summary This PR adds support for control flow for match statement. It also adds the necessary infrastructure required for narrowing constraints in case blocks and implements the logic for `PatternMatchSingleton` which is either `None` / `True` / `False`. Even after this the inferred type doesn't get simplified completely, there's a TODO for that in the test code. ## Test Plan Add test cases for control flow for (a) when there's a wildcard pattern and (b) when there isn't. There's also a test case to verify the narrowing logic. --------- Co-authored-by: Carl Meyer <carl@astral.sh>
This commit is contained in:
parent
6f53aaf931
commit
62c7d8f6ba
9 changed files with 321 additions and 37 deletions
|
@ -3124,6 +3124,29 @@ impl Pattern {
|
|||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks if the [`Pattern`] is a [wildcard pattern].
|
||||
///
|
||||
/// The following are wildcard patterns:
|
||||
/// ```python
|
||||
/// match subject:
|
||||
/// case _ as x: ...
|
||||
/// case _ | _: ...
|
||||
/// case _: ...
|
||||
/// ```
|
||||
///
|
||||
/// [wildcard pattern]: https://docs.python.org/3/reference/compound_stmts.html#wildcard-patterns
|
||||
pub fn is_wildcard(&self) -> bool {
|
||||
match self {
|
||||
Pattern::MatchAs(PatternMatchAs { pattern, .. }) => {
|
||||
pattern.as_deref().map_or(true, Pattern::is_wildcard)
|
||||
}
|
||||
Pattern::MatchOr(PatternMatchOr { patterns, .. }) => {
|
||||
patterns.iter().all(Pattern::is_wildcard)
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// See also [MatchValue](https://docs.python.org/3/library/ast.html#ast.MatchValue)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue