test: enable mypy typing checks for test/components/converters/image (#12750)

This commit is contained in:
Eddy ZHANG
2026-09-14 13:56:07 +00:00
committed by GitHub
parent 459f296ebd
commit 375211eeb2
6 changed files with 28 additions and 20 deletions
+1 -1
View File
@@ -150,7 +150,7 @@ all = 'pytest {args:test}'
e2e = 'pytest {args:e2e}'
# TODO We want to eventually type the whole test folder
types = "mypy --install-types --non-interactive --cache-dir=.mypy_cache/ {args:haystack test/core/ test/marshal/ test/testing/ test/tracing/ test/tools/ test/hooks/budget/ test/hooks/human_in_the_loop test/hooks/tool_result_offloading/ test/hooks/test_from_function.py test/hooks/test_invocation.py test/hooks/test_utils.py test/evaluation test/document_stores test/dataclasses test/utils/ test/fuzz/ test/skill_stores/ test/token_counters/ test/components/agents/test_state_class.py test/components/agents/test_agent_hooks.py test/components/agents/test_agent_hitl.py test/components/builders/ test/components/caching/ test/components/converters/test_csv_to_document.py test/components/converters/test_textfile_to_document.py test/components/converters/test_utils.py test/components/embedders/ test/components/evaluators/ test/components/extractors/ test/components/fetchers/ test/components/generators/ test/components/joiners/ test/components/preprocessors/ test/components/query/ test/components/rankers/ test/components/retrievers/ test/components/routers/ test/components/samplers/ test/components/validators/ test/components/writers/}"
types = "mypy --install-types --non-interactive --cache-dir=.mypy_cache/ {args:haystack test/core/ test/marshal/ test/testing/ test/tracing/ test/tools/ test/hooks/budget/ test/hooks/human_in_the_loop test/hooks/tool_result_offloading/ test/hooks/test_from_function.py test/hooks/test_invocation.py test/hooks/test_utils.py test/evaluation test/document_stores test/dataclasses test/utils/ test/fuzz/ test/skill_stores/ test/token_counters/ test/components/agents/test_state_class.py test/components/agents/test_agent_hooks.py test/components/agents/test_agent_hitl.py test/components/builders/ test/components/caching/ test/components/converters/image/ test/components/converters/test_csv_to_document.py test/components/converters/test_textfile_to_document.py test/components/converters/test_utils.py test/components/embedders/ test/components/evaluators/ test/components/extractors/ test/components/fetchers/ test/components/generators/ test/components/joiners/ test/components/preprocessors/ test/components/query/ test/components/rankers/ test/components/retrievers/ test/components/routers/ test/components/samplers/ test/components/validators/ test/components/writers/}"
[project.urls]
"CI: GitHub" = "https://github.com/deepset-ai/haystack/actions"
@@ -68,13 +68,13 @@ class TestDocumentToImageContent:
with pytest.raises(ValueError, match="has an unsupported MIME type"):
_ = converter.run(documents=[docx_doc])
def test_run_with_invalid_file_path(self, caplog) -> None:
def test_run_with_invalid_file_path(self, caplog: pytest.LogCaptureFixture) -> None:
converter = DocumentToImageContent()
pdf_doc = Document(content="test", meta={"file_path": "wrong_name.jpg"})
with pytest.raises(ValueError, match="has an invalid file path 'wrong_name.jpg'"):
_ = converter.run(documents=[pdf_doc])
def test_run_with_pdf_missing_page_number(self, caplog) -> None:
def test_run_with_pdf_missing_page_number(self, caplog: pytest.LogCaptureFixture) -> None:
converter = DocumentToImageContent()
pdf_doc = Document(content="test", meta={"file_path": "test/test_files/pdf/sample_pdf_1.pdf"})
with pytest.raises(ValueError, match="is missing the 'page_number' key"):
@@ -85,17 +85,18 @@ class TestDocumentToImageContent:
image_doc = Document(content="test", meta={"file_path": "apple.jpg"})
results = converter.run(documents=[image_doc])
assert len(results["image_contents"]) == 1
assert results["image_contents"][0].meta == {"file_path": "apple.jpg"}
image_content = results["image_contents"][0]
assert image_content is not None
assert image_content.meta == {"file_path": "apple.jpg"}
def test_run_with_pdf_documents(self) -> None:
converter = DocumentToImageContent()
pdf_doc = Document(content="test", meta={"file_path": "test/test_files/pdf/sample_pdf_1.pdf", "page_number": 1})
results = converter.run(documents=[pdf_doc])
assert len(results["image_contents"]) == 1
assert results["image_contents"][0].meta == {
"file_path": "test/test_files/pdf/sample_pdf_1.pdf",
"page_number": 1,
}
image_content = results["image_contents"][0]
assert image_content is not None
assert image_content.meta == {"file_path": "test/test_files/pdf/sample_pdf_1.pdf", "page_number": 1}
def test_run_with_mixed_document_types(self) -> None:
converter = DocumentToImageContent(root_path="test/test_files")
@@ -55,12 +55,13 @@ class TestImageFileToDocument:
assert len(results["documents"]) == 0
assert results == {"documents": []}
def test_run_with_invalid_source_type(self, caplog) -> None:
def test_run_with_invalid_source_type(self, caplog: pytest.LogCaptureFixture) -> None:
converter = ImageFileToDocument()
converter.run(sources=[123]) # Invalid source type
# The invalid source type is what this test exercises.
converter.run(sources=[123]) # type: ignore[list-item]
assert "Could not read" in caplog.text
def test_run_with_non_existent_file(self, caplog) -> None:
def test_run_with_non_existent_file(self, caplog: pytest.LogCaptureFixture) -> None:
converter = ImageFileToDocument()
converter.run(sources=["./non_existent_file.png"])
assert "Could not read" in caplog.text
@@ -65,12 +65,13 @@ class TestImageFileToImageContent:
assert len(results["image_contents"]) == 0
assert results == {"image_contents": []}
def test_run_with_invalid_source_type(self, caplog) -> None:
def test_run_with_invalid_source_type(self, caplog: pytest.LogCaptureFixture) -> None:
converter = ImageFileToImageContent()
converter.run(sources=[123]) # Invalid source type
# The invalid source type is what this test exercises.
converter.run(sources=[123]) # type: ignore[list-item]
assert "Could not read" in caplog.text
def test_run_with_non_existent_file(self, caplog) -> None:
def test_run_with_non_existent_file(self, caplog: pytest.LogCaptureFixture) -> None:
converter = ImageFileToImageContent()
converter.run(sources=["./non_existent_file.png"])
assert "Could not read" in caplog.text
@@ -51,9 +51,11 @@ class TestConvertPdfToImages:
pages_images = _convert_pdf_to_images(bytestream=bytestream, page_range=[1], size=(100, 100))
assert len(pages_images) == 1
assert pages_images[0][0] == 1
assert pages_images[0][1].width <= 100
assert pages_images[0][1].height <= 100
page_number, image = pages_images[0]
assert page_number == 1
assert isinstance(image, Image.Image)
assert image.width <= 100
assert image.height <= 100
def test_convert_pdf_to_images_invalid_page(self, caplog: LogCaptureFixture) -> None:
bytestream = get_bytestream_from_source(Path("test/test_files/pdf/sample_pdf_1.pdf"))
@@ -4,6 +4,8 @@
from pathlib import Path
import pytest
from haystack.components.converters.image.image_utils import _convert_pdf_to_images
from haystack.components.converters.image.pdf_to_image import PDFToImageContent
from haystack.core.serialization import component_from_dict, component_to_dict
@@ -60,12 +62,13 @@ class TestPDFToImageContent:
assert len(results["image_contents"]) == 0
assert results == {"image_contents": []}
def test_run_with_invalid_source_type(self, caplog) -> None:
def test_run_with_invalid_source_type(self, caplog: pytest.LogCaptureFixture) -> None:
converter = PDFToImageContent()
converter.run(sources=[123]) # Invalid source type
# The invalid source type is what this test exercises.
converter.run(sources=[123]) # type: ignore[list-item]
assert "Could not read" in caplog.text
def test_run_with_non_existent_file(self, caplog) -> None:
def test_run_with_non_existent_file(self, caplog: pytest.LogCaptureFixture) -> None:
converter = PDFToImageContent()
converter.run(sources=["./non_existent_file.png"])
assert "Could not read" in caplog.text