From 375211eeb26807fa0ed0600a0950a62ccb5338de Mon Sep 17 00:00:00 2001 From: Eddy ZHANG Date: Mon, 14 Sep 2026 23:56:07 +1000 Subject: [PATCH] test: enable mypy typing checks for test/components/converters/image (#12750) --- pyproject.toml | 2 +- .../image/test_document_to_image_content.py | 15 ++++++++------- .../converters/image/test_file_to_document.py | 7 ++++--- .../converters/image/test_file_to_image.py | 7 ++++--- .../converters/image/test_image_utils.py | 8 +++++--- .../converters/image/test_pdf_to_image.py | 9 ++++++--- 6 files changed, 28 insertions(+), 20 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index f1f6905102..2d396b7df1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" diff --git a/test/components/converters/image/test_document_to_image_content.py b/test/components/converters/image/test_document_to_image_content.py index 04844080f7..e1091b029a 100644 --- a/test/components/converters/image/test_document_to_image_content.py +++ b/test/components/converters/image/test_document_to_image_content.py @@ -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") diff --git a/test/components/converters/image/test_file_to_document.py b/test/components/converters/image/test_file_to_document.py index 9db8e4d2f9..bccb7c62a0 100644 --- a/test/components/converters/image/test_file_to_document.py +++ b/test/components/converters/image/test_file_to_document.py @@ -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 diff --git a/test/components/converters/image/test_file_to_image.py b/test/components/converters/image/test_file_to_image.py index 5bc81f84cd..69b0bf4e1b 100644 --- a/test/components/converters/image/test_file_to_image.py +++ b/test/components/converters/image/test_file_to_image.py @@ -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 diff --git a/test/components/converters/image/test_image_utils.py b/test/components/converters/image/test_image_utils.py index ac455f1642..1eb740702d 100644 --- a/test/components/converters/image/test_image_utils.py +++ b/test/components/converters/image/test_image_utils.py @@ -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")) diff --git a/test/components/converters/image/test_pdf_to_image.py b/test/components/converters/image/test_pdf_to_image.py index d90353a606..cb07706d7f 100644 --- a/test/components/converters/image/test_pdf_to_image.py +++ b/test/components/converters/image/test_pdf_to_image.py @@ -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