From d1a3d40d38e5655e3d234217330a030285f79aff Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Tue, 7 May 2024 08:54:38 +0200 Subject: [PATCH] Address and enable flake8 type checking checks (#687) --- matter_server/client/client.py | 7 ++++--- matter_server/client/models/node.py | 6 ++++-- matter_server/common/helpers/util.py | 8 ++++---- matter_server/common/models.py | 6 ++++-- matter_server/server/client_handler.py | 6 ++++-- matter_server/server/device_controller.py | 10 +++++----- matter_server/server/helpers/custom_web_runner.py | 5 ++++- matter_server/server/server.py | 4 +++- pyproject.toml | 5 ----- tests/server/test_server.py | 10 +++++++++- 10 files changed, 41 insertions(+), 26 deletions(-) diff --git a/matter_server/client/client.py b/matter_server/client/client.py index ff499ff9..72d2d81b 100644 --- a/matter_server/client/client.py +++ b/matter_server/client/client.py @@ -3,13 +3,10 @@ from __future__ import annotations import asyncio -from collections.abc import Callable import logging -from types import TracebackType from typing import TYPE_CHECKING, Any, Final, Optional, cast import uuid -from aiohttp import ClientSession from chip.clusters import Objects as Clusters from chip.clusters.Types import NullValue @@ -50,6 +47,10 @@ ) if TYPE_CHECKING: + from collections.abc import Callable + from types import TracebackType + + from aiohttp import ClientSession from chip.clusters.Objects import ClusterCommand SUB_WILDCARD: Final = "*" diff --git a/matter_server/client/models/node.py b/matter_server/client/models/node.py index b376adfb..81336e62 100644 --- a/matter_server/client/models/node.py +++ b/matter_server/client/models/node.py @@ -5,7 +5,7 @@ from dataclasses import dataclass from enum import Enum import logging -from typing import Any, TypeVar, cast +from typing import TYPE_CHECKING, Any, TypeVar, cast from chip.clusters import Objects as Clusters from chip.clusters.ClusterObjects import ALL_ATTRIBUTES, ALL_CLUSTERS @@ -15,7 +15,6 @@ parse_attribute_path, parse_value, ) -from matter_server.common.models import MatterNodeData from .device_types import ( ALL_TYPES as DEVICE_TYPES, @@ -25,6 +24,9 @@ RootNode, ) +if TYPE_CHECKING: + from matter_server.common.models import MatterNodeData + LOGGER = logging.getLogger(__name__) # pylint: disable=invalid-name diff --git a/matter_server/common/helpers/util.py b/matter_server/common/helpers/util.py index c38fdae4..9cf1a4ec 100644 --- a/matter_server/common/helpers/util.py +++ b/matter_server/common/helpers/util.py @@ -25,15 +25,15 @@ get_type_hints, ) -from chip.clusters.ClusterObjects import ( - ClusterAttributeDescriptor, - ClusterObjectDescriptor, -) from chip.clusters.Types import Nullable from chip.tlv import float32, uint if TYPE_CHECKING: from _typeshed import DataclassInstance + from chip.clusters.ClusterObjects import ( + ClusterAttributeDescriptor, + ClusterObjectDescriptor, + ) _T = TypeVar("_T", bound=DataclassInstance) diff --git a/matter_server/common/models.py b/matter_server/common/models.py index 8c20b1eb..91389c37 100644 --- a/matter_server/common/models.py +++ b/matter_server/common/models.py @@ -4,9 +4,11 @@ from collections.abc import Callable from dataclasses import dataclass, field -from datetime import datetime from enum import Enum -from typing import Any +from typing import TYPE_CHECKING, Any + +if TYPE_CHECKING: + from datetime import datetime # Enums and constants diff --git a/matter_server/server/client_handler.py b/matter_server/server/client_handler.py index 60ce15fd..2cdb8a84 100644 --- a/matter_server/server/client_handler.py +++ b/matter_server/server/client_handler.py @@ -3,7 +3,6 @@ from __future__ import annotations import asyncio -from collections.abc import Callable from concurrent import futures from contextlib import suppress import logging @@ -15,7 +14,6 @@ from matter_server.common.const import VERBOSE_LOG_LEVEL from matter_server.common.helpers.json import json_dumps, json_loads -from matter_server.common.models import EventType from ..common.errors import InvalidArguments, InvalidCommand, MatterError from ..common.helpers.api import parse_arguments @@ -30,6 +28,10 @@ ) if TYPE_CHECKING: + from collections.abc import Callable + + from matter_server.common.models import EventType + from ..common.helpers.api import APICommandHandler from .server import MatterServer diff --git a/matter_server/server/device_controller.py b/matter_server/server/device_controller.py index 3dc6a1dc..816811a6 100644 --- a/matter_server/server/device_controller.py +++ b/matter_server/server/device_controller.py @@ -6,22 +6,18 @@ import asyncio from collections import deque -from collections.abc import Callable, Iterable from datetime import datetime from functools import partial import logging -from pathlib import Path from random import randint import time from typing import TYPE_CHECKING, Any, TypeVar, cast -from chip.ChipDeviceCtrl import DeviceProxyWrapper from chip.clusters import Attribute, Objects as Clusters from chip.clusters.Attribute import ValueDecodeFailure from chip.clusters.ClusterObjects import ALL_ATTRIBUTES, ALL_CLUSTERS, Cluster from chip.discovery import DiscoveryType from chip.exceptions import ChipStackError -from chip.native import PyChipError from zeroconf import BadTypeInNameException, IPVersion, ServiceStateChange, Zeroconf from zeroconf.asyncio import AsyncServiceBrowser, AsyncServiceInfo, AsyncZeroconf @@ -57,7 +53,11 @@ from .helpers.paa_certificates import fetch_certificates if TYPE_CHECKING: - from chip.ChipDeviceCtrl import ChipDeviceController + from collections.abc import Callable, Iterable + from pathlib import Path + + from chip.ChipDeviceCtrl import ChipDeviceController, DeviceProxyWrapper + from chip.native import PyChipError from .server import MatterServer diff --git a/matter_server/server/helpers/custom_web_runner.py b/matter_server/server/helpers/custom_web_runner.py index 16f702bc..d0233fd7 100644 --- a/matter_server/server/helpers/custom_web_runner.py +++ b/matter_server/server/helpers/custom_web_runner.py @@ -3,11 +3,14 @@ from __future__ import annotations import asyncio -from ssl import SSLContext +from typing import TYPE_CHECKING from aiohttp import web from yarl import URL +if TYPE_CHECKING: + from ssl import SSLContext + class MultiHostTCPSite(web.BaseSite): """Multiple host capable aiohttp Site. diff --git a/matter_server/server/server.py b/matter_server/server/server.py index 4d0a11ca..cbfc06e0 100644 --- a/matter_server/server/server.py +++ b/matter_server/server/server.py @@ -3,7 +3,6 @@ from __future__ import annotations import asyncio -from collections.abc import Callable from functools import partial import ipaddress import logging @@ -36,6 +35,9 @@ from .storage import StorageController from .vendor_info import VendorInfo +if TYPE_CHECKING: + from collections.abc import Callable + DASHBOARD_DIR = Path(__file__).parent.joinpath("../dashboard/").resolve() DASHBOARD_DIR_EXISTS = DASHBOARD_DIR.exists() diff --git a/pyproject.toml b/pyproject.toml index cd3d2374..a71156e3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -162,16 +162,12 @@ ignore = [ "FIX002", # Just annoying, not really useful "PLR2004", # Just annoying, not really useful "PD011", # Just annoying, not really useful - "TCH001", # Just annoying, not really useful - "TCH003", # Just annoying, not really useful "S101", # assert is often used to satisfy type checking "TD002", # Just annoying, not really useful "TD003", # Just annoying, not really useful "TD004", # Just annoying, not really useful "COM812", # Conflict with the Ruff formatter "ISC001", # Conflict with the Ruff formatter - "TCH003", # TEMPORARY DISABLED - "TCH002", # TEMPORARY DISABLED "TID252", # TEMPORARY DISABLED "N805", # TEMPORARY DISABLED "EXE002", # TEMPORARY DISABLED @@ -216,7 +212,6 @@ ignore = [ "N801", # TEMPORARY DISABLED "N813", # TEMPORARY DISABLED "RUF012", # TEMPORARY DISABLED - "TCH001", # TEMPORARY DISABLED "ANN102", # TEMPORARY DISABLED "B007", # TEMPORARY DISABLED "SIM102", # TEMPORARY DISABLED diff --git a/tests/server/test_server.py b/tests/server/test_server.py index 1c468fef..0bcace05 100644 --- a/tests/server/test_server.py +++ b/tests/server/test_server.py @@ -2,7 +2,7 @@ from __future__ import annotations -from collections.abc import AsyncGenerator, Generator +from typing import TYPE_CHECKING from unittest.mock import MagicMock, patch import pytest @@ -11,6 +11,9 @@ from matter_server.common.models import APICommand from matter_server.server.server import MatterServer +if TYPE_CHECKING: + from collections.abc import AsyncGenerator, Generator + pytestmark = pytest.mark.usefixtures( "application", "app_runner", @@ -74,6 +77,11 @@ def chip_stack_fixture() -> Generator[MagicMock, None, None]: @pytest.fixture(name="certificate_authority_manager") def certificate_authority_manager_fixture() -> Generator[MagicMock, None, None]: """Return a mocked certificate authority manager.""" + + # Necessary for patching within tests + # pylint: disable=unused-import,import-outside-toplevel + import chip.CertificateAuthority # noqa: F401 + with patch( "matter_server.server.stack.chip.CertificateAuthority.CertificateAuthorityManager", autospec=True,