Skip to content

Commit b593f4f

Browse files
committed
__getstate__: preempt python3.6 Optional pickle err
1 parent bc1775d commit b593f4f

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

omegaconf/basecontainer.py

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
_is_missing_literal,
1515
_is_missing_value,
1616
_is_none,
17+
_is_union,
1718
_resolve_optional,
1819
get_ref_type,
1920
get_structured_config_data,
@@ -29,6 +30,7 @@
2930
from .base import Container, ContainerMetadata, DictKeyType, Node, SCMode
3031
from .errors import (
3132
ConfigCycleDetectedException,
33+
ConfigSerializationError,
3234
ConfigTypeError,
3335
InterpolationResolutionError,
3436
MissingMandatoryValue,
@@ -99,6 +101,12 @@ def __getstate__(self) -> Dict[str, Any]:
99101
dict_copy["_metadata"].ref_type = List
100102
else:
101103
assert False
104+
if sys.version_info < (3, 7):
105+
element_type = self._metadata.element_type
106+
if _is_union(element_type):
107+
raise ConfigSerializationError(
108+
"Serializing structured configs with `Union` element type requires python >= 3.7"
109+
)
102110
return dict_copy
103111

104112
# Support pickle

omegaconf/errors.py

+6
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,9 @@ class GrammarParseError(OmegaConfBaseException):
139139
"""
140140
Thrown when failing to parse an expression according to the ANTLR grammar.
141141
"""
142+
143+
144+
class ConfigSerializationError(OmegaConfBaseException):
145+
"""
146+
Thrown when an error occurs during serialization of an OmegaConf object.
147+
"""

tests/test_serialization.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from omegaconf import MISSING, DictConfig, ListConfig, OmegaConf
1515
from omegaconf._utils import get_ref_type
16-
from omegaconf.errors import OmegaConfBaseException
16+
from omegaconf.errors import ConfigSerializationError
1717
from tests import (
1818
Color,
1919
PersonA,
@@ -356,15 +356,12 @@ def test_pickle_backward_compatibility(version: str) -> None:
356356

357357

358358
@mark.skipif(sys.version_info >= (3, 7), reason="requires python3.6")
359-
@mark.xfail(reason="python3.6 pickling error is not handled")
360359
def test_python36_pickle_optional() -> None:
361360
cfg = OmegaConf.structured(SubscriptedDictOpt)
362361
with raises(
363-
OmegaConfBaseException,
362+
ConfigSerializationError,
364363
match=re.escape(
365-
"Serializing structured configs with `Union` type annotations requires python >= 3.7"
364+
"Serializing structured configs with `Union` element type requires python >= 3.7"
366365
),
367366
):
368-
# currently this raises the following:
369-
# _pickle.PicklingError: Can't pickle typing.Union[int, NoneType]: it's not the same object as typing.Union
370367
pickle.dumps(cfg)

0 commit comments

Comments
 (0)