Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 12 additions & 1 deletion pyrefly/lib/alt/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,18 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
}
Type::Union(ts) => ts
.iter()
.flat_map(|t| self.iterate(t, range, errors))
.filter_map(|t| {
let is_iterable = self.unwrap_iterable(t).is_some()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add is_iterable be added to types.rs and invoked from there instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving the function out is a good idea, but I think placing the function types.rs won't work since pyrefly_types is in a different crate. From the way it's defined right now we'd end up with circular dependencies?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's correct, it needs access to self.

Let's just pull it into a helper method?

We probably should make an AnswersSolver module for "type utilities" that require access to a solver and therefore cannot be in types.rs at some point.

|| matches!(t, Type::Tuple(_))
|| matches!(t, Type::ClassType(cls) if self.as_tuple(cls).is_some());

if is_iterable {
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()))
"#,
);