Skip to content

test: type-check test/utils and add it to mypy config - #11728

Merged
sjrl merged 12 commits into
deepset-ai:mainfrom
mustafakhokhar:test/typecheck-test-utils
Jul 1, 2026
Merged

test: type-check test/utils and add it to mypy config#11728
sjrl merged 12 commits into
deepset-ai:mainfrom
mustafakhokhar:test/typecheck-test-utils

Conversation

@mustafakhokhar

@mustafakhokhar mustafakhokhar commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

A first incremental step toward typing the whole test suite, as suggested in #10396 ("do it in a piece-meal approach where we only turn on type checking in a small part of tests and grow from there").

  • Add test/utils/ to the test:types mypy paths in pyproject.toml.
  • Make test/utils pass mypy, preferring real fixes over # type: ignore: proper type annotations, assert isinstance(...) narrowing, and correcting mis-typed test values.
  • Fix the init_http_client overloads in haystack/utils/http_client.py to carry defaults (per review). The previous overloads omitted defaults, so calling it with no/partial arguments matched no overload. This is a typing-only change — the implementation and its runtime behaviour are unchanged — and it lets the corresponding test ignores be removed.
  • # type: ignore remains only where a test intentionally passes invalid input (e.g. a non-dict http_client_kwargs, error=None) or monkeypatches a method — each scoped to the exact error code.

How did you test it?

  • hatch run test:types → clean (now also type-checks test/utils).
  • hatch run test:unit test/utils/ → passes.
  • hatch run fmt and pre-commit are clean.

Notes for the reviewer

  • The only non-test change is the init_http_client overload signatures (typing only, no runtime behaviour change), so no release note is included (the reno CI check passes).
  • Addressed all review feedback: corrected the overloads, replaced the attr-defined ignores in test_auth with isinstance narrowing, and gave test_hf's tool_result its correct str type.
  • If you run the suite on Python 3.14, you may see one unrelated, pre-existing failure in test_type_serialization (a Union repr change) — independent of this PR.

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • Added/updated type annotations and typing fixes; no runtime behaviour change.
  • I've used a conventional commit type for my PR title (test:).
  • I have documented my code.
  • Release note — N/A: typing-only change, no runtime behaviour (the reno check passes).
  • I have run pre-commit hooks and fixed any issue.

Implemented with the help of an AI assistant; I have reviewed all changes and run the relevant tests and quality checks.

Enable mypy type checking for the test/utils directory (part of deepset-ai#10396) as a
first piece-meal step toward typing the whole test suite.

Fix the errors mypy surfaced across 11 files using real annotations where
possible (annotating expected values, adding return types, narrowing
float | None with asserts, annotating mock parameters). Where a test
intentionally passes invalid input (inside pytest.raises) or monkeypatches a
method, a scoped and commented type: ignore is used. No test behaviour is
changed.

Add test/utils/ to the test:types mypy paths in pyproject.toml.
@mustafakhokhar
mustafakhokhar requested a review from a team as a code owner June 23, 2026 08:16
@mustafakhokhar
mustafakhokhar requested review from sjrl and removed request for a team June 23, 2026 08:16
@vercel

vercel Bot commented Jun 23, 2026

Copy link
Copy Markdown

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

A member of the Team first needs to authorize it.

@github-actions

github-actions Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/utils
  http_client.py
Project Total  

This report was generated by python-coverage-comment-action

@mustafakhokhar

mustafakhokhar commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

Hi @sjrl - gentle ping on this whenever you have a moment. It's a first piece-meal slice of #10396 (type-checking test/utils); CI is green and it merges cleanly. Happy to adjust anything you'd like changed. 🙏

@sjrl

sjrl commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Hey @mustafakhokhar thanks for opening the PR! I'll go ahead and give a review of this, this morning.

Comment thread test/utils/test_http_client.py Outdated
Comment thread test/utils/test_auth.py Outdated
Comment thread test/utils/test_auth.py Outdated
Comment thread test/utils/test_hf.py Outdated
Address review feedback on the test/utils type-checking work:
- Fix the init_http_client overloads to carry defaults, so calling it with
  no/partial arguments matches an overload; remove the now-unnecessary
  call-overload ignores in the test.
- test_auth: narrow with assert isinstance(secret, EnvVarSecret) instead of
  ignoring attr-defined on private-attribute access.
- test_hf: pass a correctly-typed str tool_result instead of a dict, removing
  the arg-type ignores.
@mustafakhokhar

Copy link
Copy Markdown
Contributor Author

Thanks for the review @sjrl, all addressed 🙏

  • init_http_client overloads: applied your suggested signatures (defaults on both overloads); removed the 4 now-unnecessary call-overload ignores in the test (kept only the one on the intentionally-invalid "invalid" string, which is still a genuine type error).
  • test_auth: added assert isinstance(secret, EnvVarSecret) to narrow the type instead of the attr-defined ignores.
  • test_hf: tool_result is now the correctly-typed JSON string instead of a dict, so the arg-type ignores are gone.

hatch run test:types and the unit tests are green, and I've rebased on main. PTAL!

Comment thread test/utils/test_type_serialization.py
Comment thread test/utils/test_type_serialization.py
Revert the type(None) -> None change in test_type_serialization: those tests
specifically verify NoneType as a typing-generic argument, so keep type(None)
and use a scoped # type: ignore[misc] instead (intent-preserving fix).
@mustafakhokhar

Copy link
Copy Markdown
Contributor Author

Reverted 👍 — restored type(None) in those Dict/List subscriptions (agreed, these tests are specifically about NoneType as a type argument), and kept a scoped # type: ignore[misc] there instead. Pushed.

@sjrl sjrl 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.

Thanks!

@sjrl
sjrl merged commit 7fdd1a8 into deepset-ai:main Jul 1, 2026
24 of 25 checks passed
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.

2 participants