diff --git a/coveo-functools/README.md b/coveo-functools/README.md index d7a9515..db314ab 100644 --- a/coveo-functools/README.md +++ b/coveo-functools/README.md @@ -37,7 +37,7 @@ class Person: social_number: Optional[int] = None # the deserializer is used directly to receive a list of Person instances -response = flex.deserialize(json.load(), hint=List[Person]) +response = flex.deserialize(json.load(), hint=List[Person], errors='raise') ``` Automatic usage example: @@ -229,7 +229,7 @@ class WithDateTime: timestamp = datetime.utcnow() -instance = flex.deserialize({"timestamp": timestamp.isoformat()}, hint=WithDateTime) +instance = flex.deserialize({"timestamp": timestamp.isoformat()}, hint=WithDateTime, errors='raise') assert instance.timestamp == timestamp assert isinstance(instance.timestamp, datetime) ``` @@ -278,7 +278,7 @@ class Parent: meta = SerializationMetadata.from_instance(Parent(Concrete())) -parent = deserialize({"nested": {}}, hint=meta) +parent = deserialize({"nested": {}}, hint=meta, errors='raise') assert isinstance(parent.nested, Concrete) ``` @@ -397,8 +397,8 @@ payload = { "Id": "GgfhAs89876yh.z" } -transaction = flex.deserialize(payload, hint=Transaction) -all_transactions = flex.deserialize([payload, payload], hint=List[Transaction]) +transaction = flex.deserialize(payload, hint=Transaction, errors='raise') +all_transactions = flex.deserialize([payload, payload], hint=List[Transaction], errors='raise') ``` Interesting details: diff --git a/coveo-functools/coveo_functools/flex/deserializer.py b/coveo-functools/coveo_functools/flex/deserializer.py index 8489824..8e8971e 100644 --- a/coveo-functools/coveo_functools/flex/deserializer.py +++ b/coveo-functools/coveo_functools/flex/deserializer.py @@ -441,7 +441,7 @@ def _deserialize_with_metadata( # or an instance thereof. # Here, we take a shortcut to deserialize `value` into an instance of `SerializationMetadata`. # This happens when flex is also used to serialize the metadata headers. - return hint(**convert_kwargs_for_unpacking(value, hint=hint)) # type: ignore[misc] + return hint(**convert_kwargs_for_unpacking(value, hint=hint, errors=errors)) # type: ignore[misc] root_type = hint.import_type() @@ -462,7 +462,7 @@ def _deserialize_with_metadata( if isclass(root_type) and isinstance(value, dict): # typical case of unpacking value into an instance of the root type. return root_type( - **convert_kwargs_for_unpacking(value, hint=hint) + **convert_kwargs_for_unpacking(value, hint=hint, errors=errors) ) # it's magic! # type: ignore[no-any-return] if root_type is not SerializationMetadata: diff --git a/coveo-functools/poetry.lock b/coveo-functools/poetry.lock index 4c8d549..d1afda0 100644 --- a/coveo-functools/poetry.lock +++ b/coveo-functools/poetry.lock @@ -452,6 +452,7 @@ files = [ {file = "PyYAML-6.0.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:69b023b2b4daa7548bcfbd4aa3da05b3a74b772db9e23b982788168117739938"}, {file = "PyYAML-6.0.1-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:81e0b275a9ecc9c0c0c07b4b90ba548307583c125f54d5b6946cfee6360c733d"}, {file = "PyYAML-6.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ba336e390cd8e4d1739f42dfe9bb83a3cc2e80f567d8805e11b46f4a943f5515"}, + {file = "PyYAML-6.0.1-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:326c013efe8048858a6d312ddd31d56e468118ad4cdeda36c719bf5bb6192290"}, {file = "PyYAML-6.0.1-cp310-cp310-win32.whl", hash = "sha256:bd4af7373a854424dabd882decdc5579653d7868b8fb26dc7d0e99f823aa5924"}, {file = "PyYAML-6.0.1-cp310-cp310-win_amd64.whl", hash = "sha256:fd1592b3fdf65fff2ad0004b5e363300ef59ced41c2e6b3a99d4089fa8c5435d"}, {file = "PyYAML-6.0.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:6965a7bc3cf88e5a1c3bd2e0b5c22f8d677dc88a455344035f03399034eb3007"}, @@ -459,8 +460,15 @@ files = [ {file = "PyYAML-6.0.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:42f8152b8dbc4fe7d96729ec2b99c7097d656dc1213a3229ca5383f973a5ed6d"}, {file = "PyYAML-6.0.1-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:062582fca9fabdd2c8b54a3ef1c978d786e0f6b3a1510e0ac93ef59e0ddae2bc"}, {file = "PyYAML-6.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d2b04aac4d386b172d5b9692e2d2da8de7bfb6c387fa4f801fbf6fb2e6ba4673"}, + {file = "PyYAML-6.0.1-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:e7d73685e87afe9f3b36c799222440d6cf362062f78be1013661b00c5c6f678b"}, {file = "PyYAML-6.0.1-cp311-cp311-win32.whl", hash = "sha256:1635fd110e8d85d55237ab316b5b011de701ea0f29d07611174a1b42f1444741"}, {file = "PyYAML-6.0.1-cp311-cp311-win_amd64.whl", hash = "sha256:bf07ee2fef7014951eeb99f56f39c9bb4af143d8aa3c21b1677805985307da34"}, + {file = "PyYAML-6.0.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:855fb52b0dc35af121542a76b9a84f8d1cd886ea97c84703eaa6d88e37a2ad28"}, + {file = "PyYAML-6.0.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:40df9b996c2b73138957fe23a16a4f0ba614f4c0efce1e9406a184b6d07fa3a9"}, + {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6c22bec3fbe2524cde73d7ada88f6566758a8f7227bfbf93a408a9d86bcc12a0"}, + {file = "PyYAML-6.0.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:8d4e9c88387b0f5c7d5f281e55304de64cf7f9c0021a3525bd3b1c542da3b0e4"}, + {file = "PyYAML-6.0.1-cp312-cp312-win32.whl", hash = "sha256:d483d2cdf104e7c9fa60c544d92981f12ad66a457afae824d146093b8c294c54"}, + {file = "PyYAML-6.0.1-cp312-cp312-win_amd64.whl", hash = "sha256:0d3304d8c0adc42be59c5f8a4d9e3d7379e6955ad754aa9d6ab7a398b59dd1df"}, {file = "PyYAML-6.0.1-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:50550eb667afee136e9a77d6dc71ae76a44df8b3e51e41b77f6de2932bfe0f47"}, {file = "PyYAML-6.0.1-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:1fe35611261b29bd1de0070f0b2f47cb6ff71fa6595c077e42bd0c419fa27b98"}, {file = "PyYAML-6.0.1-cp36-cp36m-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:704219a11b772aea0d8ecd7058d0082713c3562b4e271b849ad7dc4a5c90c13c"}, @@ -477,6 +485,7 @@ files = [ {file = "PyYAML-6.0.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a0cd17c15d3bb3fa06978b4e8958dcdc6e0174ccea823003a106c7d4d7899ac5"}, {file = "PyYAML-6.0.1-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:28c119d996beec18c05208a8bd78cbe4007878c6dd15091efb73a30e90539696"}, {file = "PyYAML-6.0.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7e07cbde391ba96ab58e532ff4803f79c4129397514e1413a7dc761ccd755735"}, + {file = "PyYAML-6.0.1-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:49a183be227561de579b4a36efbb21b3eab9651dd81b1858589f796549873dd6"}, {file = "PyYAML-6.0.1-cp38-cp38-win32.whl", hash = "sha256:184c5108a2aca3c5b3d3bf9395d50893a7ab82a38004c8f61c258d4428e80206"}, {file = "PyYAML-6.0.1-cp38-cp38-win_amd64.whl", hash = "sha256:1e2722cc9fbb45d9b87631ac70924c11d3a401b2d7f410cc0e3bbf249f2dca62"}, {file = "PyYAML-6.0.1-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:9eb6caa9a297fc2c2fb8862bc5370d0303ddba53ba97e71f08023b6cd73d16a8"}, @@ -484,6 +493,7 @@ files = [ {file = "PyYAML-6.0.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:5773183b6446b2c99bb77e77595dd486303b4faab2b086e7b17bc6bef28865f6"}, {file = "PyYAML-6.0.1-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:b786eecbdf8499b9ca1d697215862083bd6d2a99965554781d0d8d1ad31e13a0"}, {file = "PyYAML-6.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:bc1bf2925a1ecd43da378f4db9e4f799775d6367bdb94671027b73b393a7c42c"}, + {file = "PyYAML-6.0.1-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:04ac92ad1925b2cff1db0cfebffb6ffc43457495c9b3c39d3fcae417d7125dc5"}, {file = "PyYAML-6.0.1-cp39-cp39-win32.whl", hash = "sha256:faca3bdcf85b2fc05d06ff3fbc1f83e1391b3e724afa3feba7d13eeab355484c"}, {file = "PyYAML-6.0.1-cp39-cp39-win_amd64.whl", hash = "sha256:510c9deebc5c0225e8c96813043e62b680ba2f9c50a08d3724c7f28a747d1486"}, {file = "PyYAML-6.0.1.tar.gz", hash = "sha256:bfdf460b1736c775f2ba9f6a92bca30bc2095067b8a9d77876d1fad6cc3b4a43"}, diff --git a/coveo-functools/tests_functools/test_flex_adapters.py b/coveo-functools/tests_functools/test_flex_adapters.py index 7179f36..2c89d14 100644 --- a/coveo-functools/tests_functools/test_flex_adapters.py +++ b/coveo-functools/tests_functools/test_flex_adapters.py @@ -49,7 +49,7 @@ class Parent: def test_flex_raise_on_abstract() -> None: with pytest.raises(UnsupportedAnnotation): - deserialize({}, hint=Abstract) + deserialize({}, hint=Abstract, errors="raise") @parametrize("implementation_class", (Implementation, DataclassImplementation)) @@ -58,7 +58,7 @@ def adapter(value: Any) -> Type: return implementation_class register_subclass_adapter(Abstract, adapter) - parent = deserialize({"test": {}}, hint=Parent) + parent = deserialize({"test": {}}, hint=Parent, errors="raise") assert isinstance(parent.test, implementation_class) @@ -71,7 +71,7 @@ class Nested: nested: Parent register_subclass_adapter(Abstract, adapter) - instance = deserialize({"nested": {"test": {}}}, hint=Nested) + instance = deserialize({"nested": {"test": {}}}, hint=Nested, errors="raise") assert isinstance(instance.nested.test, Implementation) @@ -82,7 +82,11 @@ def adapter(value: Any) -> Type: return List[str] register_subclass_adapter(Abstract, adapter) - assert deserialize({"test": ["a", "b", "c"]}, hint=Parent).test == ["a", "b", "c"] + assert deserialize({"test": ["a", "b", "c"]}, hint=Parent, errors="raise").test == [ + "a", + "b", + "c", + ] def test_deserialize_any_adapter() -> None: @@ -101,14 +105,14 @@ def abstract_adapter(value: Any) -> TypeHint: register_subclass_adapter(Any, any_adapter) register_subclass_adapter(Abstract, abstract_adapter) - instance: Any = deserialize([{}, {}, {}], hint=Any) + instance: Any = deserialize([{}, {}, {}], hint=Any, errors="raise") assert ( isinstance(instance, list) and instance and all(isinstance(item, Implementation) for item in instance) ) - instance = deserialize({"item1": {}, "item2": {}}, hint=Any) + instance = deserialize({"item1": {}, "item2": {}}, hint=Any, errors="raise") assert isinstance(instance, dict) and instance assert isinstance(instance["item1"], Implementation) assert isinstance(instance["item2"], Implementation) @@ -131,7 +135,7 @@ def abstract_adapter(value: Any) -> TypeHint: # the payload will be mutated payload: Dict[str, Any] = {} - instance = deserialize(payload, hint=Parent) + instance = deserialize(payload, hint=Parent, errors="raise") assert "test" in payload assert isinstance(instance.test, Implementation) assert instance.test.value == "success" @@ -143,7 +147,7 @@ class TestFactory: @classmethod def factory(cls, raw: Dict[str, str]) -> TestFactory: - return deserialize(raw, hint=TestFactory) + return deserialize(raw, hint=TestFactory, errors="raise") def test_deserialize_adapter_factory_classmethod() -> None: @@ -155,7 +159,7 @@ def factory_adapter(value: Any) -> TypeHint: register_subclass_adapter(TestFactory, factory_adapter) payload: Dict[str, Any] = {"raw": {"value": "success"}} - assert deserialize(payload, hint=TestFactory).value == "success" + assert deserialize(payload, hint=TestFactory, errors="raise").value == "success" @dataclass @@ -174,7 +178,7 @@ def test_deserialize_adapter_factory_function() -> None: """Tests the factory feature, which expects the instance to be returned instead of the type.""" test_datetime = datetime.utcnow() payload: Dict[str, Any] = {"value": test_datetime.isoformat()} - assert deserialize(payload, hint=TestDatetimeFactory).value == test_datetime + assert deserialize(payload, hint=TestDatetimeFactory, errors="raise").value == test_datetime def test_deserialize_with_meta_and_factory() -> None: @@ -183,6 +187,7 @@ def test_deserialize_with_meta_and_factory() -> None: instance = TestDatetimeFactory(value=timestamp) meta = SerializationMetadata.from_instance(instance) deserialized = cast( - TestDatetimeFactory, deserialize({"value": timestamp.isoformat()}, hint=meta) + TestDatetimeFactory, + deserialize({"value": timestamp.isoformat()}, hint=meta, errors="raise"), ) assert deserialized.value == timestamp diff --git a/coveo-functools/tests_functools/test_flex_deserializer.py b/coveo-functools/tests_functools/test_flex_deserializer.py index 797fa37..43007ad 100644 --- a/coveo-functools/tests_functools/test_flex_deserializer.py +++ b/coveo-functools/tests_functools/test_flex_deserializer.py @@ -78,7 +78,7 @@ class MockIntEnum(int, Enum): ), ) def test_deserialize_to_list(hint: Any, payload: Any, expected: Any) -> None: - assert deserialize(payload, hint=hint) == expected + assert deserialize(payload, hint=hint, errors="raise") == expected @UnitTest @@ -86,7 +86,7 @@ def test_deserialize_unions_passthrough() -> None: """Anything from the json types will be given back without checking; this allows unions of base types.""" union = Union[JSON_TYPES] # type: ignore[valid-type] - assert deserialize(DEFAULT_VALUE, hint=union) == DEFAULT_VALUE + assert deserialize(DEFAULT_VALUE, hint=union, errors="raise") == DEFAULT_VALUE @UnitTest @@ -96,7 +96,9 @@ def test_deserialize_unions_limited() -> None: This may change in the future. """ with pytest.raises(UnsupportedAnnotation): - assert deserialize(DEFAULT_VALUE, hint=Union[str, MockType]) == DEFAULT_VALUE + assert ( + deserialize(DEFAULT_VALUE, hint=Union[str, MockType], errors="raise") == DEFAULT_VALUE + ) @UnitTest @@ -116,7 +118,7 @@ def test_deserialize_thing_or_list_of_things(hint: Type, payload: Any, expected: Some APIs will return a single map if there's one object, else a list of them. Such objects must be annotated with Union[Thing, List[Thing]]. """ - assert deserialize(payload, hint=hint) == expected + assert deserialize(payload, hint=hint, errors="raise") == expected @UnitTest @@ -133,7 +135,7 @@ def test_deserialize_thing_or_list_of_things(hint: Type, payload: Any, expected: ) def test_deserialize_dict(hint: Type) -> None: """We don't do much with'em, but they gotta work!""" - assert deserialize(DEFAULT_PAYLOAD, hint=hint) == DEFAULT_PAYLOAD + assert deserialize(DEFAULT_PAYLOAD, hint=hint, errors="raise") == DEFAULT_PAYLOAD @UnitTest @@ -146,7 +148,7 @@ def test_deserialize_dict(hint: Type) -> None: ), ) def test_deserialize_dict_with_custom_value_type(hint: Any) -> None: - assert deserialize(DEFAULT_MAP_PAYLOAD, hint=hint) == DEFAULT_MAP_MOCK + assert deserialize(DEFAULT_MAP_PAYLOAD, hint=hint, errors="raise") == DEFAULT_MAP_MOCK @UnitTest @@ -184,14 +186,14 @@ def test_deserialize_dict_with_custom_value_type(hint: Any) -> None: ), ) def test_deserialize_dict_complex(hint: Any, payload: Any, expected: Any) -> None: - assert deserialize(payload, hint=hint) == expected + assert deserialize(payload, hint=hint, errors="raise") == expected @UnitTest def test_deserialize_dict_invalid_union() -> None: """Make sure the union rules are respected in the dict value annotation.""" with pytest.raises(UnsupportedAnnotation): - _ = deserialize(DEFAULT_PAYLOAD, hint=Dict[str, Union[str, MockType]]) + _ = deserialize(DEFAULT_PAYLOAD, hint=Dict[str, Union[str, MockType]], errors="raise") @UnitTest @@ -214,7 +216,7 @@ def test_deserialize_dict_invalid_union() -> None: def test_deserialize_enum_with_data_type( value: str, hint: Union[Type[MockEnum], Type[MockIntEnum], Type[MockStrEnum]] ) -> None: - assert deserialize(value, hint=hint) is hint.TestKey + assert deserialize(value, hint=hint, errors="raise") is hint.TestKey @parametrize( @@ -251,12 +253,16 @@ def test_deserialize_enum_nested() -> None: class SomeClass: test: MockEnum - assert deserialize({"Test": "test.key"}, hint=SomeClass).test is MockEnum.TestKey + assert ( + deserialize({"Test": "test.key"}, hint=SomeClass, errors="raise").test is MockEnum.TestKey + ) @UnitTest def test_deserialize_enum_list() -> None: - assert deserialize(["test-value", "TestKey", "otherkey"], hint=List[MockEnum]) == [ + assert deserialize( + ["test-value", "TestKey", "otherkey"], hint=List[MockEnum], errors="raise" + ) == [ MockEnum.TestKey, MockEnum.TestKey, MockEnum.OtherKey, @@ -270,8 +276,8 @@ class SomeEnum(Enum): Task = Job Status = "status" - assert deserialize("job", hint=SomeEnum) is SomeEnum.Job - assert deserialize("task", hint=SomeEnum) is SomeEnum.Task + assert deserialize("job", hint=SomeEnum, errors="raise") is SomeEnum.Job + assert deserialize("task", hint=SomeEnum, errors="raise") is SomeEnum.Task assert SomeEnum.Job is SomeEnum.Task # it's the same picture. @@ -282,7 +288,7 @@ class SubclassImmutable(immutable_type): # type: ignore[valid-type,misc] def builtin_type(self) -> Type: return immutable_type - value = deserialize(1, hint=SubclassImmutable) + value = deserialize(1, hint=SubclassImmutable, errors="raise") assert isinstance(value, immutable_type) assert isinstance(value, SubclassImmutable) assert value.builtin_type is immutable_type @@ -369,36 +375,42 @@ def fn(value: str) -> str: def correct_annotations() -> None: """cases that correctly follow annotations""" - assert deserialize(DEFAULT_PAYLOAD, hint=MockType).value == DEFAULT_VALUE - assert deserialize([DEFAULT_PAYLOAD], hint=List[MockType])[0].value == DEFAULT_VALUE - assert deserialize(DEFAULT_PAYLOAD, hint=Dict[str, Any])["value"] == DEFAULT_VALUE - assert deserialize(fn, hint=fn)(value=DEFAULT_VALUE) == DEFAULT_VALUE + assert deserialize(DEFAULT_PAYLOAD, hint=MockType, errors="raise").value == DEFAULT_VALUE + assert ( + deserialize([DEFAULT_PAYLOAD], hint=List[MockType], errors="raise")[0].value + == DEFAULT_VALUE + ) + assert ( + deserialize(DEFAULT_PAYLOAD, hint=Dict[str, Any], errors="raise")["value"] + == DEFAULT_VALUE + ) + assert deserialize(fn, hint=fn, errors="ignore")(value=DEFAULT_VALUE) == DEFAULT_VALUE correct_annotations() def broken_annotations() -> None: # noqa """we don't execute this one, because it would fail.""" # mypy sees that we may not get a list - _ = deserialize(DEFAULT_PAYLOAD, hint=Union[List[MockType], MockType])[0].value # type: ignore[index] + _ = deserialize(DEFAULT_PAYLOAD, hint=Union[List[MockType], MockType], errors="raise")[0].value # type: ignore[index] # ...same as above, reversed. - _ = deserialize(DEFAULT_PAYLOAD, hint=Union[List[MockType], MockType]).value # type: ignore[attr-defined] + _ = deserialize(DEFAULT_PAYLOAD, hint=Union[List[MockType], MockType], errors="raise").value # type: ignore[attr-defined] # mypy rightfully complains about None not having a `.value` - _ = deserialize(DEFAULT_PAYLOAD, hint=Optional[MockType]).value # type: ignore[attr-defined] + _ = deserialize(DEFAULT_PAYLOAD, hint=Optional[MockType], errors="raise").value # type: ignore[attr-defined] # mypy rightfully complains about MockType not having a `.name` - _ = deserialize(DEFAULT_PAYLOAD, hint=Optional[MockType]).name # type: ignore[attr-defined] + _ = deserialize(DEFAULT_PAYLOAD, hint=Optional[MockType], errors="raise").name # type: ignore[attr-defined] def test_return() -> str: # noqa # mypy rightfully complains about returning `Any` instead of str - return deserialize(DEFAULT_PAYLOAD, hint=MockType).name # type: ignore[attr-defined,no-any-return] + return deserialize(DEFAULT_PAYLOAD, hint=MockType, errors="raise").name # type: ignore[attr-defined,no-any-return] # mypy sees that fn accepts a str, not an int - _ = deserialize(fn, hint=fn)(fn=1) # type:ignore[call-arg] + _ = deserialize(fn, hint=fn, errors="raise")(fn=1) # type:ignore[call-arg] # mypy sees that fn returns a str, not an int - _ = deserialize(fn, hint=fn)(value="") / 2 # type: ignore[operator] + _ = deserialize(fn, hint=fn, errors="raise")(value="") / 2 # type: ignore[operator] # the type of the payload doesn't fit the hint diff --git a/coveo-functools/tests_functools/test_flex_serializer.py b/coveo-functools/tests_functools/test_flex_serializer.py index 1a55cd2..e34567b 100644 --- a/coveo-functools/tests_functools/test_flex_serializer.py +++ b/coveo-functools/tests_functools/test_flex_serializer.py @@ -56,11 +56,11 @@ class MockOptionalList: def test_serialization_metadata() -> None: meta = SerializationMetadata.from_instance(MockWithAbstract(MockSubClass("test"))) - assert deserialize({"extra": {"value": "test"}}, hint=meta).extra.value == "test" # type: ignore[attr-defined] + assert deserialize({"extra": {"value": "test"}}, hint=meta, errors="raise").extra.value == "test" # type: ignore[attr-defined] # it cannot work without the meta because of the abstract class with pytest.raises(Exception): - deserialize({"extra": {"value": "test"}}, hint=MockWithAbstract) + deserialize({"extra": {"value": "test"}}, hint=MockWithAbstract, errors="raise") def test_serialization_recursive() -> None: @@ -72,7 +72,7 @@ def test_serialization_recursive() -> None: meta = SerializationMetadata.from_instance( MockWithNested(MockWithAbstract(MockSubClass("test"))) ) - assert deserialize(payload, hint=meta).root.extra.value == "test" # type: ignore[attr-defined] + assert deserialize(payload, hint=meta, errors="raise").root.extra.value == "test" # type: ignore[attr-defined] def test_serialization_enum() -> None: @@ -80,7 +80,7 @@ def test_serialization_enum() -> None: instance = MockSubClass("test", MockEnum.Value) meta = SerializationMetadata.from_instance(instance) payload = {"value": "test", "enum_test": "Value"} - assert deserialize(payload, hint=meta).enum_test is MockEnum.Value # type: ignore[attr-defined] + assert deserialize(payload, hint=meta, errors="raise").enum_test is MockEnum.Value # type: ignore[attr-defined] def test_serialization_abstract_in_list() -> None: @@ -88,9 +88,11 @@ def test_serialization_abstract_in_list() -> None: meta = SerializationMetadata.from_instance(lst) with pytest.raises(Exception): - deserialize([{"value": "first"}, {"value": "second"}], hint=List[AbstractClass]) + deserialize( + [{"value": "first"}, {"value": "second"}], hint=List[AbstractClass], errors="raise" + ) - result = deserialize([{"value": "first"}, {"value": "second"}], hint=meta) + result = deserialize([{"value": "first"}, {"value": "second"}], hint=meta, errors="raise") assert result[0].value == "first" and result[1].value == "second" # type: ignore[index] assert isinstance(result[0], MockSubClass) # type: ignore[index] assert isinstance(result[1], MockSubClass2) # type: ignore[index] @@ -103,9 +105,11 @@ def test_serialization_abstract_in_dict() -> None: meta = SerializationMetadata.from_instance(dct) with pytest.raises(Exception): - deserialize(payload, hint=Dict[str, AbstractClass]) + deserialize(payload, hint=Dict[str, AbstractClass], errors="raise") - result = deserialize({"first": {"value": "first"}, "second": {"value": "second"}}, hint=meta) + result = deserialize( + {"first": {"value": "first"}, "second": {"value": "second"}}, hint=meta, errors="raise" + ) assert result["first"].value == "first" and result["second"].value == "second" # type: ignore[index] assert isinstance(result["first"], MockSubClass) # type: ignore[index] assert isinstance(result["second"], MockSubClass2) # type: ignore[index] @@ -114,14 +118,14 @@ def test_serialization_abstract_in_dict() -> None: def test_serialization_none_in_list() -> None: lst = MockOptionalList([1, 2, None, 4]) meta = SerializationMetadata.from_instance(lst) - result = deserialize({"value": [1, 2, None, 4]}, hint=meta) + result = deserialize({"value": [1, 2, None, 4]}, hint=meta, errors="raise") assert result.value == [1, 2, None, 4] # type: ignore[attr-defined] def test_serialization_none_in_custom_object() -> None: obj = MockOptionalList(None) meta = SerializationMetadata.from_instance(obj) - result = deserialize({"value": None}, hint=meta) + result = deserialize({"value": None}, hint=meta, errors="raise") assert result.value is None # type: ignore[attr-defined] @@ -130,5 +134,5 @@ def test_serialization_can_serialize_serialization_metadata() -> None: instance = SerializationMetadata("patate", "poire") meta = SerializationMetadata.from_instance(instance) payload = asdict(instance) - assert deserialize(payload, hint=SerializationMetadata).module_name == "patate" - assert deserialize(payload, hint=meta).module_name == "patate" + assert deserialize(payload, hint=SerializationMetadata, errors="raise").module_name == "patate" + assert deserialize(payload, hint=meta, errors="raise").module_name == "patate"