Skip to content

Commit

Permalink
Fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ogrisel committed Oct 10, 2023
1 parent 4668392 commit adbe0d3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
1 change: 0 additions & 1 deletion cloudpickle/cloudpickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import uuid
import threading
import typing
import warnings

from .compat import pickle
from collections import OrderedDict
Expand Down
56 changes: 34 additions & 22 deletions tests/cloudpickle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ def test_lambda(self):

def test_nested_lambdas(self):
a, b = 1, 2
f1 = lambda x: x + a
f2 = lambda x: f1(x) // b
f1 = lambda x: x + a # noqa: E731
f2 = lambda x: f1(x) // b # noqa: E731
self.assertEqual(pickle_depickle(f2, protocol=self.protocol)(1), 1)

def test_recursive_closure(self):
Expand Down Expand Up @@ -397,10 +397,12 @@ def some_function(x, y):
return (x + y) / LOCAL_CONSTANT

# pickle the function definition
self.assertEqual(pickle_depickle(some_function, protocol=self.protocol)(41, 1), 1)
self.assertEqual(pickle_depickle(some_function, protocol=self.protocol)(81, 3), 2)
result = pickle_depickle(some_function, protocol=self.protocol)(41, 1)
assert result == 1
result = pickle_depickle(some_function, protocol=self.protocol)(81, 3)
assert result == 2

hidden_constant = lambda: LOCAL_CONSTANT
hidden_constant = lambda: LOCAL_CONSTANT # noqa: E731

class SomeClass:
"""Overly complicated class with nested references to symbols"""
Expand Down Expand Up @@ -480,7 +482,7 @@ def some_generator(cnt):

gen2 = pickle_depickle(some_generator, protocol=self.protocol)

assert type(gen2(3)) == type(some_generator(3))
assert isinstance(gen2(3), type(some_generator(3)))
assert list(gen2(3)) == list(range(3))

def test_classmethod(self):
Expand Down Expand Up @@ -1006,16 +1008,16 @@ def f(x, y):

@gen.coroutine
def g(y):
res = yield f(0.01, y)
res = yield f(0.01, y) # noqa: F821
raise gen.Return(res + 1)

data = cloudpickle.dumps([g, g], protocol=self.protocol)
f = g = None
del f, g
g2, g3 = pickle.loads(data)
self.assertTrue(g2 is g3)
assert g2 is g3
loop = ioloop.IOLoop.current()
res = loop.run_sync(functools.partial(g2, 5))
self.assertEqual(res, 7)
assert res == 7

@pytest.mark.skipif(
(3, 11, 0, 'beta') <= sys.version_info < (3, 11, 0, 'beta', 4),
Expand Down Expand Up @@ -1052,12 +1054,14 @@ def test_submodule(self):

# Choose any module NOT imported by __init__ of its parent package
# examples in standard library include:
# - http.cookies, unittest.mock, curses.textpad, xml.etree.ElementTree

global xml # imitate performing this import at top of file
# http.cookies, unittest.mock, curses.textpad, xml.etree.ElementTree
import xml
import xml.etree.ElementTree

def example():
x = xml.etree.ElementTree.Comment # potential AttributeError
_ = xml.etree.ElementTree.Comment # noqa: F821

example() # smoke test

s = cloudpickle.dumps(example, protocol=self.protocol)

Expand All @@ -1069,16 +1073,19 @@ def example():

# deserialise
f = pickle.loads(s)
f() # perform test for error
f() # smoke test

def test_submodule_closure(self):
# Same as test_submodule except the package is not a global
# Same as test_submodule except the xml package has not been imported
def scope():
import xml.etree.ElementTree

def example():
x = xml.etree.ElementTree.Comment # potential AttributeError
_ = xml.etree.ElementTree.Comment # potential AttributeError
return example

example = scope()
example() # smoke test

s = cloudpickle.dumps(example, protocol=self.protocol)

Expand All @@ -1088,13 +1095,13 @@ def example():
del sys.modules[item]

f = cloudpickle.loads(s)
f() # test
f() # smoke test

def test_multiprocess(self):
# running a function pickled by another process (a la dask.distributed)
def scope():
def example():
x = xml.etree.ElementTree.Comment
_ = xml.etree.ElementTree.Comment
return example
global xml
import xml.etree.ElementTree
Expand All @@ -1115,11 +1122,13 @@ def test_import(self):
# like test_multiprocess except subpackage modules referenced directly
# (unlike test_submodule)
global etree

def scope():
import xml.etree as foobar

def example():
x = etree.Comment
x = foobar.ElementTree
_ = etree.Comment
_ = foobar.ElementTree
return example
example = scope()
import xml.etree.ElementTree as etree
Expand All @@ -1135,7 +1144,10 @@ def example():

def test_multiprocessing_lock_raises(self):
lock = multiprocessing.Lock()
with pytest.raises(RuntimeError, match="only be shared between processes through inheritance"):
with pytest.raises(
RuntimeError,
match="only be shared between processes through inheritance"
):
cloudpickle.dumps(lock)

def test_cell_manipulation(self):
Expand Down

0 comments on commit adbe0d3

Please sign in to comment.