This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author GalaxySnail
Recipients GalaxySnail
Date 2021-10-15.12:59:43
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1634302783.77.0.196268254173.issue45483@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2021-10-15 12:59:43GalaxySnailsetrecipients: + GalaxySnail
2021-10-15 12:59:43GalaxySnailsetmessageid: <1634302783.77.0.196268254173.issue45483@roundup.psfhosted.org>
2021-10-15 12:59:43GalaxySnaillinkissue45483 messages
2021-10-15 12:59:43GalaxySnailcreate