Skip to content

tests: add type annotations to Smart tests#1686

Open
ZeliardM wants to merge 1 commit intopython-kasa:masterfrom
ZeliardM:tests/smart-type-annotations
Open

tests: add type annotations to Smart tests#1686
ZeliardM wants to merge 1 commit intopython-kasa:masterfrom
ZeliardM:tests/smart-type-annotations

Conversation

@ZeliardM
Copy link
Copy Markdown
Contributor

@ZeliardM ZeliardM commented Apr 7, 2026

Summary

Add -> None return type annotations, parameter type annotations, and cast() for module dict lookups across all 30 smart test files. This enables mypy to check test function bodies, catching type errors that were previously hidden.

Changes

All 30 smart test files (tests/smart/features/, tests/smart/modules/, tests/smart/test_smartdevice.py):

  • Add -> None return type annotations to 95 test/helper functions
  • Add parameter type annotations to 62 parameters (e.g., dev: SmartDevice, mocker: MockerFixture, feature: str, type: type, mode: ThermostatState, etc.)
  • Add missing imports: SmartDevice, MockerFixture, AbstractContextManager, Any, cast

Module dict lookups — use cast() instead of # type: ignore:

  • Replace 17 var: Type = dev.modules[key] # type: ignore[assignment] patterns with var = cast(Type, dev.modules[key]) across 6 files (test_childlock, test_childprotection, test_humidity, test_temperature, test_temperaturecontrol, test_waterleak)
  • Consistent with existing cast() usage in test_smartdevice.py

Mypy fixes in test_smartdevice.py:

  • Merge two if raise_error: blocks so mypy can narrow SmartErrorCode | TimeoutError without # type: ignore[union-attr]
  • Remove incorrect -> None from _get_child_responses and two inner _query functions that return values

Docstring fix in test_firmware.py:

  • Fix copy/paste docstring: "Test light effect features.""Test firmware features."

Files changed (30)

tests/smart/features/test_brightness.py, tests/smart/features/test_colortemp.py, tests/smart/modules/test_alarm.py, tests/smart/modules/test_autooff.py, tests/smart/modules/test_childlock.py, tests/smart/modules/test_childprotection.py, tests/smart/modules/test_childsetup.py, tests/smart/modules/test_clean.py, tests/smart/modules/test_cleanrecords.py, tests/smart/modules/test_consumables.py, tests/smart/modules/test_contact.py, tests/smart/modules/test_dustbin.py, tests/smart/modules/test_energy.py, tests/smart/modules/test_fan.py, tests/smart/modules/test_firmware.py, tests/smart/modules/test_homekit.py, tests/smart/modules/test_humidity.py, tests/smart/modules/test_light_effect.py, tests/smart/modules/test_light_strip_effect.py, tests/smart/modules/test_lighttransition.py, tests/smart/modules/test_matter.py, tests/smart/modules/test_mop.py, tests/smart/modules/test_motionsensor.py, tests/smart/modules/test_powerprotection.py, tests/smart/modules/test_speaker.py, tests/smart/modules/test_temperature.py, tests/smart/modules/test_temperaturecontrol.py, tests/smart/modules/test_triggerlogs.py, tests/smart/modules/test_waterleak.py, tests/smart/test_smartdevice.py

Merge order

This is PR 7 of 9 in the test modernization series. No file overlaps with other PRs.

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

Verification

  • mypy tests/smart/0 errors
  • All 1,811 smart tests pass (1 pre-existing failure on master: D100C fixture)
  • Pre-commit (ruff, ruff-format, mypy) all pass
  • Full test suite passes after sequential merge of all 9 PRs (10,656 passed, 194 skipped)

Copilot AI review requested due to automatic review settings April 7, 2026 01:37
@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.22%. Comparing base (76d9f68) to head (0c82070).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1686   +/-   ##
=======================================
  Coverage   93.22%   93.22%           
=======================================
  Files         157      157           
  Lines        9815     9815           
  Branches     1003     1003           
=======================================
  Hits         9150     9150           
  Misses        472      472           
  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.

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

Incrementally modernizes the “smart” test suite by adding Python type annotations and replacing a few # type: ignore patterns with explicit typing.cast() to satisfy mypy.

Changes:

  • Added -> None return annotations and parameter type annotations across smart tests.
  • Replaced several module-dict access assignment ignores with cast(...) for clearer typing.
  • Added a targeted mypy suppression in test_smartdevice.py and corrected return annotations where functions actually return values.

Reviewed changes

Copilot reviewed 30 out of 30 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/smart/test_smartdevice.py Adds/adjusts type annotations for async tests/helpers and updates a mypy-related narrowing case.
tests/smart/modules/test_waterleak.py Adds SmartDevice typing and uses cast() for module lookups.
tests/smart/modules/test_triggerlogs.py Adds -> None return type for the async test.
tests/smart/modules/test_temperaturecontrol.py Adds SmartDevice typing and uses cast() for TemperatureControl module lookups.
tests/smart/modules/test_temperature.py Adds SmartDevice typing and uses cast() for TemperatureSensor module lookups.
tests/smart/modules/test_speaker.py Adds -> None return types and formats typed signatures.
tests/smart/modules/test_powerprotection.py Adds SmartDevice typing for feature tests and -> None returns elsewhere.
tests/smart/modules/test_motionsensor.py Adds parameter/return type annotations to the async test.
tests/smart/modules/test_mop.py Adds -> None returns and typed signatures for mop module tests.
tests/smart/modules/test_matter.py Adds -> None return type for the async test.
tests/smart/modules/test_lighttransition.py Adds -> None return types for async tests.
tests/smart/modules/test_light_strip_effect.py Adds -> None return types and signature formatting for async tests.
tests/smart/modules/test_light_effect.py Adds -> None return types and signature formatting for async tests.
tests/smart/modules/test_humidity.py Adds SmartDevice typing and uses cast() for HumiditySensor module lookups.
tests/smart/modules/test_homekit.py Adds -> None return type for the async test.
tests/smart/modules/test_firmware.py Adds richer parameter typing and context-manager typing for firmware tests.
tests/smart/modules/test_fan.py Adds -> None return types for fan module async tests.
tests/smart/modules/test_energy.py Adds -> None return types for energy module async tests.
tests/smart/modules/test_dustbin.py Adds -> None return types and typed signatures for dustbin tests.
tests/smart/modules/test_contact.py Adds parameter/return type annotations to the async test.
tests/smart/modules/test_consumables.py Adds -> None return types and typed signatures for consumables tests.
tests/smart/modules/test_cleanrecords.py Adds -> None return types and signature formatting.
tests/smart/modules/test_clean.py Adds -> None returns and parameter annotations across multiple async tests.
tests/smart/modules/test_childsetup.py Adds -> None return types and parameter annotations for child setup tests.
tests/smart/modules/test_childprotection.py Adds SmartDevice typing and uses cast() for module lookups.
tests/smart/modules/test_childlock.py Adds SmartDevice typing and uses cast() for module lookups.
tests/smart/modules/test_autooff.py Adds -> None return types and signature formatting.
tests/smart/modules/test_alarm.py Adds more explicit typing (e.g., Any, dict shapes) and -> None returns.
tests/smart/features/test_colortemp.py Adds -> None return type for the async feature test.
tests/smart/features/test_brightness.py Adds -> None return types for async feature tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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