Skip to content

tests: add type annotations to transport tests#1684

Open
ZeliardM wants to merge 3 commits intopython-kasa:masterfrom
ZeliardM:tests/transport-type-annotations
Open

tests: add type annotations to transport tests#1684
ZeliardM wants to merge 3 commits intopython-kasa:masterfrom
ZeliardM:tests/transport-type-annotations

Conversation

@ZeliardM
Copy link
Copy Markdown
Contributor

@ZeliardM ZeliardM commented Apr 7, 2026

Summary

Add -> None return 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):

  • Add -> None return type annotations to all test functions and local helpers
  • Add parameter type annotations to all test function signatures (e.g., mocker: MockerFixture, caplog: pytest.LogCaptureFixture, expectation: AbstractContextManager, transport_class: type[KlapTransport], seed_auth_hash_calc: Callable[..., bytes], etc.)
  • Add missing imports: MockerFixture, AbstractContextManager, Callable, from __future__ import annotations

test_aestransport.py additional fixes:

  • Import KeyPairDict and type the aes_keys dict literal (fixes arg-type mypy error)
  • Replace invalid _handshake_done = True with _state = TransportState.ESTABLISHEDAesTransport uses _state, not _handshake_done
  • Add is not None assertion for Optional _key_pair access (fixes union-attr)

test_klaptransport.py additional fixes:

  • Keep a local transport variable (e.g. transport = KlapTransport(config=config)) before wrapping in protocol, instead of accessing private attrs through the BaseTransport-typed protocol._transport
  • Add is not None assertions for Optional _encryption_session in closures (fixes union-attr)
  • Remove unnecessary nonlocal declarations for variables that are only read, not rebound

Files 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.py

Merge order

This is PR 5 of 9 in the test modernization series. Must merge after #1683 (which removes a dead line in test_klaptransport.py in the same area).

Order PR Scope
1 #1677 Tests: cleanup and fixes
2 #1681 CI: pin GitHub Actions to SHA
3 #1682 Docs: modernize docstrings
4 #1683 Tests: centralize session cleanup
5 #1684 Tests: transport type annotations
6 #1685 Tests: IoT type annotations
7 #1686 Tests: Smart type annotations
8 #1687 Tests: CLI/protocols/smartcam type annotations
9 #1688 Tests: top-level type annotations

Merge note: If merging after #1683 produces a context conflict in test_klaptransport.py (test_handshake function), take this PR's version — it splits protocol = IotProtocol(transport=transport_class(config=config)) into separate transport/protocol variables and removes the dead http_client assignment that #1683 also removes.

Verification

  • mypy tests/transports/0 errors
  • All 287 transport tests pass
  • Pre-commit (ruff, ruff-format, mypy) all pass
  • Full test suite passes after sequential merge of all 9 PRs (10,656 passed, 194 skipped)

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.
Copilot AI review requested due to automatic review settings April 7, 2026 00:41
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 -> None return type annotations across 5 transport test files (tests + local helpers).
  • Fixed mypy/type issues in test_aestransport.py (typed aes_keys, correct internal state flag, Optional narrowing).
  • Refactored test_klaptransport.py to avoid private-attr access through BaseTransport-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
Copy link
Copy Markdown

codecov bot commented Apr 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.23%. Comparing base (76d9f68) to head (72c66d3).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…ype-annotations

# Conflicts:
#	tests/transports/test_klaptransport.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants