diff --git a/medcat/storage/serialisers.py b/medcat/storage/serialisers.py index 532d7a33..03077e00 100644 --- a/medcat/storage/serialisers.py +++ b/medcat/storage/serialisers.py @@ -13,6 +13,8 @@ from medcat.storage.serialisables import get_all_serialisable_members from medcat.storage.schema import load_schema, save_schema from medcat.storage.schema import DEFAULT_SCHEMA_FILE, IllegalSchemaException +from medcat.utils.legacy.v2_beta import ( + fix_module_and_cls_name, RemappingUnpickler) logger = logging.getLogger(__name__) @@ -146,6 +148,7 @@ def deserialise_manually(cls, folder_path: str, man_cls_path: str, **init_kwargs) -> Serialisable: logger.info("Deserialising manually based on %s", man_cls_path) module_name, cls_name = man_cls_path.rsplit(".", 1) + module_name, cls_name = fix_module_and_cls_name(module_name, cls_name) rel_module = importlib.import_module(module_name) man_cls = getattr(rel_module, cls_name) if not issubclass(man_cls, ManualSerialisable): @@ -253,7 +256,7 @@ def serialise(self, raw_parts: dict[str, Any], target_file: str) -> None: def deserialise(self, target_file: str) -> dict[str, Any]: with open(target_file, 'rb') as f: - return _dill.load(f) + return RemappingUnpickler(f).load() _DEF_SER = AvailableSerialisers.dill diff --git a/medcat/utils/legacy/v2_beta.py b/medcat/utils/legacy/v2_beta.py new file mode 100644 index 00000000..04d2489c --- /dev/null +++ b/medcat/utils/legacy/v2_beta.py @@ -0,0 +1,16 @@ +import dill + + +class RemappingUnpickler(dill.Unpickler): + # TODO: remove at some point - this only serves to load beta models + def find_class(self, module: str, name: str): + # Remap old module path to new one + module, name = fix_module_and_cls_name(module, name) + return super().find_class(module, name) + + +def fix_module_and_cls_name(module_name: str, cls_name: str + ) -> tuple[str, str]: + if module_name.startswith("medcat2"): + module_name = module_name.replace("medcat2", "medcat", 1) + return module_name, cls_name diff --git a/tests/backwards_compatibility/check_backwards_compatibility.sh b/tests/backwards_compatibility/check_backwards_compatibility.sh index 02f9334c..b4bd006c 100644 --- a/tests/backwards_compatibility/check_backwards_compatibility.sh +++ b/tests/backwards_compatibility/check_backwards_compatibility.sh @@ -1,3 +1,5 @@ +# fail upon individual failure +set -euo pipefail # CONSTANTs/ shouldn't change REGRESSION_MODULE="medcat.utils.regression.regression_checker" REGRESSION_OPTIONS="--strictness STRICTEST --require-fully-correct"