Skip to content

Commit

Permalink
Add failing test based on omry#830
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasha10 committed Dec 2, 2021
1 parent 45ff30b commit 197129e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
10 changes: 10 additions & 0 deletions tests/structured_conf/data/attr_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,3 +592,13 @@ class ParentContainers:
class ChildContainers(ParentContainers):
list1: List[int] = [1, 2, 3]
dict: Dict[str, Any] = {"a": 5, "b": 6}

@attr.s(auto_attribs=True)
class ParentNoDefaultFactory:
no_default_to_list: Any
int_to_list: Any = 1

@attr.s(auto_attribs=True)
class ChildWithDefaultFactory(ParentNoDefaultFactory):
no_default_to_list: Any = ["hi"]
int_to_list: Any = ["hi"]
10 changes: 10 additions & 0 deletions tests/structured_conf/data/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,13 @@ class ParentContainers:
class ChildContainers(ParentContainers):
list1: List[int] = field(default_factory=lambda: [1, 2, 3])
dict: Dict[str, Any] = field(default_factory=lambda: {"a": 5, "b": 6})

@dataclass
class ParentNoDefaultFactory:
no_default_to_list: Any
int_to_list: Any = 1

@dataclass
class ChildWithDefaultFactory(ParentNoDefaultFactory):
no_default_to_list: Any = field(default_factory=lambda: ["hi"])
int_to_list: Any = field(default_factory=lambda: ["hi"])
20 changes: 19 additions & 1 deletion tests/structured_conf/test_structured_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
from importlib import import_module
from typing import Any, Dict, List, Optional
from typing import Any, Callable, Dict, List, Optional

from pytest import fixture, mark, param, raises

Expand Down Expand Up @@ -1228,3 +1228,21 @@ def test_container_inheritance(self, module: Any) -> None:

assert OmegaConf.is_missing(parent, "dict")
assert child.dict == {"a": 5, "b": 6}

@mark.parametrize(
"create_fn",
[
param(lambda cls: OmegaConf.structured(cls), id="create_from_class"),
param(lambda cls: OmegaConf.structured(cls()), id="create_from_instance"),
],
)
def test_subclass_using_default_factory(
self, module: Any, create_fn: Callable[[Any], DictConfig]
) -> None:
"""
When a structured config class field has a default and a subclass defines a default_factory for the same field,
ensure that the DictConfig object created from the subclass uses the default_factory.
"""
cfg = create_fn(module.StructuredSubclass.ChildWithDefaultFactory)
assert cfg.no_default_to_list == ["hi"]
assert cfg.int_to_list == ["hi"]

0 comments on commit 197129e

Please sign in to comment.