Skip to content

Commit

Permalink
refactoring thaw function
Browse files Browse the repository at this point in the history
  • Loading branch information
sbilge committed Dec 10, 2024
1 parent ffe71e0 commit d6bc336
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 34 deletions.
7 changes: 3 additions & 4 deletions src/schemapack/_internals/spec/custom_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

"""Custom types annotations used for type hinting."""

from collections.abc import Mapping, Sequence
from typing import Annotated, Any, TypeAlias
from typing import Annotated, TypeAlias

from arcticfreeze import FrozenDict
from pydantic import Field as _Field
Expand All @@ -28,5 +27,5 @@
RelationPropertyName: TypeAlias = _NonEmptyStr
ContentPropertyName: TypeAlias = _NonEmptyStr
IdPropertyName: TypeAlias = _NonEmptyStr
FrozenType: TypeAlias = FrozenDict[str, Any] | frozenset
ThawedType: TypeAlias = Mapping | Sequence
FrozenType: TypeAlias = FrozenDict[str, str | FrozenDict | tuple]
ThawedType: TypeAlias = dict[str, str | dict | list]
39 changes: 11 additions & 28 deletions src/schemapack/_internals/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def assert_valid_json_schema(schema: Mapping[str, Any]) -> None:
schema
)
if isinstance(schema, FrozenDict):
schema = _thaw_content(schema) # type: ignore
schema = thaw_frozendict(schema)
try:
cls.check_schema(schema)
except jsonschema.exceptions.SchemaError as error:
Expand Down Expand Up @@ -112,33 +112,16 @@ def model_to_serializable_dict(
return json.loads(model.model_dump_json(exclude_defaults=True))


def thaw_frozendict(value: FrozenDict[str, Any]) -> ThawedType:
"""Fn."""
return (
{key: thaw_frozendict(val) for key, val in value.items()}
if isinstance(value, FrozenDict)
else value
)


def thaw_frozenset(value: frozenset) -> ThawedType:
"""Fn"""
return (
[thaw_frozenset(item) for item in value]
if isinstance(value, frozenset)
else value
)


def _thaw_content(frozen_thing: FrozenType) -> ThawedType:
"""Converts a nested FrozenDict or frozenset to mutable types. Frozenset is only
seen as a nested structure within the frozen content schema.
"""
if isinstance(frozen_thing, Mapping):
return thaw_frozendict(frozen_thing)
elif isinstance(frozen_thing, frozenset):
return thaw_frozenset(frozen_thing)
return frozen_thing
def thaw_frozendict(value: FrozenType) -> ThawedType:
"""Recursively thaws a FrozenDict into a standard dictionary."""
return {
key: thaw_frozendict(val)
if isinstance(val, FrozenDict)
else list(val)
if isinstance(val, tuple)
else val
for key, val in value.items()
}


def dumps_model(
Expand Down
12 changes: 10 additions & 2 deletions tests/fixtures/test_content_schema_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,16 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": False,
"description": "A dataset that is a collection of files.",
"properties": FrozenDict({"dac_contact": FrozenDict({"type": "string"})}),
"description": "A file is an object that contains information generated from a process, either an Experiment or an Analysis.",
"properties": FrozenDict(
{
"checksum": FrozenDict({"type": "string"}),
"filename": FrozenDict({"type": "string"}),
"format": FrozenDict({"type": "string"}),
"size": FrozenDict({"type": "integer"}),
}
),
"required": ("filename", "format", "checksum", "size"),
"type": "object",
}
)
Expand Down

0 comments on commit d6bc336

Please sign in to comment.