Download tests/structured_conf/data/attr_classes.py from soiz1/omegaconf: direct link, hf CLI and curl.
- Browser
- Download file 10.6 kB
-
https://huggingface.co/datasets/soiz1/omegaconf/resolve/main/tests/structured_conf/data/attr_classes.py
- Command line
-
hf download hf://datasets/soiz1/omegaconf/tests/structured_conf/data/attr_classes.py
-
curl -L -o attr_classes.py https://huggingface.co/datasets/soiz1/omegaconf/resolve/main/tests/structured_conf/data/attr_classes.py
10.6 kB
| from typing import Any, Dict, List, Optional, Tuple, Union | |
| import attr | |
| import pytest | |
| from omegaconf import II, MISSING, SI | |
| from tests import Color | |
| # attr is a dependency of pytest which means it's always available when testing with pytest. | |
| pytest.importorskip("attr") | |
| class NotStructuredConfig: | |
| name: str = "Bond" | |
| age: int = 7 | |
| class StructuredWithInvalidField: | |
| bar: NotStructuredConfig = NotStructuredConfig() | |
| class User: | |
| name: str = MISSING | |
| age: int = MISSING | |
| class UserList: | |
| list: List[User] = MISSING | |
| class UserDict: | |
| dict: Dict[str, User] = MISSING | |
| class AnyTypeConfig: | |
| with_default: Any = "Can get any type at runtime" | |
| null_default: Any = None | |
| # Access to this prior to assigning a value to it will result in | |
| # a MissingMandatoryValue exception. | |
| # Equivalent to "???" in YAML files | |
| mandatory_missing: Any = MISSING | |
| # interpolation, will inherit the type and value of `with_default' | |
| interpolation: Any = II("with_default") | |
| # specific types assigned | |
| int_default: Any = 12 | |
| float_default: Any = 10.0 | |
| str_default: Any = "foobar" | |
| bool_default: Any = True | |
| enum_default: Any = Color.BLUE | |
| # test mixing with variable with a specific type annotation | |
| typed_int_default: int = 10 | |
| class BoolConfig: | |
| # with default value | |
| with_default: bool = True | |
| # default is None | |
| null_default: Optional[bool] = None | |
| # explicit no default | |
| mandatory_missing: bool = MISSING | |
| # interpolation, will inherit the type and value of `with_default' | |
| interpolation: bool = II("with_default") | |
| class IntegersConfig: | |
| # with default value | |
| with_default: int = 10 | |
| # default is None | |
| null_default: Optional[int] = None | |
| # explicit no default | |
| mandatory_missing: int = MISSING | |
| # interpolation, will inherit the type and value of `with_default' | |
| interpolation: int = II("with_default") | |
| class StringConfig: | |
| # with default value | |
| with_default: str = "foo" | |
| # default is None | |
| null_default: Optional[str] = None | |
| # explicit no default | |
| mandatory_missing: str = MISSING | |
| # interpolation, will inherit the type and value of `with_default' | |
| interpolation: str = II("with_default") | |
| class FloatConfig: | |
| # with default value | |
| with_default: float = 0.10 | |
| # default is None | |
| null_default: Optional[float] = None | |
| # explicit no default | |
| mandatory_missing: float = MISSING | |
| # interpolation, will inherit the type and value of `with_default' | |
| interpolation: float = II("with_default") | |
| class EnumConfig: | |
| # with default value | |
| with_default: Color = Color.BLUE | |
| # default is None | |
| null_default: Optional[Color] = None | |
| # explicit no default | |
| mandatory_missing: Color = MISSING | |
| # interpolation, will inherit the type and value of `with_default' | |
| interpolation: Color = II("with_default") | |
| class ConfigWithList: | |
| list1: List[int] = [1, 2, 3] | |
| list2: Tuple[int, int, int] = (1, 2, 3) | |
| missing: List[int] = MISSING | |
| class ConfigWithDict: | |
| dict1: Dict[str, Any] = {"foo": "bar"} | |
| missing: Dict[str, Any] = MISSING | |
| class ConfigWithDict2: | |
| dict1: Dict[str, int] = {"foo": 2} | |
| class Nested: | |
| # with default value | |
| with_default: int = 10 | |
| # default is None | |
| null_default: Optional[int] = None | |
| # explicit no default | |
| mandatory_missing: int = MISSING | |
| # Note that since relative interpolations are not yet supported, | |
| # Nested configs and interpolations does not play too well together | |
| interpolation: int = II("value_at_root") | |
| class NestedSubclass(Nested): | |
| additional: int = 20 | |
| class NestedConfig: | |
| default_value: Nested | |
| # with default value | |
| user_provided_default: Nested = Nested(with_default=42) | |
| value_at_root: int = 1000 | |
| class NestedWithAny: | |
| var: Any = Nested() | |
| class NoDefaultErrors: | |
| no_default: Any | |
| class Interpolation: | |
| x: int = 100 | |
| y: int = 200 | |
| # The real type of y is int, cast the interpolation string | |
| # to help static type checkers to see this truth | |
| z1: int = II("x") | |
| z2: str = SI("${x}_${y}") | |
| class BoolOptional: | |
| with_default: Optional[bool] = True | |
| as_none: Optional[bool] = None | |
| not_optional: bool = True | |
| class IntegerOptional: | |
| with_default: Optional[int] = 1 | |
| as_none: Optional[int] = None | |
| not_optional: int = 1 | |
| class FloatOptional: | |
| with_default: Optional[float] = 1.0 | |
| as_none: Optional[float] = None | |
| not_optional: float = 1 | |
| class StringOptional: | |
| with_default: Optional[str] = "foo" | |
| as_none: Optional[str] = None | |
| not_optional: str = "foo" | |
| class ListOptional: | |
| with_default: Optional[List[int]] = [1, 2, 3] | |
| as_none: Optional[List[int]] = None | |
| not_optional: List[int] = [1, 2, 3] | |
| class TupleOptional: | |
| with_default: Optional[Tuple[int, int, int]] = (1, 2, 3) | |
| as_none: Optional[Tuple[int, int, int]] = None | |
| not_optional: Tuple[int, int, int] = (1, 2, 3) | |
| class EnumOptional: | |
| with_default: Optional[Color] = Color.BLUE | |
| as_none: Optional[Color] = None | |
| not_optional: Color = Color.BLUE | |
| class DictOptional: | |
| with_default: Optional[Dict[str, int]] = {"a": 10} | |
| as_none: Optional[Dict[str, int]] = None | |
| not_optional: Dict[str, int] = {"a": 10} | |
| class StructuredOptional: | |
| with_default: Optional[Nested] = Nested() | |
| as_none: Optional[Nested] = None | |
| not_optional: Nested = Nested() | |
| class FrozenClass: | |
| user: User = User(name="Bart", age=10) | |
| x: int = 10 | |
| list: List[int] = [1, 2, 3] | |
| class ContainsFrozen: | |
| x: int = 10 | |
| frozen: FrozenClass = FrozenClass() | |
| class WithTypedList: | |
| list: List[int] = [1, 2, 3] | |
| class WithTypedDict: | |
| dict: Dict[str, int] = {"foo": 10, "bar": 20} | |
| class ErrorDictIntKey: | |
| # invalid dict key, must be str | |
| dict: Dict[int, str] = {10: "foo", 20: "bar"} | |
| class RegularClass: | |
| pass | |
| class ErrorDictUnsupportedValue: | |
| # invalid dict value type, not one of the supported types | |
| dict: Dict[str, RegularClass] = {} | |
| class ErrorListUnsupportedValue: | |
| # invalid dict value type, not one of the supported types | |
| dict: List[RegularClass] = [] | |
| class ListExamples: | |
| any: List[Any] = [1, "foo"] | |
| ints: List[int] = [1, 2] | |
| strings: List[str] = ["foo", "bar"] | |
| booleans: List[bool] = [True, False] | |
| colors: List[Color] = [Color.RED, Color.GREEN] | |
| class TupleExamples: | |
| any: Tuple[Any, Any] = (1, "foo") | |
| ints: Tuple[int, int] = (1, 2) | |
| strings: Tuple[str, str] = ("foo", "bar") | |
| booleans: Tuple[bool, bool] = (True, False) | |
| colors: Tuple[Color, Color] = (Color.RED, Color.GREEN) | |
| class DictExamples: | |
| any: Dict[str, Any] = {"a": 1, "b": "foo"} | |
| ints: Dict[str, int] = {"a": 10, "b": 20} | |
| strings: Dict[str, str] = {"a": "foo", "b": "bar"} | |
| booleans: Dict[str, bool] = {"a": True, "b": False} | |
| colors: Dict[str, Color] = { | |
| "red": Color.RED, | |
| "green": Color.GREEN, | |
| "blue": Color.BLUE, | |
| } | |
| class DictWithEnumKeys: | |
| enum_key: Dict[Color, str] = {Color.RED: "red", Color.GREEN: "green"} | |
| class DictOfObjects: | |
| users: Dict[str, User] = {"joe": User(name="Joe", age=18)} | |
| class ListOfObjects: | |
| users: List[User] = [User(name="Joe", age=18)] | |
| class DictSubclass: | |
| class Str2Str(Dict[str, str]): | |
| pass | |
| class Color2Str(Dict[Color, str]): | |
| pass | |
| class Color2Color(Dict[Color, Color]): | |
| pass | |
| class Str2User(Dict[str, User]): | |
| pass | |
| class Str2StrWithField(Dict[str, str]): | |
| foo: str = "bar" | |
| class Str2IntWithStrField(Dict[str, int]): | |
| foo: str = "bar" | |
| class Error: | |
| class User2Str(Dict[User, str]): | |
| pass | |
| class Plugin: | |
| name: str = MISSING | |
| params: Any = MISSING | |
| class ConcretePlugin(Plugin): | |
| name: str = "foobar_plugin" | |
| class FoobarParams: | |
| foo: int = 10 | |
| params: FoobarParams = FoobarParams() | |
| class PluginWithAdditionalField(Plugin): | |
| name: str = "foobar2_plugin" | |
| additional: int = 10 | |
| # Does not extend Plugin, cannot be assigned or merged | |
| class FaultyPlugin: | |
| name: str = "faulty_plugin" | |
| class PluginHolder: | |
| none: Optional[Plugin] = None | |
| missing: Plugin = MISSING | |
| plugin: Plugin = Plugin() | |
| plugin2: Plugin = ConcretePlugin() | |
| class LinkedList: | |
| next: Optional["LinkedList"] = None | |
| value: Any = MISSING | |
| class MissingTest: | |
| class Missing1: | |
| head: LinkedList = MISSING | |
| class Missing2: | |
| head: LinkedList = LinkedList(next=MISSING, value=1) | |
| class NestedWithNone: | |
| plugin: Optional[Plugin] = None | |
| class UnionError: | |
| x: Union[int, str] = 10 | |
| class WithNativeMISSING: | |
| num: int = attr.NOTHING # type: ignore | |
| class MissingStructuredConfigField: | |
| plugin: Plugin = MISSING | |
| class ListClass: | |
| list: List[int] = [] | |
| tuple: Tuple[int, int] = (1, 2) | |