def _load_config(self) -> ServerConfig:
        user_config_path = 'config.yaml'

        # Start with a default config, and if there is a config.yaml,
        # assign whatever is contained within.
        config = ServerConfig()
        if os.path.exists(user_config_path):
            import yaml
            with open(user_config_path) as infile:
                user_config = yaml.safe_load(infile.read())

            # An empty config file will yield None, and that's ok.
            if user_config is not None:
                dataclass_assign(config, user_config)

        return config
Beispiel #2
0
def test_assign() -> None:
    """Testing various assignments."""
    # pylint: disable=too-many-statements

    @dataclass
    class _TestClass:
        ival: int = 0
        sval: str = ''
        bval: bool = True
        fval: float = 1.0
        oival: Optional[int] = None
        osval: Optional[str] = None
        obval: Optional[bool] = None
        ofval: Optional[float] = None
        lsval: List[str] = field(default_factory=list)
        lival: List[int] = field(default_factory=list)
        lbval: List[bool] = field(default_factory=list)
        lfval: List[float] = field(default_factory=list)

    tclass = _TestClass()

    class _TestClass2:
        pass

    tclass2 = _TestClass2()

    # Arg types:
    with pytest.raises(TypeError):
        dataclass_assign(tclass2, {})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, [])  # type: ignore

    # Invalid attrs.
    with pytest.raises(AttributeError):
        dataclass_assign(tclass, {'nonexistent': 'foo'})

    # Correct types.
    dataclass_assign(
        tclass, {
            'ival': 1,
            'sval': 'foo',
            'bval': True,
            'fval': 2.0,
            'lsval': ['foo'],
            'lival': [10],
            'lbval': [False],
            'lfval': [1.0]
        })
    dataclass_assign(
        tclass, {
            'oival': None,
            'osval': None,
            'obval': None,
            'ofval': None,
            'lsval': [],
            'lival': [],
            'lbval': [],
            'lfval': []
        })
    dataclass_assign(
        tclass, {
            'oival': 1,
            'osval': 'foo',
            'obval': True,
            'ofval': 2.0,
            'lsval': ['foo', 'bar', 'eep'],
            'lival': [10, 11, 12],
            'lbval': [False, True],
            'lfval': [1.0, 2.0, 3.0]
        })

    # Type mismatches.
    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'ival': 'foo'})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'sval': 1})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'bval': 2})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'oival': 'foo'})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'osval': 1})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'obval': 2})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'ofval': 'blah'})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'lsval': 'blah'})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'lsval': [1]})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'lbval': [None]})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'lival': ['foo']})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'lfval': [True]})

    # More subtle ones (we currently require EXACT type matches)
    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'ival': True})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'fval': 2})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'bval': 1})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'ofval': 1})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'lfval': [1]})
def test_assign() -> None:
    """Testing various assignments."""
    @dataclass
    class _TestClass:
        ival: int = 0
        sval: str = ''
        bval: bool = True
        fval: float = 1.0
        oival: Optional[int] = None
        osval: Optional[str] = None
        obval: Optional[bool] = None
        ofval: Optional[float] = None

    tclass = _TestClass()

    class _TestClass2:
        pass

    tclass2 = _TestClass2()

    # Arg types:
    with pytest.raises(TypeError):
        dataclass_assign(tclass2, {})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, [])  # type: ignore

    # Invalid attrs.
    with pytest.raises(AttributeError):
        dataclass_assign(tclass, {'nonexistent': 'foo'})

    # Correct types.
    dataclass_assign(tclass, {
        'ival': 1,
        'sval': 'foo',
        'bval': True,
        'fval': 2.0,
    })
    dataclass_assign(tclass, {
        'oival': None,
        'osval': None,
        'obval': None,
        'ofval': None,
    })
    dataclass_assign(tclass, {
        'oival': 1,
        'osval': 'foo',
        'obval': True,
        'ofval': 2.0,
    })

    # Type mismatches.
    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'ival': 'foo'})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'sval': 1})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'bval': 2})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'oival': 'foo'})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'osval': 1})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'obval': 2})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'ofval': 'blah'})

    # More subtle ones (we currently require EXACT type matches)
    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'ival': True})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'fval': 2})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'bval': 1})

    with pytest.raises(TypeError):
        dataclass_assign(tclass, {'ofval': 1})