Skip to content

Commit

Permalink
expose TypedpyDefaults. version 2.27.6 . Ensure handling of compact s…
Browse files Browse the repository at this point in the history
…erialization correctly.
  • Loading branch information
loyada committed Feb 27, 2024
1 parent a24592c commit b4085dd
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 11 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
license="MIT",
long_description=long_description,
url="http://github.com/loyada/typedpy",
download_url="https://github.com/loyada/typedpy/archive/v2.27.5.tar.gz",
download_url="https://github.com/loyada/typedpy/archive/v2.27.6.tar.gz",
keywords=["testing", "type-safe", "strict", "schema", "validation"],
version="2.27.5",
version="2.27.6",
)

# coverage run --source=typedpy/ setup.py test
Expand Down
1 change: 0 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def fixture_fail_on_additional_props_in_deserialization():
TypedPyDefaults.ignore_invalid_additional_properties_in_deserialization = True



@pytest.fixture(name="compact_serialization")
def fixture_compact_serialization():
Structure.set_compact_serialization_default(True)
Expand Down
9 changes: 8 additions & 1 deletion tests/test_deserialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,14 @@ class Foo(Structure):
b: int
_required = []

Deserializer(Foo).deserialize({"c": 1, "a": 2})
assert Deserializer(Foo).deserialize({"c": 1, "a": 2}) == Foo(a=2)


def test_additional_properties_turned_off_input_is_not_dict(additional_props_default_is_false, compact_serialization):
class Foo(Structure):
a: list[int]

assert Deserializer(Foo).deserialize([1,2]) == Foo(a=[1,2])


def test_additional_properties_turned_off_err_throw(
Expand Down
1 change: 1 addition & 0 deletions typedpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
Extend,
keys_of,
)
from typedpy.structures.defaults import TypedPyDefaults

from .json_schema import (
structure_to_schema,
Expand Down
13 changes: 7 additions & 6 deletions typedpy/serialization/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,14 +804,14 @@ class reference field. Users are not supposed to use this argument.

ignore_none = getattr(cls, IGNORE_NONE_VALUES, False)
field_by_name = cls.get_all_fields_by_name()

props = cls.__dict__
additional_props = props.get(
ADDITIONAL_PROPERTIES, TypedPyDefaults.additional_properties_default
)
if not isinstance(input_dict, dict):
props = cls.__dict__
fields = list(field_by_name.keys())
required = props.get(REQUIRED_FIELDS, fields)
additional_props = props.get(
ADDITIONAL_PROPERTIES, TypedPyDefaults.additional_properties_default
)

if (
len(fields) == 1
and required == fields
Expand All @@ -833,7 +833,8 @@ class reference field. Users are not supposed to use this argument.
k: v
for k, v in input_dict.items()
if k not in field_by_name
and keep_undefined
and keep_undefined and
(additional_props is True or not TypedPyDefaults.ignore_invalid_additional_properties_in_deserialization)
and k not in getattr(cls, "_constants", [])
}

Expand Down
2 changes: 1 addition & 1 deletion typedpy/serialization/serialization_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def deserialize(
adjusted_keep_undefined = (
keep_undefined
if keep_undefined is not None or additional_props_allowed
else not TypedPyDefaults.ignore_invalid_additional_properties_in_deserialization
else True
)
return deserialize_structure(
self.target_class,
Expand Down

0 comments on commit b4085dd

Please sign in to comment.