Skip to content

Commit 7071a24

Browse files
authored
Enable support for pattern matching (#96)
1 parent f3c0451 commit 7071a24

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

immutables/_map.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3414,7 +3414,11 @@ PyTypeObject _Map_Type = {
34143414
.tp_iter = (getiterfunc)map_tp_iter,
34153415
.tp_dealloc = (destructor)map_tp_dealloc,
34163416
.tp_getattro = PyObject_GenericGetAttr,
3417-
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3417+
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
3418+
#ifdef Py_TPFLAGS_MAPPING
3419+
| Py_TPFLAGS_MAPPING
3420+
#endif
3421+
,
34183422
.tp_richcompare = map_tp_richcompare,
34193423
.tp_traverse = (traverseproc)map_tp_traverse,
34203424
.tp_clear = (inquiry)map_tp_clear,

tests/conftest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
# We need the mypy pytest plugin to do the test collection for our
23
# typing tests.
34

@@ -11,3 +12,6 @@
1112
pytest_plugins = [
1213
'mypy.test.data',
1314
]
15+
16+
if sys.version_info < (3, 10):
17+
collect_ignore = ["test_pattern_matching.py"]

tests/test_pattern_matching.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import unittest
2+
3+
from immutables.map import Map as PyMap
4+
5+
6+
class BaseMapTest:
7+
8+
Map = None
9+
10+
def test_map_can_be_matched(self):
11+
match self.Map(a=1, b=2): # noqa: E999
12+
case {"a": 1 as matched}:
13+
matched = matched
14+
case _:
15+
assert False
16+
17+
assert matched == 1
18+
19+
20+
class PyMapTest(BaseMapTest, unittest.TestCase):
21+
22+
Map = PyMap
23+
24+
25+
try:
26+
from immutables._map import Map as CMap
27+
except ImportError:
28+
CMap = None
29+
30+
31+
@unittest.skipIf(CMap is None, 'C Map is not available')
32+
class CMapTest(BaseMapTest, unittest.TestCase):
33+
34+
Map = CMap
35+
36+
37+
if __name__ == "__main__":
38+
unittest.main()

0 commit comments

Comments
 (0)