chore: Add type checking to test/tracing/, test/marshal/, and test/testing/#10913
Conversation
…sting/ Partially addresses deepset-ai#10396 by extending mypy type checking to three more test directories. Fixes type errors including missing annotations on pytest fixtures, unhandled Optional returns from current_span(), incorrect generator fixture return types, and dynamic LogRecord attribute access.
|
@Krishnachaitanyakc is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
|
Hi @Krishnachaitanyakc, thanks for opening this PR! Could you please sign the CLA? |
bogdankostic
left a comment
There was a problem hiding this comment.
Thanks for the PR @Krishnachaitanyakc! I left a couple of comments on how we can improve it.
There was a problem hiding this comment.
As this PR only touches our tests, we don't need a release note. Let's remove this file.
test/tracing/test_logging_tracer.py
Outdated
| ) | ||
| assert any( | ||
| record.operation_name == "haystack.pipeline.run" for record in records if hasattr(record, "operation_name") | ||
| getattr(record, "operation_name", None) == "haystack.pipeline.run" |
There was a problem hiding this comment.
| getattr(record, "operation_name", None) == "haystack.pipeline.run" | |
| getattr(record, "operation_name") == "haystack.pipeline.run" |
test/tracing/test_logging_tracer.py
Outdated
|
|
||
| assert any( | ||
| record.operation_name == "haystack.component.run" for record in records if hasattr(record, "operation_name") | ||
| getattr(record, "operation_name", None) == "haystack.component.run" |
There was a problem hiding this comment.
| getattr(record, "operation_name", None) == "haystack.component.run" | |
| getattr(record, "operation_name") == "haystack.component.run" |
test/tracing/test_logging_tracer.py
Outdated
| ) | ||
| assert any( | ||
| record.operation_name == "haystack.pipeline.run" for record in records if hasattr(record, "operation_name") | ||
| getattr(record, "operation_name", None) == "haystack.pipeline.run" |
There was a problem hiding this comment.
| getattr(record, "operation_name", None) == "haystack.pipeline.run" | |
| getattr(record, "operation_name") == "haystack.pipeline.run" |
test/tracing/test_opentelemetry.py
Outdated
| @@ -57,6 +57,7 @@ def test_current_span( | |||
| with tracer.trace("test"): | |||
| current_span = tracer.current_span() | |||
| assert tracer.current_span() is not None | |||
There was a problem hiding this comment.
| assert tracer.current_span() is not None |
test/tracing/test_tracer.py
Outdated
| current = tracer.current_span() | ||
| assert current is not None | ||
| assert isinstance(current.raw_span(), NullSpan) |
There was a problem hiding this comment.
| current = tracer.current_span() | |
| assert current is not None | |
| assert isinstance(current.raw_span(), NullSpan) | |
| current_span = tracer.current_span() | |
| assert current_span is not None | |
| assert isinstance(current_span.raw_span(), NullSpan) |
test/tracing/test_tracer.py
Outdated
| class TestAutoEnableTracer: | ||
| @pytest.fixture() | ||
| def configured_opentelemetry_tracing(self) -> None: | ||
| def configured_opentelemetry_tracing(self) -> Iterator[None]: |
There was a problem hiding this comment.
Let's use Generator type here.
| def configured_opentelemetry_tracing(self) -> Iterator[None]: | |
| def configured_opentelemetry_tracing(self) -> Generator[None]: |
The assertion on current_span already covers this check.
The hasattr guard already ensures the attribute exists, so direct access is safe. Added type: ignore comments since LogRecord doesn't declare operation_name in its type stubs.
bogdankostic
left a comment
There was a problem hiding this comment.
I made a few comments on how to make the typing checks pass. Also, let's add # type: ignore[func-returns-value] to test_factory.py.
- Remove unnecessary type: ignore[attr-defined] where hasattr guards exist - Add type: ignore[func-returns-value] to delete_documents assertion
bogdankostic
left a comment
There was a problem hiding this comment.
Looking good, thanks @Krishnachaitanyakc! :)
Related Issues
Proposed Changes:
Follow up to #10409 and #10428.
This PR extends mypy type checking coverage to three additional test directories:
test/tracing/,test/marshal/, andtest/testing/.Changes made:
pyproject.toml: Addedtest/marshal/,test/testing/, andtest/tracing/to the mypytypesscript targettest/tracing/test_logging_tracer.py: Addedpytestimport, type annotations forcaplogparameters (pytest.LogCaptureFixture), return type annotations for componentrunmethods, and# type: ignore[attr-defined]for dynamically-added LogRecord attributestest/tracing/test_datadog.py: Addedassert current_span is not Noneto narrowSpan | Nonetype before attribute accesstest/tracing/test_opentelemetry.py: SameNonenarrowing forcurrent_span()test/tracing/test_tracer.py: AddedIteratorimport, fixed generator fixture return type (None->Iterator[None]), addedNonenarrowing forcurrent_span()before callingraw_span()test/testing/test_factory.py: Changedassert store.delete_documents([]) is Nonetostore.delete_documents([])since the method is typed to returnNoneHow did you test it?
hatch run test:types-- 333 source files, 0 errorshatch run fmt-- all checks passedhatch run test:unit -- test/tracing/ test/testing/ test/marshal/-- 75 tests passedChecklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:and added!in case the PR includes breaking changes.