Skip to content

Commit

Permalink
Add __getstate__() and __setstate__() methods
Browse files Browse the repository at this point in the history
Fixes pickling of Options/MutableOptions instances with dill.
  • Loading branch information
johnomotani committed Jun 14, 2022
1 parent c9b37f2 commit 2286efa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,9 @@ with the documentation for each option:

### `value_type`

The value_type argument can be used to give a type or sequence of types that the option
is allowed to have. Trying to set an option with a non-allowed type raises a
The `value_type` argument can be used to give a type or sequence of types that
the option is allowed to have. Trying to set an option with a non-allowed type
raises a
`ValueError`:
```python
>>> d2 = D(d=-2)
Expand Down Expand Up @@ -387,6 +388,12 @@ or all values including defaults, by passing `True` to the `with_defaults` argum
```
### Pickling (with dill)
`Options` objects can be pickled using `dill`. This is tested. Pickling of
`MutableOptions` objects is not currently supported.
Examples
--------
Expand Down
8 changes: 8 additions & 0 deletions optionsfactory/optionsfactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,14 @@ def __setattr__(self, key, value):
raise TypeError("Options does not allow assigning to attributes")
super(OptionsFactory.Options, self).__setattr__(key, value)

def __getstate__(self):
# Need to define this so that pickling with dill works
return vars(self)

def __setstate__(self, state):
# Need to define this so that pickling with dill works
vars(self).update(state)

def is_default(self, key):
try:
return self.__is_default[key]
Expand Down

0 comments on commit 2286efa

Please sign in to comment.