chore: import upstream snapshot with attribution
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
Docker image release / Build base image (push) Has been cancelled
Sync docs with Docusaurus / sync (push) Has been cancelled
Tests / Check if changed (push) Has been cancelled
Tests / format (push) Has been cancelled
Tests / check-imports (push) Has been cancelled
Tests / Unit / macos-latest (push) Has been cancelled
Tests / Unit / ubuntu-latest (push) Has been cancelled
Tests / Unit / windows-latest (push) Has been cancelled
Tests / mypy (push) Has been cancelled
Tests / Integration / ubuntu-latest (push) Has been cancelled
Tests / Integration / macos-latest (push) Has been cancelled
Tests / Integration / windows-latest (push) Has been cancelled
Tests / notify-slack-on-failure (push) Has been cancelled
Tests / Mark tests as completed (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
Docker image release / Build base image (push) Has been cancelled
Sync docs with Docusaurus / sync (push) Has been cancelled
Tests / Check if changed (push) Has been cancelled
Tests / format (push) Has been cancelled
Tests / check-imports (push) Has been cancelled
Tests / Unit / macos-latest (push) Has been cancelled
Tests / Unit / ubuntu-latest (push) Has been cancelled
Tests / Unit / windows-latest (push) Has been cancelled
Tests / mypy (push) Has been cancelled
Tests / Integration / ubuntu-latest (push) Has been cancelled
Tests / Integration / macos-latest (push) Has been cancelled
Tests / Integration / windows-latest (push) Has been cancelled
Tests / notify-slack-on-failure (push) Has been cancelled
Tests / Mark tests as completed (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import sys
|
||||
from unittest.mock import patch
|
||||
|
||||
from haystack.lazy_imports import LazyImport
|
||||
|
||||
|
||||
def test_lazy_importer_avoids_importing_unused_modules():
|
||||
# Save the original state of the module if it exists
|
||||
original_imported = sys.modules.get("haystack.components.generators.chat.azure")
|
||||
|
||||
with patch.dict(sys.modules):
|
||||
# Remove the module from sys.modules if it was already imported
|
||||
if original_imported:
|
||||
del sys.modules["haystack.components.generators.chat.azure"]
|
||||
|
||||
from haystack.components.generators.chat import OpenAIChatGenerator # Import the intended class # noqa: F401
|
||||
|
||||
assert "haystack.components.generators.chat.openai" in sys.modules.keys()
|
||||
assert "haystack.components.generators.chat.azure" not in sys.modules.keys()
|
||||
|
||||
# Restore the module if it was previously imported (preserves test isolation)
|
||||
if original_imported:
|
||||
sys.modules["haystack.components.generators.chat.azure"] = original_imported
|
||||
|
||||
|
||||
def test_import_error_is_suppressed_and_deferred():
|
||||
with LazyImport() as lazy_import:
|
||||
import a_module # noqa: F401
|
||||
|
||||
assert lazy_import._deferred is not None
|
||||
exc_value, message = lazy_import._deferred
|
||||
assert isinstance(exc_value, ImportError)
|
||||
expected_message = (
|
||||
"Haystack failed to import the optional dependency 'a_module'. Try 'pip install a_module'. "
|
||||
"Original error: No module named 'a_module'"
|
||||
)
|
||||
assert expected_message in message
|
||||
Reference in New Issue
Block a user