-
Notifications
You must be signed in to change notification settings - Fork 15.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mark sequence containers as Py_TPFLAGS_SEQUENCE, enabling pattern mat…
…ching PEP634 introduces structural pattern matching. This works out of the box for most parts of protobuf messages, but fails for sequence matching (defined in https://peps.python.org/pep-0634/#sequence-patterns). This is caused by the underlying containers missing the newly introduced Py_TPFLAGS_SEQUENCE flag (see python/cpython@069e81a). This simply adds the flag, making the following fall into the first case: ``` message = test_pb2.TestMessage(int_sequence=(1, 2, 3)) match message: case test_pb2.TestMessage(int_sequence=(1, *rest)): print(f"message.int_sequence is a seq starting with 1, ending in {rest}") case _: print(f"No case on 'int_sequence' matched! Value: {message.int_sequence}") ``` PiperOrigin-RevId: 524326722
- Loading branch information
1 parent
a1ba8d2
commit a05c57d
Showing
2 changed files
with
43 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters