Message404011
Pure Python class that has been registered as a `collections.abc.Sequence` can't be recgnized by the match statement without the `_abc` module.
For example:
```
>>> from test.support.import_helper import import_fresh_module
>>> collections_abc_with_c_abc = import_fresh_module(
... "collections.abc", fresh=["_collections_abc", "abc", "_abc"])
>>> class MyList:
... def __init__(self, iterable):
... self.list = list(iterable)
... def __len__(self):
... return len(self.list)
... def __getitem__(self, item):
... return self.list[item]
...
>>> collections_abc_with_c_abc.Sequence.register(MyList)
<class '__main__.MyList'>
>>> match MyList(range(3, 10)):
... case [x, *_]:
... print(x)
... case _:
... print("not a sequence")
...
3
>>> collections_abc_with_py_abc = import_fresh_module(
... "collections.abc", fresh=["_collections_abc", "abc"], blocked=["_abc"])
>>> class MyList:
... def __init__(self, iterable):
... self.list = list(iterable)
... def __len__(self):
... return len(self.list)
... def __getitem__(self, item):
... return self.list[item]
...
>>> collections_abc_with_py_abc.Sequence.register(MyList)
<class '__main__.MyList'>
>>> match MyList(range(3, 10)):
... case [x, *_]:
... print(x)
... case _:
... print("not a sequence")
...
not a sequence
```
It seems to be caused by https://github.com/python/cpython/commit/069e81ab3da46c441335ca762c4333b7bd91861d , only `tp_flags` are checked in the `MATCH_SEQUENCE` opcode. `Mapping` has the same problem. |
|
Date |
User |
Action |
Args |
2021-10-15 12:59:43 | GalaxySnail | set | recipients:
+ GalaxySnail |
2021-10-15 12:59:43 | GalaxySnail | set | messageid: <1634302783.77.0.196268254173.issue45483@roundup.psfhosted.org> |
2021-10-15 12:59:43 | GalaxySnail | link | issue45483 messages |
2021-10-15 12:59:43 | GalaxySnail | create | |
|