tests: add type annotations to transport tests#1684
Conversation
Add a single autouse fixture in tests/conftest.py that tracks all BaseTransport and HttpClient instances via __init__ monkeypatching, then closes them after each test. Uses functools.wraps to preserve original __init__ signatures so existing inspect-based tests continue to pass.
There was a problem hiding this comment.
Pull request overview
Modernizes the transport test suite by adding explicit return type annotations and making small typing-focused fixes to eliminate mypy errors.
Changes:
- Added
-> Nonereturn type annotations across 5 transport test files (tests + local helpers). - Fixed mypy/type issues in
test_aestransport.py(typedaes_keys, correct internal state flag, Optional narrowing). - Refactored
test_klaptransport.pyto avoid private-attr access throughBaseTransport-typed references and to add Optional narrowing assertions.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| tests/transports/test_aestransport.py | Adds return annotations and fixes mypy issues by typing aes_keys, using _state, and asserting _key_pair non-None before access. |
| tests/transports/test_klaptransport.py | Adds return annotations and refactors tests to use concrete transport variables + Optional narrowing assertions for _encryption_session. |
| tests/transports/test_linkietransport.py | Adds return annotations; updates helper to return str. |
| tests/transports/test_sslaestransport.py | Adds -> None annotations to async tests for consistency/type checking. |
| tests/transports/test_ssltransport.py | Adds -> None annotations to async tests for consistency/type checking. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1684 +/- ##
=======================================
Coverage 93.21% 93.21%
=======================================
Files 157 157
Lines 9819 9819
Branches 1005 1005
=======================================
Hits 9153 9153
Misses 472 472
Partials 194 194 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
44644c3 to
16b35d0
Compare
…ype-annotations # Conflicts: # tests/transports/test_klaptransport.py
|
There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. |
rytilahti
left a comment
There was a problem hiding this comment.
Really cool to see our tests having proper types! I left a couple of comments behind, but feel free to merge as you wish.
| config = DeviceConfig(host, credentials=Credentials("foo", "bar")) | ||
| transport = AesTransport(config=config) | ||
| transport._handshake_done = True | ||
| transport._state = TransportState.ESTABLISHED |
There was a problem hiding this comment.
Does this mean that _handshake_done could be removed altogether?
| async def test_protocol_retry_recoverable_error( | ||
| mocker, protocol_class, transport_class | ||
| ): | ||
| mocker: MockerFixture, protocol_class: type, transport_class: type |
There was a problem hiding this comment.
Can we constrain the types even more to be subclasses of the relevant interfaces?
| client_credentials = Credentials("foo", "bar") | ||
| device_auth_hash = transport_class.generate_auth_hash(device_credentials) | ||
|
|
||
| async def _return_handshake1_response(url, params=None, data=None, *_, **__): |
There was a problem hiding this comment.
Should this function also be moved to the top-level and typed accordingly?
| async def test_login_version_default_credentials( | ||
| mocker, login_version, expected_password_b64 | ||
| ): | ||
| mocker: MockerFixture, login_version: int | None, expected_password_b64: str |
There was a problem hiding this comment.
Maybe we should enforce passing the login_version across the code base to be explicit about it?
Summary
Add
-> Nonereturn type annotations and parameter type annotations to all test functions and helper functions across the 5 transport test files. This enables mypy to check function bodies, catching type errors that were previously hidden.Changes
All 5 transport test files (
test_aestransport.py,test_klaptransport.py,test_linkietransport.py,test_sslaestransport.py,test_ssltransport.py):-> Nonereturn type annotations to all test functions and local helpersmocker: MockerFixture,caplog: pytest.LogCaptureFixture,expectation: AbstractContextManager,transport_class: type[KlapTransport],seed_auth_hash_calc: Callable[..., bytes], etc.)MockerFixture,AbstractContextManager,Callable,from __future__ import annotationstest_aestransport.py additional fixes:
KeyPairDictand type theaes_keysdict literal (fixesarg-typemypy error)_handshake_done = Truewith_state = TransportState.ESTABLISHED—AesTransportuses_state, not_handshake_doneis not Noneassertion for Optional_key_pairaccess (fixesunion-attr)test_klaptransport.py additional fixes:
transportvariable (e.g.transport = KlapTransport(config=config)) before wrapping in protocol, instead of accessing private attrs through theBaseTransport-typedprotocol._transportis not Noneassertions for Optional_encryption_sessionin closures (fixesunion-attr)nonlocaldeclarations for variables that are only read, not reboundFiles changed (5)
tests/transports/test_aestransport.py,tests/transports/test_klaptransport.py,tests/transports/test_linkietransport.py,tests/transports/test_sslaestransport.py,tests/transports/test_ssltransport.pyMerge order
This is PR 5 of 9 in the test modernization series. Must merge after #1683 (which removes a dead line in
test_klaptransport.pyin the same area).Verification
mypy tests/transports/→ 0 errors