From 9a32a6d8e2cde19fcaea6895ff3bc28478b29e4d Mon Sep 17 00:00:00 2001 From: Alok Singh <8325708+alok@users.noreply.github.com> Date: Wed, 2 May 2018 18:40:13 -0700 Subject: [PATCH] Use set/dict literal syntax (#169) * Use set/dict literal syntax Every supported version can use this syntax, and it's both faster and clearer. * Fix indentation and drop unused argument The `op` arg isn't used for creating `out_names`, so we elide it. --- cloudpickle/cloudpickle.py | 3 +-- tests/cloudpickle_test.py | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/cloudpickle/cloudpickle.py b/cloudpickle/cloudpickle.py index 7eb4552df..61168175c 100644 --- a/cloudpickle/cloudpickle.py +++ b/cloudpickle/cloudpickle.py @@ -578,8 +578,7 @@ def extract_code_globals(cls, co): # PyPy "builtin-code" object out_names = set() else: - out_names = set(names[oparg] - for op, oparg in _walk_global_ops(co)) + out_names = {names[oparg] for _, oparg in _walk_global_ops(co)} # see if nested function have any global refs if co.co_consts: diff --git a/tests/cloudpickle_test.py b/tests/cloudpickle_test.py index 9991d98c7..2ca554442 100644 --- a/tests/cloudpickle_test.py +++ b/tests/cloudpickle_test.py @@ -222,7 +222,7 @@ def g(): def test_unhashable_closure(self): def f(): - s = set((1, 2)) # mutable set is unhashable + s = {1, 2} # mutable set is unhashable def g(): return len(s) @@ -494,7 +494,7 @@ def test_extended_arg(self): nvars = 65537 + 258 names = ['g%d' % i for i in range(1, nvars)] r = random.Random(42) - d = dict([(name, r.randrange(100)) for name in names]) + d = {name: r.randrange(100) for name in names} # def f(x): # x = g1, g2, ... # return zlib.crc32(bytes(bytearray(x))) @@ -693,7 +693,7 @@ def __init__(self, x): self.assertEqual(depickled3.x, 3) self.assertEqual(len(weakset), 2) - self.assertEqual(set(weakset), set([depickled1, depickled2])) + self.assertEqual(set(weakset), {depickled1, depickled2}) def test_faulty_module(self): for module_name in ['_faulty_module', '_missing_module', None]: