Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address remaining pylint issues in pytradfri #514

Merged
merged 4 commits into from
Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ disable = [
"format",
# "abstract-class-little-used",
"abstract-method",
# When Python 3.10 is the minimum version
# (or pydantic is updated to support the new union syntax with Python 3.9)
# we can remove this disable and change to the new syntax.
"consider-alternative-union-syntax",
"consider-using-assignment-expr",
"cyclic-import",
"duplicate-code",
Expand Down
2 changes: 2 additions & 0 deletions pytradfri/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

CONFIG_FILE = "tradfri_standalone_psk.conf"

# pylint: disable=invalid-name


if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
Expand Down
8 changes: 4 additions & 4 deletions pytradfri/api/aiocoap_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
from enum import Enum
import json
import logging
from typing import Any, Dict, List, Protocol, Union, cast, overload
from typing import Any, Protocol, Union, cast, overload

from aiocoap import Context, Message
from aiocoap.credentials import CredentialsMissingError
from aiocoap.error import (
ConstructionRenderableError,
Error,
LibraryShutdown,
TimeoutError,
TimeoutError as COAPTimeoutError,
)
from aiocoap.numbers.codes import Code
from aiocoap.protocol import BlockwiseRequest
Expand Down Expand Up @@ -158,7 +158,7 @@ async def _get_response(
await self._reset_protocol(exc)
await self._update_credentials()
raise RequestTimeout("Request timed out.", exc) from exc
except TimeoutError as exc:
except COAPTimeoutError as exc:
await self._reset_protocol(exc)
await self._update_credentials()
raise RequestTimeout("Request timed out.", exc) from exc
Expand Down Expand Up @@ -341,4 +341,4 @@ def _process_output(
if not parse_json:
return output

return cast(Union[List[Any], Dict[Any, Any]], json.loads(output))
return cast(Union[list[Any], dict[Any, Any]], json.loads(output))
6 changes: 2 additions & 4 deletions pytradfri/api/libcoap_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
import subprocess
from time import time
from typing import TYPE_CHECKING, Any, Dict, List, Union, cast, overload
from typing import TYPE_CHECKING, Any, Union, cast, overload

from ..command import Command, T
from ..error import ClientError, RequestError, RequestTimeout, ServerError
Expand Down Expand Up @@ -109,14 +109,12 @@ def _execute(self, api_command: Command[T], *, timeout: int | None = None) -> T:
@overload
def request(self, api_commands: Command[T], timeout: int | None = None) -> T:
"""Make a request. Timeout is in seconds."""
...

@overload
def request(
self, api_commands: list[Command[T]], timeout: int | None = None
) -> list[T]:
"""Make a request. Timeout is in seconds."""
...

def request(
self, api_commands: Command[T] | list[Command[T]], timeout: int | None = None
Expand Down Expand Up @@ -231,4 +229,4 @@ def _process_output(
raise ServerError(output)
if not parse_json:
return output
return cast(Union[Dict[Any, Any], List[Any]], json.loads(output))
return cast(Union[dict[Any, Any], list[Any]], json.loads(output))
6 changes: 3 additions & 3 deletions pytradfri/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from .device.light import LightResponse

from .const import (
SUPPORT_BRIGHTNESS,
SUPPORT_COLOR_TEMP,
Expand All @@ -14,6 +11,9 @@
SUPPORT_XY_COLOR,
)

if TYPE_CHECKING:
from .device.light import LightResponse

# Extracted from Tradfri Android App string.xml
COLOR_NAMES = {
"4a418a": "Blue",
Expand Down
12 changes: 6 additions & 6 deletions pytradfri/device/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import annotations

from datetime import datetime
from typing import List, Optional
from typing import Optional

from pydantic import BaseModel, Field

Expand Down Expand Up @@ -51,19 +51,19 @@ class DeviceInfoResponse(BaseModel):
class DeviceResponse(ApiResourceResponse):
"""Represent a device response."""

air_purifier_control: Optional[List[AirPurifierResponse]] = Field(
air_purifier_control: Optional[list[AirPurifierResponse]] = Field(
alias=ROOT_AIR_PURIFIER
)
application_type: int = Field(alias=ATTR_APPLICATION_TYPE)
blind_control: Optional[List[BlindResponse]] = Field(alias=ATTR_START_BLINDS)
blind_control: Optional[list[BlindResponse]] = Field(alias=ATTR_START_BLINDS)
device_info: DeviceInfoResponse = Field(alias=ATTR_DEVICE_INFO)
last_seen: Optional[int] = Field(alias=ATTR_LAST_SEEN)
light_control: Optional[List[LightResponse]] = Field(alias=ATTR_LIGHT_CONTROL)
light_control: Optional[list[LightResponse]] = Field(alias=ATTR_LIGHT_CONTROL)
reachable: int = Field(alias=ATTR_REACHABLE_STATE)
signal_repeater_control: Optional[List[SignalRepeaterResponse]] = Field(
signal_repeater_control: Optional[list[SignalRepeaterResponse]] = Field(
alias=ROOT_SIGNAL_REPEATER
)
socket_control: Optional[List[SocketResponse]] = Field(alias=ATTR_SWITCH_PLUG)
socket_control: Optional[list[SocketResponse]] = Field(alias=ATTR_SWITCH_PLUG)


class Device(ApiResource):
Expand Down
3 changes: 2 additions & 1 deletion pytradfri/device/base_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from __future__ import annotations

from abc import abstractmethod
from typing import TYPE_CHECKING, Sequence
from collections.abc import Sequence
from typing import TYPE_CHECKING

from ..resource import BaseResponse

Expand Down
6 changes: 3 additions & 3 deletions pytradfri/device/light_control.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""Class to control the lights."""
from __future__ import annotations

from collections.abc import Sequence
from collections.abc import Mapping, Sequence
from copy import deepcopy
from typing import TYPE_CHECKING, List, Mapping, Union, cast
from typing import TYPE_CHECKING, Union, cast

from ..color import COLORS
from ..command import Command
Expand Down Expand Up @@ -199,7 +199,7 @@ def combine_commands(self, commands: Sequence[Command[None]]) -> Command[None]:
raise TypeError(f"Invalid command data: {data}")

light_control_data = cast(
List[Mapping[str, Union[str, int]]], data[ATTR_LIGHT_CONTROL]
list[Mapping[str, Union[str, int]]], data[ATTR_LIGHT_CONTROL]
)
for control_data in light_control_data:
combined_data.update(control_data)
Expand Down
6 changes: 3 additions & 3 deletions pytradfri/group.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Group handling."""
from __future__ import annotations

from typing import TYPE_CHECKING, Dict, List, Optional
from typing import TYPE_CHECKING, Optional

from pydantic import Field

Expand Down Expand Up @@ -44,7 +44,7 @@ class GroupResponse(ApiResourceResponse):

color_hex: Optional[str] = Field(alias=ATTR_LIGHT_COLOR_HEX)
dimmer: int = Field(alias=ATTR_LIGHT_DIMMER)
group_members: Dict[str, Dict[str, List[int]]] = Field(alias=ATTR_GROUP_MEMBERS)
group_members: dict[str, dict[str, list[int]]] = Field(alias=ATTR_GROUP_MEMBERS)
mood_id: str = Field(alias=ATTR_MOOD)
state: int = Field(alias=ATTR_DEVICE_STATE)

Expand Down Expand Up @@ -225,7 +225,7 @@ def set_predefined_color(

def _value_validate(
self, value: int, rnge: tuple[int, int], identifier: str = "Given"
) -> None: # pylint: disable=no-self-use
) -> None:
"""Make sure a value is within a given range."""
if value is not None and (value < rnge[0] or value > rnge[1]):
raise ValueError(
Expand Down
5 changes: 3 additions & 2 deletions pytradfri/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
from __future__ import annotations

from abc import abstractmethod
from collections.abc import Callable
from datetime import datetime
from typing import Any, Callable, Dict, List, Optional, Union
from typing import Any, Optional, Union

from pydantic import BaseModel, Field

from .command import Command
from .const import ATTR_CREATED_AT, ATTR_ID, ATTR_NAME, ATTR_OTA_UPDATE_STATE

# type alias
TypeRaw = Dict[str, Union[str, int, List[Dict[str, Union[str, int]]]]]
TypeRaw = dict[str, Union[str, int, list[dict[str, Union[str, int]]]]]


class BaseResponse(BaseModel):
Expand Down
6 changes: 3 additions & 3 deletions pytradfri/smart_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from __future__ import annotations

from datetime import datetime as dt, time, timedelta
from typing import TYPE_CHECKING, Any, List, Optional
from typing import TYPE_CHECKING, Any, Optional

from pydantic import BaseModel, Field

Expand Down Expand Up @@ -79,7 +79,7 @@ class TimeIntervalResponse(BaseModel):
class RootStartActionResponse(SmartTaskMixin, BaseModel):
"""Represent a smart action response."""

root_start_action: List[StartActionResponse] = Field(
root_start_action: list[StartActionResponse] = Field(
alias=ROOT_START_ACTION, default=[]
)

Expand All @@ -90,7 +90,7 @@ class SmartTaskResponse(SmartTaskMixin, ApiResourceResponse):
smart_task_type: int = Field(alias=ATTR_SMART_TASK_TYPE)
repeat_days: int = Field(alias=ATTR_REPEAT_DAYS)
start_action: RootStartActionResponse = Field(alias=ATTR_START_ACTION)
time_interval: List[TimeIntervalResponse] = Field(
time_interval: list[TimeIntervalResponse] = Field(
alias=ATTR_SMART_TASK_TRIGGER_TIME_INTERVAL
)

Expand Down
5 changes: 3 additions & 2 deletions pytradfri/util.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""JSON utility functions."""
from __future__ import annotations

from collections.abc import Iterator
import json
import logging
from typing import Any, Dict, Iterator, List, Union, cast
from typing import Any, Union, cast

from .error import PytradfriError

Expand All @@ -20,7 +21,7 @@ def load_json(filename: str) -> list[Any] | dict[Any, Any]:
"""
try:
with open(filename, encoding="utf-8") as fdesc:
return cast(Union[Dict[Any, Any], List[Any]], json.loads(fdesc.read()))
return cast(Union[dict[Any, Any], list[Any]], json.loads(fdesc.read()))
except FileNotFoundError:
# This is not a fatal error
_LOGGER.debug("JSON file not found: %s", filename)
Expand Down
2 changes: 2 additions & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ flake8-docstrings==1.6.0
flake8-noqa==1.2.5
mypy==0.961
pre-commit==2.19.0
pylint==2.14.3
pylint_strict_informational==0.1
pytest==7.1.2
pytest-cov==3.0.0
pytest-timeout==2.1.0
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ignore_errors = True
commands =
black --check ./
flake8 examples pytradfri tests
pylint pytradfri
deps =
-rrequirements.txt
-rrequirements_test.txt
Expand Down