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
15 changes: 14 additions & 1 deletion pyrefly/lib/alt/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,12 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
}
}

fn is_iterable(&self, ty: &Type) -> bool {
self.unwrap_iterable(ty).is_some()
|| matches!(ty, Type::Tuple(_))
|| matches!(ty, Type::ClassType(cls) if self.as_tuple(cls).is_some())
}

/// Given an `iterable` type, determine the iteration type; this is the type
/// of `x` if we were to loop using `for x in iterable`.
///
Expand All @@ -584,7 +590,14 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
}
Type::Union(ts) => ts
.iter()
.flat_map(|t| self.iterate(t, range, errors))
.filter_map(|t| {
if self.is_iterable(t) {
Some(self.iterate(t, range, errors))
} else {
None
}
})
.flatten()
.collect(),
_ => {
let ty = self
Expand Down
19 changes: 19 additions & 0 deletions pyrefly/lib/test/pattern_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,22 @@ def my_func(x: dict[MyEnumType, int]) -> int:
return 0
"#,
);

testcase!(
test_union_pattern_match_sequence,
r#"
from typing import *

class T: ...

def my_func(x: T | tuple[T, T]):
match x:
case T(), T(): # Should not error: Type `T` is not iterable
print("Two T")
case T():
print("One T")

my_func(T())
my_func((T(), T()))
"#,
);