|
from dataclasses import dataclass, field |
|
from enum import Enum |
|
from typing import Any, Dict, List, Optional |
|
|
|
import pytest |
|
|
|
from omegaconf import ( |
|
MISSING, |
|
DictConfig, |
|
MissingMandatoryValue, |
|
OmegaConf, |
|
ReadonlyConfigError, |
|
ValidationError, |
|
) |
|
|
|
|
|
class Height(Enum): |
|
SHORT = 0 |
|
TALL = 1 |
|
|
|
|
|
@dataclass |
|
class SimpleTypes: |
|
num: int = 10 |
|
pi: float = 3.1415 |
|
is_awesome: bool = True |
|
height: Height = Height.SHORT |
|
description: str = "text" |
|
|
|
|
|
def test_simple_types_class() -> None: |
|
|
|
conf = OmegaConf.structured(SimpleTypes) |
|
assert conf.num == 10 |
|
assert conf.pi == 3.1415 |
|
assert conf.is_awesome is True |
|
assert conf.height == Height.SHORT |
|
assert conf.description == "text" |
|
|
|
|
|
def test_static_typing() -> None: |
|
conf: SimpleTypes = OmegaConf.structured(SimpleTypes) |
|
assert conf.description == "text" |
|
with pytest.raises(AttributeError): |
|
|
|
|
|
conf.no_such_attribute |
|
|
|
|
|
def test_simple_types_obj() -> None: |
|
|
|
|
|
conf = OmegaConf.structured(SimpleTypes(num=20, pi=3)) |
|
assert conf.num == 20 |
|
assert conf.pi == 3 |
|
|
|
assert conf.is_awesome is True |
|
assert conf.height == Height.SHORT |
|
assert conf.description == "text" |
|
|
|
|
|
def test_conversions() -> None: |
|
conf: SimpleTypes = OmegaConf.structured(SimpleTypes) |
|
|
|
|
|
conf.num = 20 |
|
|
|
|
|
conf.num = "20" |
|
|
|
assert conf.num == 20 |
|
with pytest.raises(ValidationError): |
|
|
|
conf.num = "one" |
|
|
|
|
|
for expected, values in { |
|
True: ["on", "yes", "true", True, "1"], |
|
False: ["off", "no", "false", False, "0"], |
|
}.items(): |
|
for b in values: |
|
conf.is_awesome = b |
|
assert conf.is_awesome == expected |
|
|
|
|
|
for expected1, values1 in { |
|
Height.SHORT: [Height.SHORT, "Height.SHORT", "SHORT", 0], |
|
Height.TALL: [Height.TALL, "Height.TALL", "TALL", 1], |
|
}.items(): |
|
for b in values1: |
|
conf.height = b |
|
assert conf.height == expected1 |
|
|
|
|
|
@dataclass |
|
class Modifiers: |
|
|
|
num: int = 10 |
|
|
|
|
|
optional_num: Optional[int] = None |
|
|
|
|
|
|
|
another_num: int = MISSING |
|
|
|
|
|
def test_modifiers() -> None: |
|
conf: Modifiers = OmegaConf.structured(Modifiers) |
|
|
|
with pytest.raises(ValidationError): |
|
conf.num = None |
|
|
|
|
|
conf.optional_num = None |
|
assert conf.optional_num is None |
|
|
|
|
|
with pytest.raises(MissingMandatoryValue): |
|
|
|
conf.another_num |
|
|
|
|
|
conf.another_num = 42 |
|
assert conf.another_num == 42 |
|
|
|
|
|
@dataclass |
|
class User: |
|
|
|
name: str = MISSING |
|
height: Height = MISSING |
|
|
|
|
|
|
|
@dataclass |
|
class Group: |
|
name: str = MISSING |
|
|
|
admin: User = User |
|
|
|
|
|
manager: User = User(name="manager", height=Height.TALL) |
|
|
|
|
|
def test_nesting() -> None: |
|
conf = OmegaConf.structured(Group) |
|
assert conf == { |
|
"name": "???", |
|
"admin": {"name": MISSING, "height": MISSING}, |
|
"manager": {"name": "manager", "height": Height.TALL}, |
|
} |
|
|
|
expected = """name: ??? |
|
admin: |
|
name: ??? |
|
height: ??? |
|
manager: |
|
name: manager |
|
height: TALL |
|
""" |
|
assert OmegaConf.to_yaml(conf) == expected |
|
|
|
|
|
conf.admin = User(name="omry", height=Height.TALL) |
|
with pytest.raises(ValidationError): |
|
|
|
conf.admin = 10 |
|
|
|
with pytest.raises(ValidationError): |
|
|
|
conf.manager = {"name": "secret", "height": Height.TALL} |
|
|
|
|
|
@dataclass |
|
class Lists: |
|
|
|
|
|
untyped_list: List[Any] = field(default_factory=lambda: [1, "foo", True]) |
|
|
|
|
|
int_list: List[int] = field(default_factory=lambda: [10, 20, 30]) |
|
|
|
|
|
def test_typed_list_runtime_validation() -> None: |
|
conf = OmegaConf.structured(Lists) |
|
|
|
conf.untyped_list[0] = True |
|
|
|
conf.int_list[0] = 999 |
|
assert conf.int_list[0] == 999 |
|
|
|
conf.int_list[0] = "1000" |
|
assert conf.int_list[0] == 1000 |
|
|
|
with pytest.raises(ValidationError): |
|
conf.int_list[0] = "fail" |
|
|
|
|
|
|
|
@dataclass |
|
class Dicts: |
|
|
|
|
|
untyped_dict: Dict[str, Any] = field( |
|
default_factory=lambda: {"foo": True, "bar": 100} |
|
) |
|
|
|
|
|
str_to_height: Dict[str, Height] = field( |
|
default_factory=lambda: {"Yoda": Height.SHORT, "3-CPO": Height.TALL} |
|
) |
|
|
|
|
|
def test_typed_dict_runtime_validation() -> None: |
|
conf = OmegaConf.structured(Dicts) |
|
conf.untyped_dict["foo"] = "buzz" |
|
conf.str_to_height["Shorty"] = Height.SHORT |
|
with pytest.raises(ValidationError): |
|
|
|
conf.str_to_height["Yoda"] = True |
|
|
|
|
|
|
|
@dataclass(frozen=True) |
|
class FrozenClass: |
|
x: int = 10 |
|
list: List[int] = field(default_factory=lambda: [1, 2, 3]) |
|
|
|
|
|
def test_frozen() -> None: |
|
|
|
|
|
conf = OmegaConf.structured(FrozenClass) |
|
with pytest.raises(ReadonlyConfigError): |
|
conf.x = 20 |
|
|
|
|
|
with pytest.raises(ReadonlyConfigError): |
|
conf.list[0] = 20 |
|
|
|
|
|
class Protocol(Enum): |
|
HTTP = 0 |
|
HTTPS = 1 |
|
|
|
|
|
@dataclass |
|
class Domain: |
|
name: str = MISSING |
|
path: str = MISSING |
|
protocols: List[Protocol] = field(default_factory=lambda: [Protocol.HTTPS]) |
|
|
|
|
|
@dataclass |
|
class WebServer(DictConfig): |
|
protocol_ports: Dict[Protocol, int] = field( |
|
default_factory=lambda: {Protocol.HTTP: 80, Protocol.HTTPS: 443} |
|
) |
|
|
|
domains: Dict[str, Domain] = field(default_factory=dict) |
|
|
|
|
|
domains_list: List[Domain] = field(default_factory=list) |
|
|
|
|
|
|
|
def test_enum_key() -> None: |
|
conf = OmegaConf.structured(WebServer) |
|
|
|
|
|
assert conf.protocol_ports.HTTP == 80 |
|
assert conf.protocol_ports["HTTP"] == 80 |
|
assert conf.protocol_ports[Protocol.HTTP] == 80 |
|
|
|
|
|
def test_dict_of_objects() -> None: |
|
conf: WebServer = OmegaConf.structured(WebServer) |
|
conf.domains["blog"] = Domain(name="blog.example.com", path="/www/blog.example.com") |
|
with pytest.raises(ValidationError): |
|
conf.domains.foo = 10 |
|
|
|
assert conf.domains["blog"].name == "blog.example.com" |
|
assert conf.domains["blog"].path == "/www/blog.example.com" |
|
assert conf == { |
|
"protocol_ports": {Protocol.HTTP: 80, Protocol.HTTPS: 443}, |
|
"domains": { |
|
"blog": { |
|
"name": "blog.example.com", |
|
"path": "/www/blog.example.com", |
|
"protocols": [Protocol.HTTPS], |
|
} |
|
}, |
|
"domains_list": [], |
|
} |
|
|
|
|
|
def test_list_of_objects() -> None: |
|
conf: WebServer = OmegaConf.structured(WebServer) |
|
conf.domains_list.append( |
|
Domain(name="blog.example.com", path="/www/blog.example.com") |
|
) |
|
with pytest.raises(ValidationError): |
|
conf.domains_list.append(10) |
|
|
|
assert conf.domains_list[0].name == "blog.example.com" |
|
assert conf.domains_list[0].path == "/www/blog.example.com" |
|
assert conf == { |
|
"protocol_ports": {Protocol.HTTP: 80, Protocol.HTTPS: 443}, |
|
"domains": {}, |
|
"domains_list": [ |
|
{ |
|
"name": "blog.example.com", |
|
"path": "/www/blog.example.com", |
|
"protocols": [Protocol.HTTPS], |
|
} |
|
], |
|
} |
|
|
|
|
|
def test_merge() -> None: |
|
@dataclass |
|
class Config: |
|
num: int = 10 |
|
user: User = User(name=MISSING, height=MISSING) |
|
domains: Dict[str, Domain] = field(default_factory=dict) |
|
|
|
yaml = """ |
|
user: |
|
name: Omry |
|
domains: |
|
blog_website: |
|
name: blog |
|
protocols: |
|
- HTTPS |
|
""" |
|
|
|
schema: Config = OmegaConf.structured(Config) |
|
cfg = OmegaConf.create(yaml) |
|
merged: Any = OmegaConf.merge(schema, cfg) |
|
assert merged == { |
|
"num": 10, |
|
"user": {"name": "Omry", "height": "???"}, |
|
"domains": { |
|
"blog_website": { |
|
"name": "blog", |
|
"path": "???", |
|
"protocols": [Protocol.HTTPS], |
|
} |
|
}, |
|
} |
|
assert OmegaConf.is_missing(merged.domains.blog_website, "path") |
|
|
|
|
|
def test_merge_example() -> None: |
|
@dataclass |
|
class Server: |
|
port: int = MISSING |
|
|
|
@dataclass |
|
class Log: |
|
file: str = MISSING |
|
rotation: int = MISSING |
|
|
|
@dataclass |
|
class MyConfig: |
|
server: Server = Server() |
|
log: Log = Log() |
|
users: List[str] = field(default_factory=list) |
|
numbers: List[int] = field(default_factory=list) |
|
|
|
schema = OmegaConf.structured(MyConfig) |
|
with pytest.raises(ValidationError): |
|
OmegaConf.merge(schema, OmegaConf.create({"log": {"rotation": "foo"}})) |
|
|
|
with pytest.raises(ValidationError): |
|
cfg = schema.copy() |
|
cfg.numbers.append("fo") |
|
|
|
with pytest.raises(ValidationError): |
|
OmegaConf.merge(schema, OmegaConf.create({"numbers": ["foo"]})) |
|
|