-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: callables can be deserialized from fully qualified import path (#…
…8788) * fix: callables can be deserialized from fully qualified import path * fix: license header * fix: format * fix: types * fix? types * test: extend test case * format * add release notes
- Loading branch information
1 parent
379711f
commit 1a91365
Showing
4 changed files
with
43 additions
and
15 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
haystack/testing/callable_serialization/random_callable.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
|
||
def callable_to_deserialize(hello: str) -> str: | ||
""" | ||
A function to test callable deserialization. | ||
""" | ||
return f"{hello}, world!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
releasenotes/notes/fix-callable-deserialization-5a3ef204a8d07616.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
fixes: | ||
- | | ||
Callable deserialization now works for all fully qualified import paths. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import pytest | ||
import requests | ||
from haystack.core.errors import DeserializationError, SerializationError | ||
from haystack.components.generators.utils import print_streaming_chunk | ||
from haystack.testing.callable_serialization.random_callable import callable_to_deserialize | ||
from haystack.utils import serialize_callable, deserialize_callable | ||
|
||
|
||
|
@@ -40,6 +42,13 @@ def test_callable_serialization_non_local(): | |
assert result == "requests.api.get" | ||
|
||
|
||
def test_fully_qualified_import_deserialization(): | ||
func = deserialize_callable("haystack.testing.callable_serialization.random_callable.callable_to_deserialize") | ||
|
||
assert func is callable_to_deserialize | ||
assert func("Hello") == "Hello, world!" | ||
|
||
|
||
def test_callable_serialization_instance_methods_fail(): | ||
with pytest.raises(SerializationError): | ||
serialize_callable(TestClass.my_method) | ||
|