Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable support for pattern matching #96

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +3418 to +3420
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I am not a C developer. I noticed errors for this name not being available on <3.10, and scrambled this together. Please let me know if this is not how things are done, or if you think the formatting looks off.

,
.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()