Skip to content

Commit

Permalink
Fix unpickling of Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Dec 10, 2023
1 parent 33c806b commit 680cb61
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [1.5.4](https://github.com/sdss/sdsstools/compare/1.5.3...1.5.4) - 2023-12-10

- Fix unpickling of `Configuration` instances. This only seems relevant when trying to pass a `Configuration` object to a `multiprocessing` callback.


## [1.5.3](https://github.com/sdss/sdsstools/compare/1.5.2...1.5.3) - 2023-12-08

- Vendorise `pydl`'s `yanny` module which can be accessed as `sdsstools.yanny`.
Expand Down
15 changes: 11 additions & 4 deletions src/sdsstools/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,22 @@ def __init__(

def __getitem__(self, __key: str) -> Any:
if self.strict_mode:
return super().__getitem__(__key)
return dict.__getitem__(self, __key)

return self.get(__key)

def __setitem__(self, __key: str, __value: Any) -> None:
if isinstance(__value, dict) and self.propagate_type:
__value = self.__class__(__value, strict_mode=self.strict_mode)
# We use getattr to give default values to the non-default dict attributes.
# This only seems to matter when pickling/unpickling, and at the end of
# unpickling the values are set correctly anyway.

return super().__setitem__(__key, __value)
if isinstance(__value, dict) and getattr(self, "propagate_type", True):
__value = self.__class__(
__value,
strict_mode=getattr(self, "strict_mode", False),
)

return dict.__setitem__(self, __key, __value)

def get(self, __key: str, default: Any = None, strict: bool | None = None) -> Any:
if (strict is None and self.strict_mode is True) or strict is True:
Expand Down
12 changes: 12 additions & 0 deletions test/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import copy
import inspect
import io
import multiprocessing
import os
import unittest.mock

Expand Down Expand Up @@ -379,3 +380,14 @@ def test_configuration_assignment_dot():
conf["cat1.key1"] = {"subkey2": 2}
assert len(conf) == 2
assert conf["cat1.key1"] == {"subkey2": 2}


def _process(config):
assert config["cat1"]["key1"] == "another_value"


def test_configuration_with_multiprocessing(config_file):
config = Configuration(config_file)
config.propagate_type = False
with multiprocessing.Pool(2) as pool:
pool.apply(_process, (config,))

0 comments on commit 680cb61

Please sign in to comment.