Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion comprehensiveconfig/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def dump_value(cls, node: spec.AnyConfigField, value):
return value.name
case spec.ConfigEnum(_, False):
return value.value
case str() | int() | float() | datetime() | dict() | None:
case str() | int() | float() | bool() | datetime() | dict() | None:
return value
case _:
# magic method to make writing new field types possible
Expand Down
43 changes: 22 additions & 21 deletions comprehensiveconfig/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,27 +321,6 @@ def nullable(self):
return False


class Float(ConfigurationField):
"""Floating point field"""

__slots__ = ()

_holds: float

def __get__(self, instance, owner) -> float:
return super().__get__(instance, owner)

def __set__(self, instance, value: float):
super().__set__(instance, value)

def _validate_value(self, value: Any, name: str | None = None, /):
super()._validate_value(value)
if not isinstance(value, (float, int)):
raise ValueError(
f"Field: {name or self._name}\nValue was not a valid number: {repr(value)}"
)


class List[T](ConfigurationField):
"""List field"""

Expand Down Expand Up @@ -583,6 +562,27 @@ def _validate_value(self, value: Any, name: str | None = None, /):
self.inner_type._validate_value(item, f"{name or self._name}[{c}]")


class Boolean(ConfigurationField):
"""Boolean (true/false) field"""

__slots__ = ()

_holds: bool

def __get__(self, instance, owner) -> bool:
return super().__get__(instance, owner)

def __set__(self, instance, value: bool):
super().__set__(instance, value)

def _validate_value(self, value: Any, name: str | None = None, /):
super()._validate_value(value)
if not isinstance(value, bool):
raise ValueError(
f"Field: {name or self._name}\nValue was not a valid boolean: {repr(value)}"
)


class Float(ConfigurationField):
"""Floating point field"""

Expand Down Expand Up @@ -840,6 +840,7 @@ def _validate_value(self, value: Any, name: str | None = None, /):
"NoDefaultValue",
"_NoDefaultValueT",
"Section",
"Boolean",
"Float",
"Integer",
"Number",
Expand Down
2 changes: 2 additions & 0 deletions comprehensiveconfig/toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def format_value(cls, field, value) -> str:
match value:
case int() | float():
return str(value)
case bool():
return "true" if value else "false"
case str():
return f'"{escape(value)}"'
case list():
Expand Down
1 change: 1 addition & 0 deletions comprehensiveconfig/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Bar(comprehensiveconfig.spec.Section, name="burger"):
)
test_int = comprehensiveconfig.spec.Integer(20)
test_float = comprehensiveconfig.spec.Float(20.20)
test_bool = comprehensiveconfig.spec.Boolean(True)
test_dict = comprehensiveconfig.spec.Table(
{10: "burgers"},
key_type=comprehensiveconfig.spec.Integer(),
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "comprehensiveconfig"
version = "1.1.0"
version = "1.1.1"
description = "A library to create ergonomic, auto-validated configuration models with great support for static type annotations."
readme = "readme.md"
requires-python = ">=3.12"
Expand Down
1 change: 1 addition & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ class Bar(comprehensiveconfig.spec.Section, name="burger"):
)
test_int = comprehensiveconfig.spec.Integer(20)
test_float = comprehensiveconfig.spec.Float(20.20)
test_bool = comprehensiveconfig.spec.Boolean(True)
test_dict = comprehensiveconfig.spec.Table(
{10: "burgers"},
key_type=comprehensiveconfig.spec.Integer(),
Expand Down