mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 13:51:16 +00:00
[ty] Support as-patterns in reachability analysis (#19728)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks-instrumented (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks-instrumented (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
## Summary Support `as` patterns in reachability analysis: ```py from typing import assert_never def f(subject: str | int): match subject: case int() as x: pass case str(): pass case _: assert_never(subject) # would previously emit an error ``` Note that we still don't support inferring correct types for the bound name (`x`). Closes https://github.com/astral-sh/ty/issues/928 ## Test Plan New Markdown tests
This commit is contained in:
parent
af8587eabf
commit
739c94f95a
8 changed files with 76 additions and 6 deletions
|
@ -350,6 +350,25 @@ def _(target: None | Foo):
|
||||||
reveal_type(y) # revealed: Literal[1, 3]
|
reveal_type(y) # revealed: Literal[1, 3]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## `as` patterns
|
||||||
|
|
||||||
|
```py
|
||||||
|
def _(target: int | str):
|
||||||
|
y = 1
|
||||||
|
|
||||||
|
match target:
|
||||||
|
case 1 as x:
|
||||||
|
y = 2
|
||||||
|
reveal_type(x) # revealed: @Todo(`match` pattern definition types)
|
||||||
|
case "foo" as x:
|
||||||
|
y = 3
|
||||||
|
reveal_type(x) # revealed: @Todo(`match` pattern definition types)
|
||||||
|
case _:
|
||||||
|
y = 4
|
||||||
|
|
||||||
|
reveal_type(y) # revealed: Literal[2, 3, 4]
|
||||||
|
```
|
||||||
|
|
||||||
## Guard with object that implements `__bool__` incorrectly
|
## Guard with object that implements `__bool__` incorrectly
|
||||||
|
|
||||||
```py
|
```py
|
||||||
|
|
|
@ -338,3 +338,32 @@ def no_invalid_return_diagnostic_here_either[T](x: A[T]) -> ASub[T]:
|
||||||
# is null and void (and therefore we don't emit a diagnostic)
|
# is null and void (and therefore we don't emit a diagnostic)
|
||||||
return x
|
return x
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## More `match` pattern types
|
||||||
|
|
||||||
|
### `as` patterns
|
||||||
|
|
||||||
|
```py
|
||||||
|
from typing import assert_never
|
||||||
|
|
||||||
|
def as_pattern_exhaustive(subject: int | str):
|
||||||
|
match subject:
|
||||||
|
case int() as x:
|
||||||
|
pass
|
||||||
|
case str() as y:
|
||||||
|
pass
|
||||||
|
case _:
|
||||||
|
no_diagnostic_here
|
||||||
|
|
||||||
|
assert_never(subject)
|
||||||
|
|
||||||
|
def as_pattern_non_exhaustive(subject: int | str):
|
||||||
|
match subject:
|
||||||
|
case int() as x:
|
||||||
|
pass
|
||||||
|
case _:
|
||||||
|
this_should_be_an_error # error: [unresolved-reference]
|
||||||
|
|
||||||
|
# this diagnostic is correct: the inferred type of `subject` is `str`
|
||||||
|
assert_never(subject) # error: [type-assertion-failure]
|
||||||
|
```
|
||||||
|
|
|
@ -182,16 +182,19 @@ class ContextManagerThatMightNotRunToCompletion:
|
||||||
with ContextManagerThatMightNotRunToCompletion() as L:
|
with ContextManagerThatMightNotRunToCompletion() as L:
|
||||||
U = ...
|
U = ...
|
||||||
|
|
||||||
match 42:
|
def get_object() -> object:
|
||||||
|
pass
|
||||||
|
|
||||||
|
match get_object():
|
||||||
case {"something": M}:
|
case {"something": M}:
|
||||||
...
|
...
|
||||||
case [*N]:
|
case [*N]:
|
||||||
...
|
...
|
||||||
case [O]:
|
case [O]:
|
||||||
...
|
...
|
||||||
case P | Q: # error: [invalid-syntax] "name capture `P` makes remaining patterns unreachable"
|
case I(foo=R):
|
||||||
...
|
...
|
||||||
case object(foo=R):
|
case P | Q:
|
||||||
...
|
...
|
||||||
|
|
||||||
match 56:
|
match 56:
|
||||||
|
|
|
@ -838,6 +838,13 @@ impl<'db, 'ast> SemanticIndexBuilder<'db, 'ast> {
|
||||||
.collect();
|
.collect();
|
||||||
PatternPredicateKind::Or(predicates)
|
PatternPredicateKind::Or(predicates)
|
||||||
}
|
}
|
||||||
|
ast::Pattern::MatchAs(pattern) => PatternPredicateKind::As(
|
||||||
|
pattern
|
||||||
|
.pattern
|
||||||
|
.as_ref()
|
||||||
|
.map(|p| Box::new(self.predicate_kind(p))),
|
||||||
|
pattern.name.as_ref().map(|name| name.id.clone()),
|
||||||
|
),
|
||||||
_ => PatternPredicateKind::Unsupported,
|
_ => PatternPredicateKind::Unsupported,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
use ruff_db::files::File;
|
use ruff_db::files::File;
|
||||||
use ruff_index::{Idx, IndexVec};
|
use ruff_index::{Idx, IndexVec};
|
||||||
use ruff_python_ast::Singleton;
|
use ruff_python_ast::{Singleton, name::Name};
|
||||||
|
|
||||||
use crate::db::Db;
|
use crate::db::Db;
|
||||||
use crate::semantic_index::expression::Expression;
|
use crate::semantic_index::expression::Expression;
|
||||||
|
@ -136,6 +136,7 @@ pub(crate) enum PatternPredicateKind<'db> {
|
||||||
Value(Expression<'db>),
|
Value(Expression<'db>),
|
||||||
Or(Vec<PatternPredicateKind<'db>>),
|
Or(Vec<PatternPredicateKind<'db>>),
|
||||||
Class(Expression<'db>, ClassPatternKind),
|
Class(Expression<'db>, ClassPatternKind),
|
||||||
|
As(Option<Box<PatternPredicateKind<'db>>>, Option<Name>),
|
||||||
Unsupported,
|
Unsupported,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -340,6 +340,10 @@ fn pattern_kind_to_type<'db>(db: &'db dyn Db, kind: &PatternPredicateKind<'db>)
|
||||||
PatternPredicateKind::Or(predicates) => {
|
PatternPredicateKind::Or(predicates) => {
|
||||||
UnionType::from_elements(db, predicates.iter().map(|p| pattern_kind_to_type(db, p)))
|
UnionType::from_elements(db, predicates.iter().map(|p| pattern_kind_to_type(db, p)))
|
||||||
}
|
}
|
||||||
|
PatternPredicateKind::As(pattern, _) => pattern
|
||||||
|
.as_deref()
|
||||||
|
.map(|p| pattern_kind_to_type(db, p))
|
||||||
|
.unwrap_or_else(|| Type::object(db)),
|
||||||
PatternPredicateKind::Unsupported => Type::Never,
|
PatternPredicateKind::Unsupported => Type::Never,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -761,6 +765,10 @@ impl ReachabilityConstraints {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
PatternPredicateKind::As(pattern, _) => pattern
|
||||||
|
.as_deref()
|
||||||
|
.map(|p| Self::analyze_single_pattern_predicate_kind(db, p, subject_ty))
|
||||||
|
.unwrap_or(Truthiness::AlwaysTrue),
|
||||||
PatternPredicateKind::Unsupported => Truthiness::Ambiguous,
|
PatternPredicateKind::Unsupported => Truthiness::Ambiguous,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3584,7 +3584,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
|
||||||
fn infer_nested_match_pattern(&mut self, pattern: &ast::Pattern) {
|
fn infer_nested_match_pattern(&mut self, pattern: &ast::Pattern) {
|
||||||
match pattern {
|
match pattern {
|
||||||
ast::Pattern::MatchValue(match_value) => {
|
ast::Pattern::MatchValue(match_value) => {
|
||||||
self.infer_expression(&match_value.value);
|
self.infer_maybe_standalone_expression(&match_value.value);
|
||||||
}
|
}
|
||||||
ast::Pattern::MatchSequence(match_sequence) => {
|
ast::Pattern::MatchSequence(match_sequence) => {
|
||||||
for pattern in &match_sequence.patterns {
|
for pattern in &match_sequence.patterns {
|
||||||
|
@ -3619,7 +3619,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
|
||||||
for keyword in &arguments.keywords {
|
for keyword in &arguments.keywords {
|
||||||
self.infer_nested_match_pattern(&keyword.pattern);
|
self.infer_nested_match_pattern(&keyword.pattern);
|
||||||
}
|
}
|
||||||
self.infer_expression(cls);
|
self.infer_maybe_standalone_expression(cls);
|
||||||
}
|
}
|
||||||
ast::Pattern::MatchAs(match_as) => {
|
ast::Pattern::MatchAs(match_as) => {
|
||||||
if let Some(pattern) = &match_as.pattern {
|
if let Some(pattern) = &match_as.pattern {
|
||||||
|
|
|
@ -410,6 +410,9 @@ impl<'db, 'ast> NarrowingConstraintsBuilder<'db, 'ast> {
|
||||||
PatternPredicateKind::Or(predicates) => {
|
PatternPredicateKind::Or(predicates) => {
|
||||||
self.evaluate_match_pattern_or(subject, predicates, is_positive)
|
self.evaluate_match_pattern_or(subject, predicates, is_positive)
|
||||||
}
|
}
|
||||||
|
PatternPredicateKind::As(pattern, _) => pattern
|
||||||
|
.as_deref()
|
||||||
|
.and_then(|p| self.evaluate_pattern_predicate_kind(p, subject, is_positive)),
|
||||||
PatternPredicateKind::Unsupported => None,
|
PatternPredicateKind::Unsupported => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue