Skip to content

Commit

Permalink
OpenEoBackendConfig: kw_only by default
Browse files Browse the repository at this point in the history
Allows mandatory fields (no sensible default) after fields with defaults
  • Loading branch information
soxofaan committed Jun 14, 2023
1 parent 423c759 commit 065166a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion openeo_driver/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.54.0a1"
__version__ = "0.55.0a1"
6 changes: 5 additions & 1 deletion openeo_driver/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ class ConfigException(ValueError):
pass


@attrs.frozen
@attrs.frozen(
# Note: `kw_only=True` enforces "kwargs" based construction (which is good for readability/maintainability)
# and allows defining mandatory fields (fields without default) after optional fields.
kw_only=True
)
class OpenEoBackendConfig:
"""
Configuration for openEO backend.
Expand Down
18 changes: 18 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,21 @@ def test_get_backend_config_not_found(monkeypatch, tmp_path, final_flush):
get_backend_config.flush()
with pytest.raises(FileNotFoundError):
_ = get_backend_config()


def test_kw_only():
with pytest.raises(TypeError, match="takes 1 positional argument but 3 were given"):
OpenEoBackendConfig(123, [])


def test_add_mandatory_fields():
@attrs.frozen(kw_only=True)
class MyConfig(OpenEoBackendConfig):
color: str = "red"
set_this_or_die: int

with pytest.raises(TypeError, match="missing.*required.*argument.*set_this_or_die"):
_ = MyConfig()

conf = MyConfig(set_this_or_die=4)
assert conf.set_this_or_die == 4

0 comments on commit 065166a

Please sign in to comment.