1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
"""Limited, best-effort test suite regarding cloudpickle backward-compat.
Cloudpickle does not officially support reading pickles files
generated with an older version of cloudpickle than the one used to read the
said pickles. However, this policy is not widely known among users that use
libraries that rely on cloudpickle such as mlflow, and is subject to confusion.
As a compromise, this script make sure cloudpickle is backward compatible for a
few canonical use cases. Cloudpicke backward-compatitibility support remains a
best-effort initiative.
"""
import pickle
import pytest
from .generate_old_pickles import PICKLE_DIRECTORY
def load_obj(filename, check_deprecation_warning="auto"):
if check_deprecation_warning == "auto":
check_deprecation_warning = False
pickle_filepath = PICKLE_DIRECTORY / filename
if not pickle_filepath.exists():
pytest.skip(f"Could not find {str(pickle_filepath)}")
with open(str(pickle_filepath), "rb") as f:
if check_deprecation_warning:
msg = "A pickle file created using an old"
with pytest.warns(UserWarning, match=msg):
obj = pickle.load(f)
else:
obj = pickle.load(f)
return obj
def test_simple_func():
f = load_obj("simple_func.pkl")
assert f(1) == 2
assert f(1, 1) == 2
assert f(2, 2) == 4
def test_simple_class():
SimpleClass = load_obj("simple_class.pkl")
c = SimpleClass(1)
assert hasattr(c, "attribute")
assert c.attribute == 1
# test class tracking feature
assert SimpleClass is load_obj("simple_class.pkl")
def test_dynamic_module():
mod = load_obj("simple_module.pkl")
assert hasattr(mod, "f")
assert mod.f(1) == 2
assert mod.f(1, 1) == 2
assert mod.f(2, 2) == 4
def test_simple_enum():
enum = load_obj("simple_enum.pkl", check_deprecation_warning=False)
assert hasattr(enum, "RED")
assert enum.RED == 1
assert enum.BLUE == 2
# test enum tracking feature
new_enum = load_obj("simple_enum.pkl", check_deprecation_warning=False)
assert new_enum is enum
def test_complex_class():
SimpleClass = load_obj("class_with_type_hints.pkl")
c = SimpleClass(1)
assert hasattr(c, "attribute")
assert c.attribute == 1
# test class tracking feature
assert SimpleClass is load_obj("class_with_type_hints.pkl")
def test_complex_function():
MyClass, f = load_obj("function_with_type_hints.pkl")
assert len(f.__annotations__) > 0
a = MyClass(1)
b = MyClass(2)
c = f(a, b)
assert c.attribute == 3
def test_nested_function():
f = load_obj("nested_function.pkl")
assert f(41) == 42
|