Open
Description
Feature or enhancement
Allow match-case to match literal sets (permutation invariant!)
Pitch
match {2, 1}:
case {1, 2}: # ✘: SyntaxError
print("Sets are unordered!")
Note that unordered match-case is already implemented for matching dicts:
match {2:0, 1:0}:
case {1:0, 2:0}: # ✔
print("Mappings work!")
Limitations
The same limitations as for matching the keys in mapping pattern would apply, i.e. only literal_pattern | constant_pattern
are allowed inside the set (otherwise potentially O(n²) cost occur). In particular, the set-pattern can already be emulated today by using an equivalent mapping pattern with object()
as the value pattern for every key.
Previous discussion
https://discuss.python.org/t/structural-pattern-matching-literal-sets/28394/1