-
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.
feat: use importlib when deserializing callables
- Loading branch information
1 parent
3b9a60b
commit dcecb97
Showing
3 changed files
with
23 additions
and
7 deletions.
There are no files selected for viewing
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
6 changes: 6 additions & 0 deletions
6
releasenotes/notes/use-importlib-when-deserializing-callable-1f36f07c4518c2cf.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,6 @@ | ||
--- | ||
enhancements: | ||
- | | ||
Improved performance of deserializing callables by using `importlib` instead of `sys.modules`. | ||
This change allows importing local functions and classes that are not in `sys.modules` | ||
when deserializing callables. |
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,8 +1,10 @@ | ||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import pytest | ||
import requests | ||
|
||
from haystack import DeserializationError | ||
from haystack.components.generators.utils import print_streaming_chunk | ||
from haystack.utils import serialize_callable, deserialize_callable | ||
|
||
|
@@ -36,3 +38,10 @@ def test_callable_deserialization_non_local(): | |
result = serialize_callable(requests.api.get) | ||
fn = deserialize_callable(result) | ||
assert fn is requests.api.get | ||
|
||
|
||
def test_callable_deserialization_error(): | ||
with pytest.raises(DeserializationError): | ||
deserialize_callable("this.is.not.a.valid.module") | ||
with pytest.raises(DeserializationError): | ||
deserialize_callable("sys.foobar") |