Skip to content

test: enable mypy typing checks for test/components/extractors/ - #12343

Open
abhati27 wants to merge 1 commit into
deepset-ai:mainfrom
abhati27:test-mypy-extractors
Open

test: enable mypy typing checks for test/components/extractors/#12343
abhati27 wants to merge 1 commit into
deepset-ai:mainfrom
abhati27:test-mypy-extractors

Conversation

@abhati27

Copy link
Copy Markdown

Related Issues

(Deliberately not a closing keyword — #10396 tracks ~19 directories and only this one is covered here. It got auto-closed by #12272 and had to be reopened, so flagging it.)

Proposed Changes:

Adds test/components/extractors/ to the types target in pyproject.toml and fixes the six type errors it surfaced. No changes to haystack source — test typing fixes plus the allowlist entry only.

  • test_regex_text_extractor.py — annotate the empty message list as list[ChatMessage], and ignore the arg-type on the call that deliberately passes a list[str] to assert the TypeError.
  • test_llm_metadata_extractor.py — ignore the call-arg on the constructor call that deliberately omits chat_generator to assert the TypeError, and narrow _chat_generator with isinstance before calling to_dict(), since the ChatGenerator protocol does not declare it.
  • image/test_llm_document_content_extractor.py — the same call-arg ignore for the missing-chat_generator case, and bind Document.content to a local before len() so the str | None is narrowed.

How did you test it?

$ hatch run test:types
Success: no issues found in 432 source files

$ hatch run test:unit test/components/extractors/
85 passed, 5 deselected in 5.39s

$ hatch run fmt
All checks passed!
6321 files left unchanged

hatch run test:types is the full CI target, not just the new directory.

Notes for the reviewer

One non-mechanical change worth a look: in test_from_dict_openai, the assertion is on extractor._chat_generator.to_dict(), but the attribute is typed as the ChatGenerator protocol, which doesn't declare to_dict. Rather than silence it, I narrowed with assert isinstance(extractor._chat_generator, OpenAIChatGenerator). That satisfies mypy and also pins that from_dict rebuilt an OpenAIChatGenerator, which is what the test is named for — the protocol-vs-implementation gap this issue was opened to surface.

The three type: ignores are all deliberate negative tests, and each sits directly under the pytest.raises(TypeError) that explains it — consistent with how #12318 (joiners) and #11859 (embedders) handled the same situation.

Checklist

Adds test/components/extractors/ to the mypy target in pyproject.toml and fixes
the six type errors it surfaced. No changes to haystack source.

- test_regex_text_extractor.py: annotate the empty message list, and ignore the
  arg-type on the call that deliberately passes a list[str] to assert the
  TypeError.
- test_llm_metadata_extractor.py: ignore the call-arg on the constructor call
  that deliberately omits chat_generator to assert the TypeError, and narrow
  _chat_generator with isinstance before calling to_dict(), since the
  ChatGenerator protocol does not declare it. The narrowing also pins that
  from_dict rebuilt an OpenAIChatGenerator, which is what the test is for.
- image/test_llm_document_content_extractor.py: the same call-arg ignore for the
  missing-chat_generator case, and bind Document.content to a local before len()
  so the str | None is narrowed.

Each ignore sits directly under the pytest.raises that explains it, matching the
approach in the joiners and embedders increments.
@abhati27
abhati27 requested a review from a team as a code owner August 13, 2026 23:02
@abhati27
abhati27 requested review from davidsbatista and a lite review from Copilot and removed request for a team August 13, 2026 23:02
@vercel

vercel Bot commented Aug 13, 2026

Copy link
Copy Markdown

@abhati27 is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant

CLAassistant commented Aug 13, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

  • Extends the test:types mypy target to include test/components/extractors/ and applies small typing-only adjustments in extractor tests so mypy passes without touching haystack runtime code.

Changes:

  • Add test/components/extractors/ to the types script target list in pyproject.toml.
  • Fix mypy errors in extractor tests via explicit annotations, narrowing, and scoped # type: ignore[...] for deliberate negative tests.
  • Strengthen one test assertion by pinning from_dict() to rebuild an OpenAIChatGenerator instance.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
pyproject.toml Adds test/components/extractors/ to the types (mypy) target paths.
test/components/extractors/test_regex_text_extractor.py Annotates an empty messages list as list[ChatMessage] and scopes an ignore for an intentional type error test.
test/components/extractors/test_llm_metadata_extractor.py Adds a scoped ignore for an intentional constructor misuse and narrows _chat_generator before calling implementation-specific APIs.
test/components/extractors/image/test_llm_document_content_extractor.py Adds a scoped ignore for an intentional constructor misuse and narrows Document.content before len().

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines 69 to +71
def test_init_fails_without_chat_generator(self):
with pytest.raises(TypeError):
LLMDocumentContentExtractor()
LLMDocumentContentExtractor() # type: ignore[call-arg]
Comment on lines 99 to +102
with pytest.raises(TypeError):
_ = LLMMetadataExtractor(prompt="prompt {{document.content}}", expected_keys=["key1", "key2"])
_ = LLMMetadataExtractor( # type: ignore[call-arg]
prompt="prompt {{document.content}}", expected_keys=["key1", "key2"]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants