Skip to content

Commit

Permalink
only test cloudpickle and dill if installed
Browse files Browse the repository at this point in the history
  • Loading branch information
basnijholt committed Apr 10, 2020
1 parent bc587c4 commit 303efaa
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
34 changes: 27 additions & 7 deletions adaptive/tests/test_pickling.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import pickle
import random

import cloudpickle
import dill
import pytest

from adaptive.learner import (
Expand All @@ -18,6 +16,20 @@
)
from adaptive.runner import simple

try:
import cloudpickle

with_cloudpickle = True
except ModuleNotFoundError:
with_cloudpickle = False

try:
import dill

with_dill = True
except ModuleNotFoundError:
with_dill = False


def goal_1(learner):
return learner.npoints >= 10
Expand All @@ -36,7 +48,11 @@ def goal_2(learner):
(AverageLearner, dict(atol=0.1)),
]

serializers = (pickle, dill, cloudpickle)
serializers = [pickle]
if with_cloudpickle:
serializers.append(cloudpickle)
if with_dill:
serializers.append(dill)

learners = [
(learner_type, learner_kwargs, serializer)
Expand All @@ -45,7 +61,7 @@ def goal_2(learner):
]


def f_for_pickle_balancing_learner(x):
def f_for_pickle(x):
return 1


Expand All @@ -62,17 +78,21 @@ def test_serialization_for(learner_type, learner_kwargs, serializer):
def f(x):
return random.random()

if serializer is pickle:
# f from the local scope cannot be pickled
f = f_for_pickle # noqa: F811

learner = learner_type(f, **learner_kwargs)

simple(learner, goal_1)
learner_bytes = cloudpickle.dumps(learner)
learner_bytes = serializer.dumps(learner)

if serializer is not pickle:
# With pickle the functions are only pickled by reference
del f
del learner

learner_loaded = cloudpickle.loads(learner_bytes)
learner_loaded = serializer.loads(learner_bytes)
assert learner_loaded.npoints >= 10
simple(learner_loaded, goal_2)
assert learner_loaded.npoints >= 20
Expand Down Expand Up @@ -116,7 +136,7 @@ def f(x):

if serializer is pickle:
# f from the local scope cannot be pickled
f = f_for_pickle_balancing_learner # noqa: F811
f = f_for_pickle # noqa: F811

learner_1 = Learner1D(f, bounds=(-1, 1))
learner_2 = Learner1D(f, bounds=(-2, 2))
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ def get_version_and_cmdclass(package_name):
"plotly",
],
"testing": [
"cloudpickle",
"dill",
"flaky",
"pytest",
"pytest-cov",
Expand All @@ -53,6 +51,8 @@ def get_version_and_cmdclass(package_name):
"pre_commit",
],
"other": [
"cloudpickle",
"dill",
"distributed",
"ipyparallel",
"loky",
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ include_trailing_comma=True
force_grid_wrap=0
use_parentheses=True
line_length=88
known_third_party=PIL,atomicwrites,cloudpickle,dill,flaky,holoviews,matplotlib,nbconvert,numpy,pytest,scipy,setuptools,skopt,sortedcollections,sortedcontainers
known_third_party=PIL,atomicwrites,flaky,holoviews,matplotlib,nbconvert,numpy,pytest,scipy,setuptools,skopt,sortedcollections,sortedcontainers

0 comments on commit 303efaa

Please sign in to comment.