Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/pyrefly_config/src/error_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ pub enum ErrorKind {
InvalidOverload,
/// An error related to ParamSpec definition or usage.
InvalidParamSpec,
/// An error caused by an invalid match pattern.
InvalidPattern,
/// A use of `typing.Self` in a context where Pyrefly does not recognize it as
/// mapping to a valid class type.
InvalidSelfType,
Expand Down
20 changes: 20 additions & 0 deletions pyrefly/lib/alt/narrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use pyrefly_types::facet::UnresolvedFacetKind;
use pyrefly_types::simplify::intersect;
use pyrefly_types::type_info::JoinStyle;
use pyrefly_util::prelude::SliceExt;
use pyrefly_util::visit::Visit;
use ruff_python_ast::Arguments;
use ruff_python_ast::AtomicNodeIndex;
use ruff_python_ast::Expr;
Expand Down Expand Up @@ -752,6 +753,25 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
let right = self.expr_infer(v, errors);
self.narrow_isinstance(ty, &right)
}
AtomicNarrowOp::IsInstancePattern(v) => {
let right = self.expr_infer(v, errors);
let mut contains_subscript = false;
v.visit(&mut |e| {
if matches!(e, Expr::Subscript(_)) {
contains_subscript = true;
}
});
self.check_type_is_class_object(
right.clone(),
Some(ty.clone()),
contains_subscript,
v.range(),
&FunctionKind::IsInstance,
errors,
ErrorKind::InvalidPattern,
);
self.narrow_isinstance(ty, &right)
}
AtomicNarrowOp::IsNotInstance(v) => {
let right = self.expr_infer(v, errors);
self.narrow_is_not_instance(ty, &right)
Expand Down
20 changes: 11 additions & 9 deletions pyrefly/lib/alt/special_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,22 +298,23 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
self.stdlib.bool().clone().to_type()
}

fn check_type_is_class_object(
pub(crate) fn check_type_is_class_object(
&self,
ty: Type,
object_type: Option<Type>,
contains_subscript: bool,
range: TextRange,
func_kind: &FunctionKind,
errors: &ErrorCollector,
error_kind: ErrorKind,
) {
for ty in self.as_class_info(ty) {
if let Type::ClassDef(cls) = &ty {
if cls.has_toplevel_qname("typing", "Any") {
self.error(
errors,
range,
ErrorInfo::Kind(ErrorKind::InvalidArgument),
ErrorInfo::Kind(error_kind),
"Expected class object, got `Any`".to_owned(),
);
}
Expand All @@ -323,7 +324,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
self.error(
errors,
range,
ErrorInfo::Kind(ErrorKind::InvalidArgument),
ErrorInfo::Kind(error_kind),
format!("NewType `{}` not allowed in {}", cls.name(), func_display(),),
);
}
Expand All @@ -332,7 +333,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
self.error(
errors,
range,
ErrorInfo::Kind(ErrorKind::InvalidArgument),
ErrorInfo::Kind(error_kind),
format!(
"TypedDict `{}` not allowed as second argument to {}",
cls.name(),
Expand All @@ -346,7 +347,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
self.error(
errors,
range,
ErrorInfo::Kind(ErrorKind::InvalidArgument),
ErrorInfo::Kind(error_kind),
format!("Protocol `{}` is not decorated with @runtime_checkable and cannot be used with {}", cls.name(), func_display()),
);
} else {
Expand All @@ -358,7 +359,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
self.error(
errors,
range,
ErrorInfo::Kind(ErrorKind::InvalidArgument),
ErrorInfo::Kind(error_kind),
format!("Protocol `{}` has non-method members and cannot be used with issubclass()", cls.name()),
);
}
Expand Down Expand Up @@ -422,7 +423,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
self.error(
errors,
range,
ErrorInfo::Kind(ErrorKind::InvalidArgument),
ErrorInfo::Kind(error_kind),
format!(
"Expected class object, got parameterized generic type: `{}`",
self.for_display(ty)
Expand All @@ -433,15 +434,15 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
self.error(
errors,
range,
ErrorInfo::Kind(ErrorKind::InvalidArgument),
ErrorInfo::Kind(error_kind),
format!("Expected class object, got special form `{}`", special_form),
);
}
} else if self.unwrap_class_object_silently(&ty).is_none() {
self.error(
errors,
range,
ErrorInfo::Kind(ErrorKind::InvalidArgument),
ErrorInfo::Kind(error_kind),
format!("Expected class object, got `{}`", self.for_display(ty)),
);
} else {
Expand Down Expand Up @@ -534,6 +535,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
classinfo_expr.range(),
func_kind,
errors,
ErrorKind::InvalidArgument,
);
}

Expand Down
6 changes: 6 additions & 0 deletions pyrefly/lib/binding/narrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub enum AtomicNarrowOp {
Eq(Expr),
NotEq(Expr),
IsInstance(Expr),
IsInstancePattern(Expr),
IsNotInstance(Expr),
IsSubclass(Expr),
IsNotSubclass(Expr),
Expand Down Expand Up @@ -119,6 +120,9 @@ impl DisplayWith<ModuleInfo> for AtomicNarrowOp {
AtomicNarrowOp::Eq(expr) => write!(f, "Eq({})", expr.display_with(ctx)),
AtomicNarrowOp::NotEq(expr) => write!(f, "NotEq({})", expr.display_with(ctx)),
AtomicNarrowOp::IsInstance(expr) => write!(f, "IsInstance({})", expr.display_with(ctx)),
AtomicNarrowOp::IsInstancePattern(expr) => {
write!(f, "IsInstancePattern({})", expr.display_with(ctx))
}
AtomicNarrowOp::IsNotInstance(expr) => {
write!(f, "IsNotInstance({})", expr.display_with(ctx))
}
Expand Down Expand Up @@ -220,6 +224,7 @@ impl AtomicNarrowOp {
Self::Is(v) => Self::IsNot(v.clone()),
Self::IsNot(v) => Self::Is(v.clone()),
Self::IsInstance(v) => Self::IsNotInstance(v.clone()),
Self::IsInstancePattern(v) => Self::IsNotInstance(v.clone()),
Self::IsNotInstance(v) => Self::IsInstance(v.clone()),
Self::IsSubclass(v) => Self::IsNotSubclass(v.clone()),
Self::IsNotSubclass(v) => Self::IsSubclass(v.clone()),
Expand Down Expand Up @@ -875,6 +880,7 @@ impl NarrowOps {
// Technically the `__class__` attribute can be mutated, but code that does that
// probably isn't statically analyzable anyway.
| AtomicNarrowOp::IsInstance(..)
| AtomicNarrowOp::IsInstancePattern(..)
| AtomicNarrowOp::IsNotInstance(..)
| AtomicNarrowOp::IsSubclass(..)
| AtomicNarrowOp::IsNotSubclass(..)
Expand Down
2 changes: 1 addition & 1 deletion pyrefly/lib/binding/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ impl<'a> BindingsBuilder<'a> {
}
Pattern::MatchClass(mut x) => {
self.ensure_expr(&mut x.cls, narrowing_usage);
let narrow_op = AtomicNarrowOp::IsInstance((*x.cls).clone());
let narrow_op = AtomicNarrowOp::IsInstancePattern((*x.cls).clone());
// Redefining subject_idx to apply the class level narrowing,
// which is used for additional narrowing for attributes below.
let subject_idx = self.insert_binding(
Expand Down
15 changes: 15 additions & 0 deletions pyrefly/lib/test/pattern_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,21 @@ def describe_ok(color: Literal["red", "blue"]):
"#,
);

testcase!(
test_enum_class_pattern_invalid,
r#"
from enum import Enum

class Color(Enum):
RED = "red"

def describe(color: Color) -> None:
match color: # E: Match on `Color` is not exhaustive
case Color.RED(): # E: Expected class object, got `Literal[Color.RED]`
pass
"#,
);

testcase!(
test_non_exhaustive_enum_match_facet_subject,
r#"
Expand Down
17 changes: 17 additions & 0 deletions website/docs/error-kinds.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,23 @@ def f(x, *args: P1.args, **kwargs: P2.kwargs) -> None:

Here, `P1.args` and `P2.kwargs` can't be used together; `*args` and `**kwargs` must come from the same `ParamSpec`.

## invalid-pattern

This error is reported when a pattern is invalid at runtime. For example, enum members are values,
so they must be matched as value patterns (without `()`), not class patterns:

```python
from enum import Enum

class Color(Enum):
RED = "red"

def describe(color: Color) -> None:
match color:
case Color.RED(): # Invalid pattern: use `Color.RED` (without parentheses)
pass
```

## invalid-self-type

This error occurs when `Self` is used in a context Pyrefly does not currently support.
Expand Down
Loading