Skip to content

tests: add type annotations to IoT tests#1685

Open
ZeliardM wants to merge 2 commits intopython-kasa:masterfrom
ZeliardM:tests/iot-type-annotations
Open

tests: add type annotations to IoT tests#1685
ZeliardM wants to merge 2 commits intopython-kasa:masterfrom
ZeliardM:tests/iot-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 across 8 files in tests/iot/, improving type safety and enabling mypy to check function bodies.

Changes

Annotations added (71 functions across 8 files):

  • tests/iot/test_iotdevice.py — 23 functions
  • tests/iot/test_iotdimmer.py — 11 functions
  • tests/iot/test_iotlightstrip.py — 7 functions
  • tests/iot/test_iotstrip.py — 3 functions
  • tests/iot/test_iottimezone.py — 13 functions
  • tests/iot/test_wallswitch.py — 1 function
  • tests/iot/modules/test_emeter.py — 10 functions
  • tests/iot/modules/test_usage.py — 3 functions

Parameter type annotations added to all test functions:

  • Device params typed to specific classes (e.g., dev: IotDevice, dev: IotDimmer, dev: IotLightStrip, dev: IotStrip, dev: IotWallSwitch)
  • Test infrastructure params typed (e.g., mocker: MockerFixture, turn_on: bool, brightness: int, transition: int)
  • Added missing imports: MockerFixture, IotStrip, IotWallSwitch, IotStripPlug, IotDevice

Mypy fixes (exposed by the new annotations):

  • Annotate reassigned dest/source dicts in test_merge_dict as dict to prevent narrowed-type conflicts on reassignment
  • Annotate emeter_data dicts in test_emeter_daily, test_usage_today, and test_usage_this_month as dict to allow .append()/.extend() on nested lists (union with int from err_code)
  • Replace None with Mock() in Usage() constructor call (arg-type)
  • Add list[dict[str, int]] annotation to test_data variable (var-annotated)
  • Add isinstance narrowing for IotStripPlug children access

Files changed (8)

tests/iot/test_iotdevice.py, tests/iot/test_iotdimmer.py, tests/iot/test_iotlightstrip.py, tests/iot/test_iotstrip.py, tests/iot/test_iottimezone.py, tests/iot/test_wallswitch.py, tests/iot/modules/test_emeter.py, tests/iot/modules/test_usage.py

Merge order

This is PR 6 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/iot/0 errors
  • All 3,243 IoT tests pass (222 skipped)
  • 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 00:52
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

This PR improves static type checking for the IoT test suite by adding explicit -> None return annotations to test functions (sync + async), which enables mypy to type-check their bodies more thoroughly under --check-untyped-defs.

Changes:

  • Added -> None return annotations to 71 test functions across tests/iot/ (8 files).
  • Addressed a handful of newly-exposed mypy issues via targeted variable annotations and more appropriate test doubles (e.g., using Mock() instead of None).
  • Added a concrete type annotation for test_data in test_usage_convert_stat_data to satisfy mypy’s var-annotated requirement.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/iot/test_wallswitch.py Adds -> None to the wallswitch motion test function.
tests/iot/test_iottimezone.py Adds -> None to async/sync timezone-related unit tests.
tests/iot/test_iotstrip.py Adds -> None to async strip behavior tests.
tests/iot/test_iotlightstrip.py Adds -> None to async lightstrip tests and one sync device type test.
tests/iot/test_iotdimmer.py Adds -> None to async dimmer tests and one sync device type test.
tests/iot/test_iotdevice.py Adds -> None broadly to device tests; annotates dict vars to avoid mypy narrowing issues.
tests/iot/modules/test_usage.py Adds -> None; replaces None with Mock() for Usage constructor; adds variable annotations for mypy.
tests/iot/modules/test_emeter.py Adds -> None; annotates inline emeter_data dict to avoid mypy union/narrowing issues.

💡 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.22%. Comparing base (76d9f68) to head (bd06ca6).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1685   +/-   ##
=======================================
  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.

Add `-> None` return types and parameter type annotations to all test
functions across 8 files in `tests/iot/`.

Parameter annotations added:
- `dev` typed to match fixture (IotDevice, IotDimmer, IotLightStrip,
  IotStrip, IotWallSwitch)
- `mocker: MockerFixture` throughout
- `turn_on: bool` for turn_on fixture
- `brightness: int`, `transition: int` for parametrized values

Fix mypy errors exposed by the new annotations:
- Annotate reassigned `dest`/`source` dicts in `test_merge_dict`
- Annotate `emeter_data` dicts to allow nested list access
- Replace `None` with `Mock()` in `Usage()` constructor call
- Add `list[dict[str, int]]` annotation to `test_data` variable
- Add `isinstance(child, IotStripPlug)` narrowing for strip children
- Suppress intentional attr-defined/arg-type errors with type: ignore
Add -> None return types and parameter type annotations to 7 previously
uncovered test files in tests/iot/ and tests/iot/modules/:

- tests/iot/test_iotbulb.py (17 functions)
- tests/iot/modules/test_ambientlight.py (3 functions)
- tests/iot/modules/test_cloud.py (1 function)
- tests/iot/modules/test_dimmer.py (6 functions + fix missing setup vars)
- tests/iot/modules/test_homekit.py (3 functions)
- tests/iot/modules/test_motion.py (7 functions)
- tests/iot/modules/test_schedule.py (1 function)

Also fixes pre-existing bugs in test_dimmer.py where 4 test functions
referenced undefined variables (dimmer, query_helper, test_threshold).
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