8000 Configure mypy to run in virtual environment and fix resulting issues… · python-kasa/python-kasa@416d311 · GitHub
[go: up one dir, main page]

Skip to content

Commit 416d311

Browse files
authored
Configure mypy to run in virtual environment and fix resulting issues (#989)
For some time I've noticed that my IDE is reporting mypy errors that the pre-commit hook is not picking up. This is because [mypy mirror](https://github.com/pre-commit/mirrors-mypy) runs in an isolated pre-commit environment which does not have dependencies installed and it enables `--ignore-missing-imports` to avoid errors. This is [advised against by mypy](https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-library-stubs-or-py-typed-marker) for obvious reasons: > We recommend avoiding --ignore-missing-imports if possible: it’s equivalent to adding a # type: ignore to all unresolved imports in your codebase. This PR configures the mypy pre-commit hook to run in the virtual environment and addresses the additional errors identified as a result. It also introduces a minimal mypy config into the `pyproject.toml` [mypy errors identified without the fixes in this PR](https://github.com/user-attachments/files/15896693/mypyerrors.txt)
1 parent 5b7e590 commit 416d311

17 files changed

+138
-42
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ jobs:
2727
python-version: ${{ matrix.python-version }}
2828
cache-pre-commit: true
2929
poetry-version: ${{ env.POETRY_VERSION }}
30+
poetry-install-options: "--all-extras"
3031
- name: "Check supported device md files are up to date"
3132
run: |
3233
poetry run pre-commit run generate-supported --all-files

.pre-commit-config.yaml

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,6 @@ repos:
1616
args: [--fix, --exit-non-zero-on-fix]
1717
- id: ruff-format
1818

19-
- repo: https://github.com/pre-commit/mirrors-mypy
20-
rev: v1.9.0
21-
hooks:
22-
- id: mypy
23-
additional_dependencies: [types-click]
24-
exclude: |
25-
(?x)^(
26-
kasa/modulemapping\.py|
27-
)$
28-
29-
3019
- repo: https://github.com/PyCQA/doc8
3120
rev: 'v1.1.1'
3221
hooks:
@@ -35,6 +24,18 @@ repos:
3524

3625
- repo: local
3726
hooks:
27+
# Run mypy in the virtual environment so it uses the installed dependencies
28+
# for more accurate checking than using the pre-commit mypy mirror
29+
- id: mypy
30+
name: mypy
31+
entry: devtools/run-in-env.sh mypy
32+
language: script
33+
types_or: [python, pyi]
34+
require_serial: true
35+
exclude: | # exclude required because --all-files passes py and pyi
36+
(?x)^(
37+
kasa/modulemapping\.py|
38+
)$
3839
- id: generate-supported
3940
name: Generate supported devices
4041
description: This hook generates the supported device sections of README.md and SUPPORTED.md

devtools/bench/benchmark.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
import orjson
77
from kasa_crypt import decrypt, encrypt
8-
from utils.data import REQUEST, WIRE_RESPONSE
9-
from utils.original import OriginalTPLinkSmartHomeProtocol
8+
9+
from devtools.bench.utils.data import REQUEST, WIRE_RESPONSE
10+
from devtools.bench.utils.original import OriginalTPLinkSmartHomeProtocol
1011

1112

1213
def original_request_response() -> None:

devtools/run-in-env.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
source $(poetry env info --path)/bin/activate
3+
exec "$@"

kasa/aestransport.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,10 @@ def create_from_keypair(handshake_key: str, keypair):
371371
handshake_key_bytes: bytes = base64.b64decode(handshake_key.encode("UTF-8"))
372372
private_key_data = base64.b64decode(keypair.get_private_key().encode("UTF-8"))
373373

374-
private_key = serialization.load_der_private_key(private_key_data, None, None)
374+
private_key = cast(
375+
rsa.RSAPrivateKey,
376+
serialization.load_der_private_key(private_key_data, None, None),
377+
)
375378
key_and_iv = private_key.decrypt(
376379
handshake_key_bytes, asymmetric_padding.PKCS1v15()
377380
)

kasa/cli.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ def error(msg: str):
101101
# Block list of commands which require no update
102102
SKIP_UPDATE_COMMANDS = ["raw-command", "command"]
103103

104-
click.anyio_backend = "asyncio"
105-
106-
pass_dev = click.make_pass_decorator(Device)
104+
pass_dev = click.make_pass_decorator(Device) # type: ignore[type-abstract]
107105

108106

109107
def CatchAllExceptions(cls):
@@ -1005,7 +1003,7 @@ async def time_get(dev: Device):
10051003

10061004
@time.command(name="sync")
10071005
@pass_dev
1008-
async def time_sync(dev: SmartDevice):
1006+
async def time_sync(dev: Device):
10091007
"""Set the device time to current time."""
10101008
if not isinstance(dev, SmartDevice):
10111009
raise NotImplementedError("setting time currently only implemented on smart")
@@ -1143,7 +1141,7 @@ async def presets(ctx):
11431141

11441142
@presets.command(name="list")
11451143
@pass_dev
1146-
def presets_list(dev: IotBulb):
1144+
def presets_list(dev: Device):
11471145
"""List presets."""
11481146
if not dev.is_bulb or not isinstance(dev, IotBulb):
11491147
error("Presets only supported on iot bulbs")
@@ -1162,7 +1160,7 @@ def presets_list(dev: IotBulb):
11621160
@click.option("--saturation", type=int)
11631161
@click.option("--temperature", type=int)
11641162
@pass_dev
1165-
async def presets_modify(dev: IotBulb, index, brightness, hue, saturation, temperature):
1163+
async def presets_modify(dev: Device, index, brightness, hue, saturation, temperature):
11661164
"""Modify a preset."""
11671165
for preset in dev.presets:
11681166
if preset.index == index:
@@ -1190,7 +1188,7 @@ async def presets_modify(dev: IotBulb, index, brightness, hue, saturation, tempe
11901188
@click.option("--type", type=click.Choice(["soft", "hard"], case_sensitive=False))
11911189
@click.option("--last", is_flag=True)
11921190
@click.option("--preset", type=int)
1193-
async def turn_on_behavior(dev: IotBulb, type, last, preset):
1191+
async def turn_on_behavior(dev: Device, type, last, preset):
11941192
"""Modify bulb turn-on behavior."""
11951193
if not dev.is_bulb or not isinstance(dev, IotBulb):
11961194
error("Presets only supported on iot bulbs")
@@ -1248,7 +1246,7 @@ async def shell(dev: Device):
12481246
logging.getLogger("asyncio").setLevel(logging.WARNING)
12491247
loop = asyncio.get_event_loop()
12501248
try:
1251-
await embed(
1249+
await embed( # type: ignore[func-returns-value]
12521250
globals=globals(),
12531251
locals=locals(),
12541252
return_asyncio_coroutine=True,

kasa/httpclient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class HttpClient:
3636

3737
def __init__(self, config: DeviceConfig) -> None:
3838
self._config = config
39-
self._client_session: aiohttp.ClientSession = None
39+
self._client_session: aiohttp.ClientSession | None = None
4040
self._jar = aiohttp.CookieJar(unsafe=True, quote_cookie=False)
4141
self._last_url = URL(f"http://{self._config.host}/")
4242

kasa/tests/discovery_fixtures.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@ def discovery_data(request, mocker):
231231
return {"system": {"get_sysinfo": fixture_data["system"]["get_sysinfo"]}}
232232

233233

234-
@pytest.fixture(params=UNSUPPORTED_DEVICES.values(), ids=UNSUPPORTED_DEVICES.keys())
57AE
234+
@pytest.fixture(
235+
params=UNSUPPORTED_DEVICES.values(), ids=list(UNSUPPORTED_DEVICES.keys())
236+
)
235237
def unsupported_device_info(request, mocker):
236238
"""Return unsupported devices for cli and discovery tests."""
237239
discovery_data = request.param

kasa/tests/fakeprotocol_smart.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def _send_request(self, request_dict: dict):
276276
):
277277
result["sum"] = len(result[list_key])
278278
if self.warn_fixture_missing_methods:
279-
pytest.fixtures_missing_methods.setdefault(
279+
pytest.fixtures_missing_methods.setdefault( # type: ignore[attr-defined]
280280
self.fixture_name, set()
281281
).add(f"{method} (incomplete '{list_key}' list)")
282282

@@ -305,7 +305,7 @@ def _send_request(self, request_dict: dict):
305305
}
306306
# Reduce warning spam by consolidating and reporting at the end of the run
307307
if self.warn_fixture_missing_methods:
308-
pytest.fixtures_missing_methods.setdefault(
308+
pytest.fixtures_missing_methods.setdefault( # type: ignore[attr-defined]
309309
self.fixture_name, set()
310310
).add(method)
311311
return retval

kasa/tests/smart/modules/test_firmware.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import asyncio
44
import logging
5+
from typing import TypedDict
56

67
import pytest
78
from pytest_mock import MockerFixture
@@ -71,7 +72,17 @@ async def test_firmware_update(
7172
assert fw
7273

7374
upgrade_time = 5
74-
extras = {"reboot_time": 5, "upgrade_time": upgrade_time, "auto_upgrade": False}
75+
76+
class Extras(TypedDict):
77+
reboot_time: int
78+
upgrade_time: int
79+
auto_upgrade: bool
80+
81+
extras: Extras = {
82+
"reboot_time": 5,
83+
"upgrade_time": upgrade_time,
84+
"auto_upgrade": False,
85+
}
7586
update_states = [
7687
# Unknown 1
7788
DownloadState(status=1, download_progress=0, **extras),

0 commit comments

Comments
 (0)
0