From 9e7979464982deebaf329cbc74ca6466f85fc2da Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen Date: Fri, 8 May 2020 20:12:11 +0200 Subject: [PATCH 1/4] Add a test for a Map kwarg named "col" --- tests/test_map.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_map.py b/tests/test_map.py index 5c4e1fb3c..ca7eecac9 100644 --- a/tests/test_map.py +++ b/tests/test_map.py @@ -1441,6 +1441,16 @@ class PyMapTest(BaseMapTest, unittest.TestCase): Map = PyMap + # TODO: + # 1. Make `test_kwarg_named_col` test pass. + # 2. Remove `test_kwarg_named_col` from `PyMapTest` and `CMapTest`. + # Move it to `BaseMapTest`. + @unittest.expectedFailure + def test_kwarg_named_col(self): + self.assertEqual(dict(self.Map(col=0)), {"col": 0}) + self.assertEqual(dict(self.Map(a=0, col=1)), {"a": 0, "col": 1}) + self.assertEqual(dict(self.Map({"a": 0}, col=1)), {"a": 0, "col": 1}) + try: from immutables._map import Map as CMap @@ -1453,6 +1463,11 @@ class CMapTest(BaseMapTest, unittest.TestCase): Map = CMap + def test_kwarg_named_col(self): + self.assertEqual(dict(self.Map(col=0)), {"col": 0}) + self.assertEqual(dict(self.Map(a=0, col=1)), {"a": 0, "col": 1}) + self.assertEqual(dict(self.Map({"a": 0}, col=1)), {"a": 0, "col": 1}) + if __name__ == "__main__": unittest.main() From 29b3c0da150c780ed293c8426dfef08f3dde5d30 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen Date: Sat, 9 May 2020 02:03:40 +0200 Subject: [PATCH 2/4] Fix edge case in Python implementation when a kwarg is named "col" --- immutables/map.py | 34 +++++++++++++++++++++++++++++++--- tests/test_map.py | 20 +++++--------------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/immutables/map.py b/immutables/map.py index 3ea4656b3..3d96aa3d3 100644 --- a/immutables/map.py +++ b/immutables/map.py @@ -433,7 +433,16 @@ def __iter__(self): class Map: - def __init__(self, col=None, **kw): + def __init__(self, *args, **kw): + if not args: + col = None + elif len(args) == 1: + col = args[0] + else: + raise TypeError( + "Map expected at most 1 argument, got {}".format(len(args)) + ) + self.__count = 0 self.__root = BitmapNode(0, 0, [], 0) self.__hash = -1 @@ -483,8 +492,18 @@ def __eq__(self, other): return True - def update(self, col=None, **kw): + def update(self, *args, **kw): + if not args: + col = None + elif len(args) == 1: + col = args[0] + else: + raise TypeError( + "update expected at most 1 argument, got {}".format(len(args)) + ) + it = None + if col is not None: if hasattr(col, 'items'): it = iter(col.items()) @@ -721,7 +740,16 @@ def __contains__(self, key): else: return True - def update(self, col=None, **kw): + def update(self, *args, **kw): + if not args: + col = None + elif len(args) == 1: + col = args[0] + else: + raise TypeError( + "update expected at most 1 argument, got {}".format(len(args)) + ) + if self.__mutid == 0: raise ValueError('mutation {!r} has been finished'.format(self)) diff --git a/tests/test_map.py b/tests/test_map.py index ca7eecac9..37b5b65e1 100644 --- a/tests/test_map.py +++ b/tests/test_map.py @@ -1436,22 +1436,17 @@ def test_map_pickle(self): def test_map_is_subscriptable(self): self.assertIs(self.Map[int, str], self.Map) - -class PyMapTest(BaseMapTest, unittest.TestCase): - - Map = PyMap - - # TODO: - # 1. Make `test_kwarg_named_col` test pass. - # 2. Remove `test_kwarg_named_col` from `PyMapTest` and `CMapTest`. - # Move it to `BaseMapTest`. - @unittest.expectedFailure def test_kwarg_named_col(self): self.assertEqual(dict(self.Map(col=0)), {"col": 0}) self.assertEqual(dict(self.Map(a=0, col=1)), {"a": 0, "col": 1}) self.assertEqual(dict(self.Map({"a": 0}, col=1)), {"a": 0, "col": 1}) +class PyMapTest(BaseMapTest, unittest.TestCase): + + Map = PyMap + + try: from immutables._map import Map as CMap except ImportError: @@ -1463,11 +1458,6 @@ class CMapTest(BaseMapTest, unittest.TestCase): Map = CMap - def test_kwarg_named_col(self): - self.assertEqual(dict(self.Map(col=0)), {"col": 0}) - self.assertEqual(dict(self.Map(a=0, col=1)), {"a": 0, "col": 1}) - self.assertEqual(dict(self.Map({"a": 0}, col=1)), {"a": 0, "col": 1}) - if __name__ == "__main__": unittest.main() From 286c0229ad720127fb604306c24a5ee42b920247 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen Date: Sun, 10 May 2020 23:27:25 +0200 Subject: [PATCH 3/4] Make error messages the same in Python and C --- immutables/map.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/immutables/map.py b/immutables/map.py index 3d96aa3d3..7096ce002 100644 --- a/immutables/map.py +++ b/immutables/map.py @@ -440,7 +440,7 @@ def __init__(self, *args, **kw): col = args[0] else: raise TypeError( - "Map expected at most 1 argument, got {}".format(len(args)) + "immutables.Map expected at most 1 arguments, got {}".format(len(args)) ) self.__count = 0 @@ -499,7 +499,7 @@ def update(self, *args, **kw): col = args[0] else: raise TypeError( - "update expected at most 1 argument, got {}".format(len(args)) + "update expected at most 1 arguments, got {}".format(len(args)) ) it = None @@ -747,7 +747,7 @@ def update(self, *args, **kw): col = args[0] else: raise TypeError( - "update expected at most 1 argument, got {}".format(len(args)) + "update expected at most 1 arguments, got {}".format(len(args)) ) if self.__mutid == 0: From 906940b6c12628cd54344a28ce5970a32066baa9 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen Date: Sun, 10 May 2020 23:39:00 +0200 Subject: [PATCH 4/4] Fix too long line --- immutables/map.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/immutables/map.py b/immutables/map.py index 7096ce002..8dcfee477 100644 --- a/immutables/map.py +++ b/immutables/map.py @@ -440,7 +440,8 @@ def __init__(self, *args, **kw): col = args[0] else: raise TypeError( - "immutables.Map expected at most 1 arguments, got {}".format(len(args)) + "immutables.Map expected at most 1 arguments, " + "got {}".format(len(args)) ) self.__count = 0