Repository: mit-ll-responsible-ai/hydra-zen

Issue:
Add Python 3.11 support
For parity with Hydra 1.3.0: https://github.com/facebookresearch/hydra/releases/tag/v1.3.0


Discussion:

Resolving patch:
diff --git a/src/hydra_zen/structured_configs/_implementations.py b/src/hydra_zen/structured_configs/_implementations.py
--- a/src/hydra_zen/structured_configs/_implementations.py
+++ b/src/hydra_zen/structured_configs/_implementations.py
@@ -184,8 +184,11 @@ def mutable_value(x: _T, *, zen_convert: Optional[ZenConvert] = None) -> _T:
     cast = type(x)  # ensure that we return a copy of the default value
     settings = _utils.merge_settings(zen_convert, _BUILDS_CONVERT_SETTINGS)
     del zen_convert
-    x = sanitize_collection(x, convert_dataclass=settings["dataclass"])
-    return field(default_factory=lambda: cast(x))
+
+    if cast in {list, tuple, dict}:
+        x = sanitize_collection(x, convert_dataclass=settings["dataclass"])
+        return field(default_factory=lambda: cast(x))
+    return field(default_factory=lambda: x)
 
 
 @dataclass_transform()
@@ -797,6 +800,8 @@ def sanitized_field(
     if (
         type_value in _utils.KNOWN_MUTABLE_TYPES
         and type_value in HYDRA_SUPPORTED_PRIMITIVES
+    ) or (
+        is_dataclass(value) and not isinstance(value, type) and value.__hash__ is None
     ):
         if _mutable_default_permitted:
             return cast(
diff --git a/src/hydra_zen/structured_configs/_make_config.py b/src/hydra_zen/structured_configs/_make_config.py
--- a/src/hydra_zen/structured_configs/_make_config.py
+++ b/src/hydra_zen/structured_configs/_make_config.py
@@ -50,7 +50,7 @@ def __init__(self) -> None:
         raise TypeError("`NOTHING` cannot be instantiated")
 
 
-@dataclass
+@dataclass(unsafe_hash=True)
 class ZenField:
     """
     ZenField(hint=Any, default=<class 'NOTHING'>, name=<class 'NOTHING'>)


Test patch:
diff --git a/tests/custom_strategies.py b/tests/custom_strategies.py
--- a/tests/custom_strategies.py
+++ b/tests/custom_strategies.py
@@ -1,7 +1,6 @@
 # Copyright (c) 2022 Massachusetts Institute of Technology
 # SPDX-License-Identifier: MIT
 import string
-from dataclasses import dataclass
 from typing import (
     Any,
     Deque,
@@ -33,16 +32,6 @@ def _wrapper(obj):
     return obj
 
 
-@dataclass
-class BaseA:
-    ...
-
-
-@dataclass
-class BaseB:
-    ...
-
-
 # strategies for drawing valid inputs to `zen_wrappers`
 single_wrapper_strat = (
     st.just(_wrapper)
@@ -51,6 +40,18 @@ class BaseB:
 )
 wrapper_strat = single_wrapper_strat | st.lists(single_wrapper_strat)
 
+slots_strat = st.booleans()
+
+
+def _compat_slots(conf: Dict[str, Any]):
+    # dataclass has some hard rules about a frozen dataclass inheriting
+    # from a non-frozen one anf vice versa. Let's avoid this
+
+    if conf.get("weakref_slot", None) is True:
+        conf["slots"] = True
+    return conf
+
+
 st.register_type_strategy(
     DataclassOptions,
     st.fixed_dictionaries(
@@ -65,10 +66,10 @@ class BaseB:
             "frozen": st.booleans(),
             "match_args": st.booleans(),
             "kw_only": st.booleans(),
-            "slots": st.booleans(),
-            "weakref_slot": st.booleans(),
+            "slots": st.shared(st.booleans(), key="slot"),
+            "weakref_slot": st.shared(st.booleans(), key="slot"),
         },
-    ),
+    ).map(_compat_slots),
 )
 
 _valid_builds_strats = dict(
@@ -112,14 +113,18 @@ def valid_builds_args(*required: str, excluded: Sequence[str] = ()):
     assert _required <= set(_valid_builds_strats), _required - set(_valid_builds_strats)
     assert _excluded <= set(_valid_builds_strats), _excluded - set(_valid_builds_strats)
 
-    return st.fixed_dictionaries(
-        {k: _valid_builds_strats[k] for k in sorted(_required)},
-        optional={
-            k: v
-            for k, v in _valid_builds_strats.items()
-            if k not in _excluded and k not in _required
-        },
-    ).map(_compat_frozen)
+    return (
+        st.fixed_dictionaries(
+            {k: _valid_builds_strats[k] for k in sorted(_required)},
+            optional={
+                k: v
+                for k, v in _valid_builds_strats.items()
+                if k not in _excluded and k not in _required
+            },
+        )
+        .map(_compat_frozen)
+        .map(_compat_slots)
+    )
 
 
 @st.composite
diff --git a/tests/test_config_value_validation.py b/tests/test_config_value_validation.py
--- a/tests/test_config_value_validation.py
+++ b/tests/test_config_value_validation.py
@@ -6,7 +6,7 @@
 from typing import Any
 
 import pytest
-from hypothesis import HealthCheck, assume, example, given, settings
+from hypothesis import HealthCheck, assume, example, given, note, settings
 from omegaconf import DictConfig, ListConfig, OmegaConf
 from omegaconf.errors import KeyValidationError
 
@@ -21,7 +21,6 @@
 from hydra_zen._compatibility import ZEN_SUPPORTED_PRIMITIVES
 from hydra_zen.errors import HydraZenUnsupportedPrimitiveError
 from hydra_zen.structured_configs._implementations import HYDRA_SUPPORTED_PRIMITIVES
-from hydra_zen.structured_configs._utils import KNOWN_MUTABLE_TYPES
 from tests import everything_except
 
 
@@ -34,7 +33,7 @@ def f_with_kwargs(*args, **kwargs):
 
 
 def make_hydrated_dataclass(target, a):
-    assume(not isinstance(a, tuple(KNOWN_MUTABLE_TYPES)))
+    assume(a.__hash__ is not None)
 
     @hydrated_dataclass(target)
     class Config:
@@ -44,7 +43,7 @@ class Config:
 
 
 def make_dataclass(a):
-    assume(not isinstance(a, tuple(KNOWN_MUTABLE_TYPES)))
+    assume(a.__hash__ is not None)
 
     @dataclass
     class C:
@@ -135,6 +134,7 @@ def test_omegaconf_doesnt_permit_dataclasses_as_dict_keys():
 def test_unsupported_config_value_raises_while_making_config(
     config_construction_fn, unsupported
 ):
+    note(f"unsupported: {unsupported}")
     with pytest.raises((HydraZenUnsupportedPrimitiveError, ModuleNotFoundError)):
         config_construction_fn(unsupported)
 
diff --git a/tests/test_dataclass_conversion.py b/tests/test_dataclass_conversion.py
--- a/tests/test_dataclass_conversion.py
+++ b/tests/test_dataclass_conversion.py
@@ -1,6 +1,6 @@
 # Copyright (c) 2022 Massachusetts Institute of Technology
 # SPDX-License-Identifier: MIT
-from dataclasses import InitVar, dataclass, field
+from dataclasses import MISSING, InitVar, dataclass, field
 from pathlib import Path
 from typing import Any, Callable, List, Type, TypeVar
 
@@ -18,6 +18,7 @@
     just,
     make_config,
     make_custom_builds_fn,
+    mutable_value,
 )
 from hydra_zen.errors import HydraZenUnsupportedPrimitiveError
 from hydra_zen.structured_configs._type_guards import is_builds
@@ -53,7 +54,7 @@ def baz(f: float) -> float:
 class Stuff:
     field1: Interface1
     field2: Interface2
-    field3: Nested = Nested(baz)
+    field3: Nested = mutable_value(Nested(baz))
     field4: Any = field(default_factory=lambda: [1, 2, 3])
 
 
@@ -205,6 +206,11 @@ def test_builds_with_initvar_field(dataclass_type):
     ) == dataclass_type(x=1)
 
 
+def _zen_get(obj):
+    x = obj.default.default
+    return x if x is not MISSING else obj.default.default_factory()
+
+
 @pytest.mark.parametrize(
     "dataclass_obj",
     [
@@ -231,9 +237,9 @@ def test_builds_with_initvar_field(dataclass_type):
             dict, x=x
         )().x,
         lambda x: just(x, zen_convert={"dataclass": False}),
-        lambda x: ZenField(
-            default=x, name="x", zen_convert={"dataclass": False}
-        ).default.default,  # type: ignore
+        lambda x: _zen_get(
+            ZenField(default=x, name="x", zen_convert={"dataclass": False})
+        ),
     ],
 )
 def test_no_dataclass_conversion(
@@ -264,10 +270,12 @@ def test_no_dataclass_conversion(
         lambda x: make_custom_builds_fn(zen_convert={"dataclass": True})(dict, x=x)().x,
         lambda x: builds(dict, x=x)().x,
         lambda x: just(x),
-        lambda x: ZenField(
-            default=x,
-            name="x",
-        ).default.default,  # type: ignore
+        lambda x: _zen_get(
+            ZenField(
+                default=x,
+                name="x",
+            )
+        ),
     ],
 )
 def test_yes_dataclass_conversion(
diff --git a/tests/test_py310.py b/tests/test_py310.py
--- a/tests/test_py310.py
+++ b/tests/test_py310.py
@@ -60,9 +60,6 @@ def func(x):
 @pytest.mark.filterwarnings("ignore:A structured config was supplied for")
 @given(kw=valid_builds_args(), as_inst=..., x=st.sampled_from(["", 1, [1, 2]]))
 def test_equiv_getattr(kw, as_inst: bool, x):
-    conf_fn = (
-        lambda **k: builds(func, x=x, **k) if x else lambda **k: mak(func, x=x, **k)
-    )
     obj = builds(func, x=x, **kw)
 
     if "zen_dataclass" in kw and kw["zen_dataclass"] is not None:
@@ -73,6 +70,7 @@ def test_equiv_getattr(kw, as_inst: bool, x):
             assume(False)
 
         kw["zen_dataclass"]["slots"] = False
+        kw["zen_dataclass"]["weakref_slot"] = False
 
     no_slot_obj = builds(func, x=x, **kw)
 
diff --git a/tests/test_value_conversion.py b/tests/test_value_conversion.py
--- a/tests/test_value_conversion.py
+++ b/tests/test_value_conversion.py
@@ -310,10 +310,10 @@ def test_zen_supported_primitives_arent_supported_by_hydra(type_, data: st.DataO
 
     @dataclass
     class C:
-        x: type_ = mutable_value(value) if isinstance(value, (set, dict)) else value
+        x: type_ = mutable_value(value) if value.__hash__ is None else value
 
     with pytest.raises((ValidationError, AssertionError)):
-        Conf = OmegaConf.create(C)
+        Conf = OmegaConf.create(C)  # type: ignore
         assert isinstance(Conf.x, type_)