Skip to content

Commit

Permalink
Enable support for pattern matching (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonagestam authored Nov 18, 2022
1 parent f3c0451 commit 7071a24
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
6 changes: 5 additions & 1 deletion immutables/_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -3414,7 +3414,11 @@ PyTypeObject _Map_Type = {
.tp_iter = (getiterfunc)map_tp_iter,
.tp_dealloc = (destructor)map_tp_dealloc,
.tp_getattro = PyObject_GenericGetAttr,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
#ifdef Py_TPFLAGS_MAPPING
| Py_TPFLAGS_MAPPING
#endif
,
.tp_richcompare = map_tp_richcompare,
.tp_traverse = (traverseproc)map_tp_traverse,
.tp_clear = (inquiry)map_tp_clear,
Expand Down
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
# We need the mypy pytest plugin to do the test collection for our
# typing tests.

Expand All @@ -11,3 +12,6 @@
pytest_plugins = [
'mypy.test.data',
]

if sys.version_info < (3, 10):
collect_ignore = ["test_pattern_matching.py"]
38 changes: 38 additions & 0 deletions tests/test_pattern_matching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import unittest

from immutables.map import Map as PyMap


class BaseMapTest:

Map = None

def test_map_can_be_matched(self):
match self.Map(a=1, b=2): # noqa: E999
case {"a": 1 as matched}:
matched = matched
case _:
assert False

assert matched == 1


class PyMapTest(BaseMapTest, unittest.TestCase):

Map = PyMap


try:
from immutables._map import Map as CMap
except ImportError:
CMap = None


@unittest.skipIf(CMap is None, 'C Map is not available')
class CMapTest(BaseMapTest, unittest.TestCase):

Map = CMap


if __name__ == "__main__":
unittest.main()

0 comments on commit 7071a24

Please sign in to comment.