Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify how assertions can be chained and passed as arguments #1574

Merged
merged 1 commit into from
Jun 21, 2024
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
4 changes: 2 additions & 2 deletions betty/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@
from betty.serde.load import (
AssertionFailed,
Fields,
Assertions,
OptionalField,
Asserter,
AssertionChain,
)
from betty.warnings import deprecate

Expand Down Expand Up @@ -164,7 +164,7 @@ def load(
Fields(
OptionalField(
"locale",
Assertions(asserter.assert_str())
AssertionChain(asserter.assert_str())
| asserter.assert_setattr(configuration, "locale"),
),
),
Expand Down
10 changes: 3 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, Assertions
from betty.serde.load import Asserter, Assertion

if TYPE_CHECKING:
from _weakref import ReferenceType
Expand Down Expand Up @@ -507,11 +507,7 @@ def load(
configuration._clear_without_dispatch()
asserter = Asserter()
with SerdeErrorCollection().assert_valid():
configuration.append(
*asserter.assert_sequence(Assertions(cls._item_type().assert_load()))(
dump
)
)
configuration.append(*asserter.assert_sequence(cls._item_type().load)(dump))
return configuration

@override
Expand Down Expand Up @@ -666,7 +662,7 @@ def load(
configuration = cls()
asserter = Asserter()
dict_dump = asserter.assert_dict()(dump)
mapping = asserter.assert_mapping(Assertions(cls._item_type().load))(
mapping = asserter.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
34 changes: 12 additions & 22 deletions betty/extension/cotton_candy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
from betty.serde.load import (
AssertionFailed,
Fields,
Assertions,
OptionalField,
Asserter,
AssertionChain,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -204,47 +204,37 @@ def load(
Fields(
OptionalField(
"featured_entities",
Assertions(
configuration._featured_entities.assert_load(
configuration._featured_entities
)
configuration._featured_entities.assert_load(
configuration._featured_entities
),
),
OptionalField(
"primary_inactive_color",
Assertions(
configuration._primary_inactive_color.assert_load(
configuration._primary_inactive_color
)
configuration._primary_inactive_color.assert_load(
configuration._primary_inactive_color
),
),
OptionalField(
"primary_active_color",
Assertions(
configuration._primary_active_color.assert_load(
configuration._primary_active_color
)
configuration._primary_active_color.assert_load(
configuration._primary_active_color
),
),
OptionalField(
"link_inactive_color",
Assertions(
configuration._link_inactive_color.assert_load(
configuration._link_inactive_color
)
configuration._link_inactive_color.assert_load(
configuration._link_inactive_color
),
),
OptionalField(
"link_active_color",
Assertions(
configuration._link_active_color.assert_load(
configuration._link_active_color
)
configuration._link_active_color.assert_load(
configuration._link_active_color
),
),
OptionalField(
"logo",
Assertions(asserter.assert_path())
AssertionChain(asserter.assert_path())
| asserter.assert_setattr(configuration, "logo"),
),
)
Expand Down
16 changes: 9 additions & 7 deletions betty/extension/gramps/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@

from betty.config import Configuration, ConfigurationSequence
from betty.serde.dump import minimize, Dump, VoidableDump
from betty.serde.load import Asserter, Fields, RequiredField, Assertions, OptionalField
from betty.serde.load import (
Asserter,
Fields,
RequiredField,
OptionalField,
AssertionChain,
)

if TYPE_CHECKING:
from pathlib import Path
Expand Down Expand Up @@ -60,7 +66,7 @@ def load(
Fields(
RequiredField(
"file",
Assertions(asserter.assert_path())
AssertionChain(asserter.assert_path())
| asserter.assert_setattr(configuration, "file_path"),
),
)
Expand Down Expand Up @@ -127,11 +133,7 @@ def load(
Fields(
OptionalField(
"family_trees",
Assertions(
configuration._family_trees.assert_load(
configuration.family_trees
)
),
configuration._family_trees.assert_load(configuration.family_trees),
),
)
)(dump)
Expand Down
6 changes: 3 additions & 3 deletions betty/extension/nginx/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from betty.config import Configuration
from betty.serde.dump import Dump, VoidableDump, minimize, Void, VoidableDictDump
from betty.serde.load import Asserter, Fields, OptionalField, Assertions
from betty.serde.load import Asserter, Fields, OptionalField, AssertionChain


class NginxConfiguration(Configuration):
Expand Down Expand Up @@ -71,7 +71,7 @@ def load(
Fields(
OptionalField(
"https",
Assertions(
AssertionChain(
asserter.assert_or(
asserter.assert_bool(), asserter.assert_none()
)
Expand All @@ -80,7 +80,7 @@ def load(
),
OptionalField(
"www_directory_path",
Assertions(asserter.assert_str())
AssertionChain(asserter.assert_str())
| asserter.assert_setattr(configuration, "www_directory_path"),
),
)
Expand Down
4 changes: 2 additions & 2 deletions betty/extension/wikipedia/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from betty.config import Configuration
from betty.serde.dump import Dump, VoidableDump, minimize, VoidableDictDump
from betty.serde.load import Asserter, Fields, OptionalField, Assertions
from betty.serde.load import Asserter, Fields, OptionalField, AssertionChain


class WikipediaConfiguration(Configuration):
Expand Down Expand Up @@ -50,7 +50,7 @@ def load(
Fields(
OptionalField(
"populate_images",
Assertions(asserter.assert_bool())
AssertionChain(asserter.assert_bool())
| asserter.assert_setattr(configuration, "populate_images"),
),
)
Expand Down
46 changes: 22 additions & 24 deletions betty/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
from betty.serde.load import (
AssertionFailed,
Fields,
Assertions,
Assertion,
RequiredField,
OptionalField,
Asserter,
AssertionChain,
)
from betty.warnings import deprecate

Expand Down Expand Up @@ -132,12 +132,12 @@ def load(
Fields(
RequiredField(
"entity_type",
Assertions(asserter.assert_entity_type())
AssertionChain(asserter.assert_entity_type())
| asserter.assert_setattr(configuration, "entity_type"),
),
OptionalField(
"entity_id",
Assertions(asserter.assert_str())
AssertionChain(asserter.assert_str())
| asserter.assert_setattr(configuration, "entity_id"),
),
)
Expand Down Expand Up @@ -322,7 +322,7 @@ def load(
extension_type = asserter.assert_field(
RequiredField(
"extension",
Assertions(asserter.assert_extension_type()),
asserter.assert_extension_type(),
)
)(dump)
if configuration is None:
Expand All @@ -337,15 +337,13 @@ def load(
),
OptionalField(
"enabled",
Assertions(asserter.assert_bool())
AssertionChain(asserter.assert_bool())
| asserter.assert_setattr(configuration, "enabled"),
),
OptionalField(
"configuration",
Assertions(
configuration._assert_load_extension_configuration(
configuration.extension_type
)
configuration._assert_load_extension_configuration(
configuration.extension_type
),
),
)
Expand Down Expand Up @@ -521,7 +519,7 @@ def load(
entity_type = asserter.assert_field(
RequiredField[Any, type[Entity]](
"entity_type",
Assertions(asserter.assert_str()) | asserter.assert_entity_type(),
AssertionChain(asserter.assert_str()) | asserter.assert_entity_type(),
),
)(dump)
configuration = cls(entity_type)
Expand All @@ -532,7 +530,7 @@ def load(
),
OptionalField(
"generate_html_list",
Assertions(asserter.assert_bool())
AssertionChain(asserter.assert_bool())
| asserter.assert_setattr(configuration, "generate_html_list"),
),
)
Expand Down Expand Up @@ -668,7 +666,7 @@ def load(
) -> Self:
asserter = Asserter()
locale = asserter.assert_field(
RequiredField("locale", Assertions(asserter.assert_locale())),
RequiredField("locale", asserter.assert_locale()),
)(dump)
if configuration is None:
configuration = cls(locale)
Expand All @@ -677,7 +675,7 @@ def load(
RequiredField("locale"),
OptionalField(
"alias",
Assertions(asserter.assert_str())
AssertionChain(asserter.assert_str())
| asserter.assert_setattr(configuration, "alias"),
),
)
Expand Down Expand Up @@ -1039,59 +1037,59 @@ def load(
Fields(
OptionalField(
"name",
Assertions(asserter.assert_str())
AssertionChain(asserter.assert_str())
| asserter.assert_setattr(configuration, "name"),
),
RequiredField(
"base_url",
Assertions(asserter.assert_str())
AssertionChain(asserter.assert_str())
| asserter.assert_setattr(configuration, "base_url"),
),
OptionalField(
"title",
Assertions(asserter.assert_str())
AssertionChain(asserter.assert_str())
| asserter.assert_setattr(configuration, "title"),
),
OptionalField(
"author",
Assertions(asserter.assert_str())
AssertionChain(asserter.assert_str())
| asserter.assert_setattr(configuration, "author"),
),
OptionalField(
"root_path",
Assertions(asserter.assert_str())
AssertionChain(asserter.assert_str())
| asserter.assert_setattr(configuration, "root_path"),
),
OptionalField(
"clean_urls",
Assertions(asserter.assert_bool())
AssertionChain(asserter.assert_bool())
| asserter.assert_setattr(configuration, "clean_urls"),
),
OptionalField(
"debug",
Assertions(asserter.assert_bool())
AssertionChain(asserter.assert_bool())
| asserter.assert_setattr(configuration, "debug"),
),
OptionalField(
"lifetime_threshold",
Assertions(asserter.assert_int())
AssertionChain(asserter.assert_int())
| asserter.assert_setattr(configuration, "lifetime_threshold"),
),
OptionalField(
"locales",
Assertions(
AssertionChain(
configuration._locales.assert_load(configuration.locales)
),
),
OptionalField(
"extensions",
Assertions(
AssertionChain(
configuration._extensions.assert_load(configuration.extensions)
),
),
OptionalField(
"entity_types",
Assertions(
AssertionChain(
configuration._entity_types.assert_load(
configuration.entity_types
)
Expand Down
Loading
Loading