Skip to content

[match-case] Literal Sets #107497

Open
Open
@randolf-scholz

Description

@randolf-scholz

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

Metadata

Metadata

Assignees

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions