mirror of
https://github.com/deepset-ai/haystack.git
synced 2026-10-12 02:12:50 +00:00
chore: remove unused imports and enforce related Ruff rule (#10278)
* chore: remove unused imports and enforce ruff rule * fix * enforce f401 rule * pylint fix
This commit is contained in:
@@ -3,9 +3,8 @@ import os
|
||||
import sys
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from haystack import logging # pylint: disable=unused-import # this is needed to avoid circular imports
|
||||
from haystack import logging # pylint: disable=unused-import # noqa: F401 # this is needed to avoid circular imports
|
||||
|
||||
|
||||
def validate_module_imports(root_dir: str, exclude_subdirs: list[str] | None = None) -> tuple[list, list]:
|
||||
|
||||
@@ -15,7 +15,7 @@ from haystack.core.pipeline import AsyncPipeline, Pipeline, PredefinedPipeline
|
||||
from haystack.core.serialization import default_from_dict, default_to_dict
|
||||
from haystack.core.super_component.super_component import SuperComponent, super_component
|
||||
from haystack.dataclasses import Answer, Document, ExtractedAnswer, GeneratedAnswer
|
||||
from haystack.version import __version__
|
||||
from haystack.version import __version__ # noqa: F401
|
||||
|
||||
# Initialize the logging configuration
|
||||
# This is a no-op unless `structlog` is installed
|
||||
|
||||
@@ -18,7 +18,7 @@ from haystack.utils.jinja2_extensions import _extract_template_variables_and_ass
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
with LazyImport("Run 'pip install \"arrow>=1.3.0\"'") as arrow_import:
|
||||
import arrow # pylint: disable=unused-import
|
||||
import arrow # pylint: disable=unused-import # noqa: F401
|
||||
|
||||
NO_TEXT_ERROR_MESSAGE = "ChatMessages from {role} role must contain text. Received ChatMessage with no text: {message}"
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ with LazyImport(
|
||||
"Image resizing will be applied, which requires the Pillow library. "
|
||||
"Run 'pip install pillow'"
|
||||
) as pillow_import:
|
||||
import PIL # pylint: disable=unused-import
|
||||
import PIL # pylint: disable=unused-import # noqa: F401
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -15,11 +15,13 @@ from haystack.lazy_imports import LazyImport
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
with LazyImport("Run 'pip install pandas openpyxl'") as pandas_xlsx_import:
|
||||
import openpyxl # pylint: disable=unused-import # the library is used but not directly referenced
|
||||
import openpyxl # pylint: disable=unused-import # noqa: F401 # the library is used but not directly referenced
|
||||
import pandas as pd
|
||||
|
||||
with LazyImport("Run 'pip install tabulate'") as tabulate_import:
|
||||
from tabulate import tabulate # pylint: disable=unused-import # the library is used but not directly referenced
|
||||
from tabulate import ( # pylint: disable=unused-import
|
||||
tabulate, # noqa: F401 # the library is used but not directly referenced
|
||||
)
|
||||
|
||||
|
||||
@component
|
||||
|
||||
@@ -18,7 +18,7 @@ if TYPE_CHECKING:
|
||||
from .named_entity_extractor import NamedEntityAnnotation as NamedEntityAnnotation
|
||||
from .named_entity_extractor import NamedEntityExtractor as NamedEntityExtractor
|
||||
from .named_entity_extractor import NamedEntityExtractorBackend as NamedEntityExtractorBackend
|
||||
from .regex_text_extractor import RegexTextExtractor
|
||||
from .regex_text_extractor import RegexTextExtractor as RegexTextExtractor
|
||||
|
||||
else:
|
||||
sys.modules[__name__] = LazyImporter(name=__name__, module_file=__file__, import_structure=_import_structure)
|
||||
|
||||
@@ -10,7 +10,7 @@ from lazy_imports import LazyImporter
|
||||
_import_structure = {"query_expander": ["QueryExpander"]}
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .query_expander import QueryExpander
|
||||
from .query_expander import QueryExpander as QueryExpander
|
||||
|
||||
else:
|
||||
sys.modules[__name__] = LazyImporter(name=__name__, module_file=__file__, import_structure=_import_structure)
|
||||
|
||||
@@ -11,7 +11,7 @@ from haystack.utils import ComponentDevice, DeviceMap, Secret, deserialize_secre
|
||||
from haystack.utils.hf import deserialize_hf_model_kwargs, resolve_hf_device_map, serialize_hf_model_kwargs
|
||||
|
||||
with LazyImport(message="Run 'pip install transformers[torch,sentencepiece]'") as torch_and_transformers_import:
|
||||
import accelerate # pylint: disable=unused-import # the library is used but not directly referenced
|
||||
import accelerate # pylint: disable=unused-import # noqa: F401 # the library is used but not directly referenced
|
||||
import torch
|
||||
from torch.utils.data import DataLoader, Dataset
|
||||
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
||||
|
||||
@@ -12,7 +12,7 @@ from haystack.utils import ComponentDevice, DeviceMap, Secret, deserialize_secre
|
||||
from haystack.utils.hf import deserialize_hf_model_kwargs, resolve_hf_device_map, serialize_hf_model_kwargs
|
||||
|
||||
with LazyImport("Run 'pip install transformers[torch,sentencepiece]'") as torch_and_transformers_import:
|
||||
import accelerate # pylint: disable=unused-import # the library is used but not directly referenced
|
||||
import accelerate # pylint: disable=unused-import # noqa: F401 # the library is used but not directly referenced
|
||||
import torch
|
||||
from tokenizers import Encoding
|
||||
from transformers import AutoModelForQuestionAnswering, AutoTokenizer
|
||||
|
||||
@@ -15,7 +15,7 @@ from haystack.dataclasses import ByteStream
|
||||
from haystack.utils.misc import _guess_mime_type # ruff: isort: skip
|
||||
|
||||
# We import CUSTOM_MIMETYPES here to prevent breaking change from moving to haystack.utils.misc
|
||||
from haystack.utils.misc import CUSTOM_MIMETYPES # pylint: disable=unused-import
|
||||
from haystack.utils.misc import CUSTOM_MIMETYPES # pylint: disable=unused-import # noqa: F401
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -2,4 +2,6 @@
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# ruff: noqa: F401
|
||||
|
||||
from haystack.telemetry._telemetry import pipeline_running, tutorial_running
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
# NOTE: we do not use LazyImporter here because it creates conflicts between the tool module and the tool decorator
|
||||
|
||||
# ruff: noqa: I001 (ignore import order as we need to import Tool before ComponentTool and PipelineTool)
|
||||
from typing import Union
|
||||
|
||||
from haystack.tools.from_function import create_tool_from_function, tool
|
||||
from haystack.tools.tool import Tool, _check_duplicate_tool_names
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# ruff: noqa: F401
|
||||
|
||||
from haystack.tracing.tracer import ( # noqa: I001 (otherwise we end up with partial imports)
|
||||
Span,
|
||||
Tracer,
|
||||
|
||||
@@ -349,7 +349,6 @@ select = [
|
||||
]
|
||||
|
||||
ignore = [
|
||||
"F401", # unused-import
|
||||
"PERF203", # `try`-`except` within a loop incurs performance overhead
|
||||
"PERF401", # Use a list comprehension to create a transformed list
|
||||
"PLR1714", # repeated-equality-comparison
|
||||
|
||||
@@ -6,7 +6,7 @@ import logging
|
||||
import os
|
||||
import re
|
||||
from datetime import datetime
|
||||
from typing import Any, Iterator, Optional, Union
|
||||
from typing import Any, Iterator
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
import os
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
from typing import Any, Optional, Union
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ import os
|
||||
import re
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import logging
|
||||
from typing import Any, Optional, Union
|
||||
from typing import Any
|
||||
|
||||
import arrow
|
||||
import pytest
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import logging
|
||||
from typing import Any, Optional
|
||||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
import arrow
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
import logging
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
from haystack.components.converters.txt import TextFileToDocument
|
||||
from haystack.dataclasses import ByteStream
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import logging
|
||||
from typing import Union
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import os
|
||||
import random
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
import json
|
||||
import os
|
||||
from typing import Any, Optional
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from openai import OpenAIError
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
import json
|
||||
import os
|
||||
from typing import Any, Optional
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from openai import OpenAIError
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
from typing import Any, Optional
|
||||
from typing import Any
|
||||
from urllib.error import HTTPError as URLLibHTTPError
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import asyncio
|
||||
import gc
|
||||
from typing import Optional
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import pytest
|
||||
@@ -12,12 +10,11 @@ from transformers import PreTrainedTokenizer
|
||||
|
||||
from haystack.components.generators.chat import HuggingFaceLocalChatGenerator
|
||||
from haystack.dataclasses import ChatMessage, ChatRole, ToolCall
|
||||
from haystack.dataclasses.streaming_chunk import AsyncStreamingCallbackT, StreamingChunk
|
||||
from haystack.dataclasses.streaming_chunk import StreamingChunk
|
||||
from haystack.tools import Tool
|
||||
from haystack.tools.toolset import Toolset
|
||||
from haystack.utils import ComponentDevice
|
||||
from haystack.utils.auth import Secret
|
||||
from haystack.utils.hf import AsyncHFTokenStreamingHandler
|
||||
|
||||
|
||||
# used to test serialization of streaming_callback
|
||||
|
||||
@@ -6,7 +6,7 @@ import json
|
||||
import logging
|
||||
import os
|
||||
from datetime import datetime
|
||||
from typing import Any, Optional
|
||||
from typing import Any
|
||||
from unittest.mock import ANY, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
import json
|
||||
import os
|
||||
from typing import Any, Optional
|
||||
from typing import Any
|
||||
from unittest.mock import ANY, MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
from unittest.mock import Mock, patch
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from openai.types import ImagesResponse
|
||||
|
||||
@@ -4,9 +4,8 @@
|
||||
|
||||
import pytest
|
||||
|
||||
from haystack import Document, Pipeline
|
||||
from haystack import Document
|
||||
from haystack.components.preprocessors import HierarchicalDocumentSplitter
|
||||
from haystack.components.retrievers import InMemoryBM25Retriever
|
||||
from haystack.components.retrievers.auto_merging_retriever import AutoMergingRetriever
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
|
||||
|
||||
@@ -6,11 +6,10 @@ from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from haystack import AsyncPipeline, DeserializationError, Pipeline
|
||||
from haystack import AsyncPipeline
|
||||
from haystack.components.retrievers.filter_retriever import FilterRetriever
|
||||
from haystack.dataclasses import Document
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.testing.factory import document_store_class
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
|
||||
@@ -7,7 +7,7 @@ import re
|
||||
|
||||
import pytest
|
||||
|
||||
from haystack import Document, Pipeline
|
||||
from haystack import Document
|
||||
from haystack.components.preprocessors import DocumentSplitter
|
||||
from haystack.components.retrievers import InMemoryBM25Retriever
|
||||
from haystack.components.retrievers.sentence_window_retriever import SentenceWindowRetriever
|
||||
|
||||
@@ -2,9 +2,6 @@
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from haystack import Pipeline
|
||||
|
||||
@@ -9,9 +9,7 @@ from pathlib import PosixPath
|
||||
from unittest.mock import mock_open, patch
|
||||
|
||||
import pytest
|
||||
from packaging import version
|
||||
|
||||
import haystack
|
||||
from haystack import Pipeline
|
||||
from haystack.components.converters import PyPDFToDocument, TextFileToDocument
|
||||
from haystack.components.routers.file_type_router import FileTypeRouter
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
from typing import Union
|
||||
|
||||
import pytest
|
||||
|
||||
from haystack import Pipeline
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
import datetime
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pydantic
|
||||
|
||||
@@ -6,7 +6,7 @@ import asyncio
|
||||
import re
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from typing import Any, Union
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from pandas import DataFrame
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
import json
|
||||
import re
|
||||
from copy import deepcopy
|
||||
from typing import Any, Optional, Union
|
||||
from typing import Any
|
||||
from unittest.mock import ANY
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -15,7 +15,7 @@ from haystack.core.pipeline.utils import (
|
||||
args_deprecated,
|
||||
parse_connect_string,
|
||||
)
|
||||
from haystack.tools import ComponentTool, Tool
|
||||
from haystack.tools import ComponentTool
|
||||
|
||||
|
||||
def get_weather_report(city: str) -> str:
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
from typing import Any, Union
|
||||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -5,8 +5,10 @@
|
||||
import sys
|
||||
from unittest.mock import patch
|
||||
|
||||
from haystack.lazy_imports import LazyImport
|
||||
|
||||
def test_lazy_import():
|
||||
|
||||
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")
|
||||
|
||||
@@ -15,7 +17,7 @@ def test_lazy_import():
|
||||
if original_imported:
|
||||
del sys.modules["haystack.components.generators.chat.azure"]
|
||||
|
||||
from haystack.components.generators.chat import OpenAIChatGenerator # Import the intended class
|
||||
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()
|
||||
@@ -23,3 +25,17 @@ def test_lazy_import():
|
||||
# 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
|
||||
@@ -1,22 +0,0 @@
|
||||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <[email protected]>
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import pytest
|
||||
|
||||
from haystack.lazy_imports import DEFAULT_IMPORT_ERROR_MSG, LazyImport
|
||||
|
||||
|
||||
class TestLazyImport:
|
||||
def test_import_error_is_suppressed_and_deferred(self):
|
||||
with LazyImport() as lazy_import:
|
||||
import a_module
|
||||
|
||||
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
|
||||
@@ -2,8 +2,6 @@
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import pytest
|
||||
|
||||
from haystack.core.component import component
|
||||
from haystack.dataclasses import Document
|
||||
from haystack.testing.factory import component_class, document_store_class
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
from typing import Annotated, Literal, Optional
|
||||
from typing import Annotated, Literal
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@@ -5,9 +5,8 @@
|
||||
import pytest
|
||||
|
||||
from haystack.components.agents import Agent
|
||||
from haystack.components.tools import ToolInvoker
|
||||
from haystack.core.component.component import component
|
||||
from haystack.tools import Tool, Toolset, warm_up_tools
|
||||
from haystack.tools import Tool, Toolset
|
||||
from haystack.tools.toolset import _ToolsetWrapper
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user