tests: add type annotations to transport tests#1684
Open
ZeliardM wants to merge 3 commits intopython-kasa:masterfrom
Open
tests: add type annotations to transport tests#1684ZeliardM wants to merge 3 commits intopython-kasa:masterfrom
ZeliardM wants to merge 3 commits intopython-kasa:masterfrom
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.22% 93.23% +0.01%
==========================================
Files 157 157
Lines 9815 9815
Branches 1003 1003
==========================================
+ Hits 9150 9151 +1
+ Misses 472 471 -1
Partials 193 193 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
44644c3 to
16b35d0
Compare
This was referenced Apr 7, 2026
…ype-annotations # Conflicts: # tests/transports/test_klaptransport.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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