Skip to content

Commit

Permalink
Replace Asserter with module-level assertion functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bartfeenstra committed Jun 22, 2024
1 parent 643047d commit 84abd84
Show file tree
Hide file tree
Showing 13 changed files with 647 additions and 622 deletions.
11 changes: 6 additions & 5 deletions betty/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@
AssertionFailed,
Fields,
OptionalField,
Asserter,
AssertionChain,
assert_record,
assert_setattr,
assert_str,
)
from betty.warnings import deprecate

Expand Down Expand Up @@ -158,13 +160,12 @@ def load(
) -> Self:
if configuration is None:
configuration = cls()
asserter = Asserter()
asserter.assert_record(
assert_record(

Check warning on line 163 in betty/app/__init__.py

View check run for this annotation

Codecov / codecov/patch

betty/app/__init__.py#L163

Added line #L163 was not covered by tests
Fields(
OptionalField(
"locale",
AssertionChain(asserter.assert_str())
| asserter.assert_setattr(configuration, "locale"),
AssertionChain(assert_str())
| assert_setattr(configuration, "locale"),
),
),
)(dump)
Expand Down
11 changes: 4 additions & 7 deletions betty/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from betty.serde.dump import Dumpable, Dump, minimize, VoidableDump, Void
from betty.serde.error import SerdeErrorCollection
from betty.serde.format import FormatRepository
from betty.serde.load import Asserter, Assertion
from betty.serde.load import Assertion, assert_dict, assert_mapping, assert_sequence

if TYPE_CHECKING:
from _weakref import ReferenceType
Expand All @@ -58,7 +58,6 @@ class Configuration(Dumpable):

def __init__(self, *args: Any, **kwargs: Any):
super().__init__(*args, **kwargs)
self._asserter = Asserter()
self._on_change_listeners: MutableSequence[
ReferenceType[_ConfigurationListener]
] = []
Expand Down Expand Up @@ -503,9 +502,8 @@ def load(
configuration = cls()
else:
configuration._clear_without_dispatch()
asserter = Asserter()
with SerdeErrorCollection().assert_valid():
configuration.append(*asserter.assert_sequence(cls._item_type().load)(dump))
configuration.append(*assert_sequence(cls._item_type().load)(dump))
return configuration

@override
Expand Down Expand Up @@ -658,9 +656,8 @@ def load(
) -> Self:
if configuration is None:
configuration = cls()
asserter = Asserter()
dict_dump = asserter.assert_dict()(dump)
mapping = asserter.assert_mapping(cls._item_type().load)(
dict_dump = assert_dict()(dump)
mapping = assert_mapping(cls._item_type().load)(
{key: cls._load_key(value, key) for key, value in dict_dump.items()}
)
configuration.replace(*mapping.values())
Expand Down
15 changes: 8 additions & 7 deletions betty/extension/cotton_candy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@
AssertionFailed,
Fields,
OptionalField,
Asserter,
AssertionChain,
assert_str,
assert_record,
assert_path,
assert_setattr,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -95,8 +98,7 @@ def load(
dump: Dump,
configuration: Self | None = None,
) -> Self:
asserter = Asserter()
hex_value = asserter.assert_str()(dump)
hex_value = assert_str()(dump)
if configuration is None:
configuration = cls(hex_value)
else:
Expand Down Expand Up @@ -201,8 +203,7 @@ def load(
) -> Self:
if configuration is None:
configuration = cls()
asserter = Asserter()
asserter.assert_record(
assert_record(
Fields(
OptionalField(
"featured_entities",
Expand Down Expand Up @@ -236,8 +237,8 @@ def load(
),
OptionalField(
"logo",
AssertionChain(asserter.assert_path())
| asserter.assert_setattr(configuration, "logo"),
AssertionChain(assert_path())
| assert_setattr(configuration, "logo"),
),
)
)(dump)
Expand Down
14 changes: 7 additions & 7 deletions betty/extension/gramps/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
from betty.config import Configuration, ConfigurationSequence
from betty.serde.dump import minimize, Dump, VoidableDump
from betty.serde.load import (
Asserter,
Fields,
RequiredField,
OptionalField,
AssertionChain,
assert_record,
assert_path,
assert_setattr,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -61,13 +63,12 @@ def load(
) -> Self:
if configuration is None:
configuration = cls()
asserter = Asserter()
asserter.assert_record(
assert_record(
Fields(
RequiredField(
"file",
AssertionChain(asserter.assert_path())
| asserter.assert_setattr(configuration, "file_path"),
AssertionChain(assert_path())
| assert_setattr(configuration, "file_path"),
),
)
)(dump)
Expand Down Expand Up @@ -128,8 +129,7 @@ def load(
) -> Self:
if configuration is None:
configuration = cls()
asserter = Asserter()
asserter.assert_record(
assert_record(
Fields(
OptionalField(
"family_trees",
Expand Down
27 changes: 16 additions & 11 deletions betty/extension/nginx/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@

from betty.config import Configuration
from betty.serde.dump import Dump, VoidableDump, minimize, Void, VoidableDictDump
from betty.serde.load import Asserter, Fields, OptionalField, AssertionChain
from betty.serde.load import (
Fields,
OptionalField,
AssertionChain,
assert_record,
assert_or,
assert_bool,
assert_none,
assert_setattr,
assert_str,
)


class NginxConfiguration(Configuration):
Expand Down Expand Up @@ -66,22 +76,17 @@ def load(
) -> Self:
if configuration is None:
configuration = cls()
asserter = Asserter()
asserter.assert_record(
assert_record(
Fields(
OptionalField(
"https",
AssertionChain(
asserter.assert_or(
asserter.assert_bool(), asserter.assert_none()
)
)
| asserter.assert_setattr(configuration, "https"),
AssertionChain(assert_or(assert_bool(), assert_none()))
| assert_setattr(configuration, "https"),
),
OptionalField(
"www_directory_path",
AssertionChain(asserter.assert_str())
| asserter.assert_setattr(configuration, "www_directory_path"),
AssertionChain(assert_str())
| assert_setattr(configuration, "www_directory_path"),
),
)
)(dump)
Expand Down
16 changes: 11 additions & 5 deletions betty/extension/wikipedia/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@

from betty.config import Configuration
from betty.serde.dump import Dump, VoidableDump, minimize, VoidableDictDump
from betty.serde.load import Asserter, Fields, OptionalField, AssertionChain
from betty.serde.load import (
Fields,
OptionalField,
AssertionChain,
assert_record,
assert_bool,
assert_setattr,
)


class WikipediaConfiguration(Configuration):
Expand Down Expand Up @@ -45,13 +52,12 @@ def load(
) -> Self:
if configuration is None:
configuration = cls()
asserter = Asserter()
asserter.assert_record(
assert_record(
Fields(
OptionalField(
"populate_images",
AssertionChain(asserter.assert_bool())
| asserter.assert_setattr(configuration, "populate_images"),
AssertionChain(assert_bool())
| assert_setattr(configuration, "populate_images"),
),
)
)(dump)
Expand Down
Loading

0 comments on commit 84abd84

Please sign in to comment.