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

some error behaviors were lost and caused warnings #174

Merged
merged 1 commit into from
Oct 31, 2023
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
10 changes: 5 additions & 5 deletions coveo-functools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
```
Expand Down Expand Up @@ -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)
```

Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions coveo-functools/coveo_functools/flex/deserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions coveo-functools/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 16 additions & 11 deletions coveo-functools/tests_functools/test_flex_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)


Expand All @@ -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)


Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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"
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
Loading
Loading