Skip to content

Commit

Permalink
fix: Fix format/iSort/Ruff rules
Browse files Browse the repository at this point in the history
  • Loading branch information
jabesq committed Aug 20, 2024
1 parent 0088211 commit e6e20d2
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 80 deletions.
25 changes: 3 additions & 22 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,20 @@ exclude: ^(fixtures/)

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.9
rev: v0.6.1
hooks:
- id: ruff
args:
- --fix

- repo: https://github.com/asottile/pyupgrade
rev: v3.16.0
hooks:
- id: pyupgrade
args: [--py310-plus]
exclude: "external_src/int-tools"

- repo: https://github.com/asottile/add-trailing-comma
rev: v3.1.0
hooks:
- id: add-trailing-comma
args: [--py36-plus]
exclude: "external_src/int-tools"

- repo: https://github.com/asottile/yesqa
rev: v1.5.0
hooks:
- id: yesqa

- repo: https://github.com/psf/black
rev: 24.4.2
rev: 24.8.0
hooks:
- id: black
language_version: python3

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.10.0
rev: v1.11.1
hooks:
- id: mypy
name: mypy
Expand Down
23 changes: 11 additions & 12 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ minversion = "7.0"
asyncio_mode = "auto"

[tool.ruff]
target-version = "py310"

[tool.ruff.lint]
select = [
"B002", # Python does not support the unary prefix increment
"B007", # Loop control variable {name} not used within loop body
Expand All @@ -23,17 +26,13 @@ select = [
"G", # flake8-logging-format
"I", # isort
"ICN001", # import concentions; {name} should be imported as {asname}
"ISC001", # Implicitly concatenated string literals on one line
"N804", # First argument of a class method should be named cls
"N805", # First argument of a method should be named self
"N815", # Variable {name} in class scope should not be mixedCase
"PGH001", # No builtin eval() allowed
"S307", # No builtin eval() allowed
"PGH004", # Use specific rule codes when using noqa
"PLC0414", # Useless import alias. Import alias does not rename original package.
"PLC", # pylint
"PLE", # pylint
"PLR", # pylint
"PLW", # pylint
"PL", # pylint
"Q000", # Double quotes found but single quotes preferred
"RUF006", # Store a reference to the return value of asyncio.create_task
"S102", # Use of exec detected
Expand Down Expand Up @@ -66,7 +65,7 @@ select = [
"T20", # flake8-print
"TID251", # Banned imports
"TRY004", # Prefer TypeError exception for invalid type
"TRY200", # Use raise from to specify exception cause
"B904", # Use raise from to specify exception cause
"TRY302", # Remove exception handler; error is immediately re-raised
"UP", # pyupgrade
"W", # pycodestyle
Expand Down Expand Up @@ -94,25 +93,25 @@ ignore = [
"UP038", # Use `X | Y` in `isinstance` call instead of `(X, Y)`
]

[tool.ruff.flake8-pytest-style]
[tool.ruff.lint.flake8-pytest-style]
fixture-parentheses = false

[tool.ruff.flake8-tidy-imports.banned-api]
[tool.ruff.lint.flake8-tidy-imports.banned-api]
"pytz".msg = "use zoneinfo instead"

[tool.ruff.isort]
[tool.ruff.lint.isort]
force-sort-within-sections = true
combine-as-imports = true
split-on-trailing-comma = false

[tool.ruff.per-file-ignores]
[tool.ruff.lint.per-file-ignores]
# Allow for main entry & scripts to write to stdout
"src/pyatmo/__main__.py" = ["T201"]

# Exceptions for tests
"tests/*" = ["D10"]

[tool.ruff.mccabe]
[tool.ruff.lint.mccabe]
max-complexity = 25

[tool.setuptools_scm]
Expand Down
34 changes: 25 additions & 9 deletions src/pyatmo/modules/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,8 @@ class MeasureType(Enum):


def compute_riemann_sum(
power_data: list[tuple[int, float]], conservative: bool = False
power_data: list[tuple[int, float]],
conservative: bool = False,
):
"""Compute energy from power with a rieman sum."""

Expand Down Expand Up @@ -710,7 +711,9 @@ def reset_measures(self, start_power_time, in_reset=True):
self.sum_energy_elec_off_peak = 0

def get_sum_energy_elec_power_adapted(
self, to_ts: int | float | None = None, conservative: bool = False
self,
to_ts: int | float | None = None,
conservative: bool = False,
):
"""Compute proper energy value with adaptation from power."""
v = self.sum_energy_elec
Expand All @@ -733,10 +736,13 @@ def get_sum_energy_elec_power_adapted(
and isinstance(self, NetatmoBase)
):
power_data = self.get_history_data(
"power", from_ts=from_ts, to_ts=to_ts
"power",
from_ts=from_ts,
to_ts=to_ts,
)
if isinstance(
self, EnergyHistoryMixin
self,
EnergyHistoryMixin,
): # well to please the linter....
delta_energy = compute_riemann_sum(power_data, conservative)

Expand Down Expand Up @@ -791,7 +797,10 @@ async def async_update_measures(
filters, raw_data = await self._energy_API_calls(start_time, end_time, interval)

hist_good_vals = await self._get_aligned_energy_values_and_mode(
start_time, end_time, delta_range, raw_data
start_time,
end_time,
delta_range,
raw_data,
)

self.historical_data = []
Expand Down Expand Up @@ -920,7 +929,11 @@ async def _prepare_exported_historical_data(
self._anchor_for_power_adjustment = computed_end_for_calculus

async def _get_aligned_energy_values_and_mode(
self, start_time, end_time, delta_range, raw_data
self,
start_time,
end_time,
delta_range,
raw_data,
):
hist_good_vals = []
values_lots = raw_data
Expand All @@ -937,7 +950,7 @@ async def _get_aligned_energy_values_and_mode(
)
raise ApiError(
f"Energy badly formed resp beg_time missing: {values_lots} - "
f"module: {self.name}"
f"module: {self.name}",
) from None

interval_sec = values_lot.get("step_time")
Expand Down Expand Up @@ -997,12 +1010,15 @@ async def _energy_API_calls(self, start_time, end_time, interval):

if rw_dt is None:
self._log_energy_error(
start_time, end_time, msg=f"direct from {filters}", body=rw_dt_f
start_time,
end_time,
msg=f"direct from {filters}",
body=rw_dt_f,
)
raise ApiError(
f"Energy badly formed resp: {rw_dt_f} - "
f"module: {self.name} - "
f"when accessing '{filters}'"
f"when accessing '{filters}'",
)

raw_data = rw_dt
Expand Down
35 changes: 21 additions & 14 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from contextlib import contextmanager
from unittest.mock import AsyncMock, patch

import pyatmo
import pytest

import pyatmo

from .common import fake_post_request, fake_post_request_multi


Expand All @@ -27,12 +28,15 @@ async def async_account(async_auth):
"""AsyncAccount fixture."""
account = pyatmo.AsyncAccount(async_auth)

with patch(
"pyatmo.auth.AbstractAsyncAuth.async_post_api_request",
fake_post_request,
), patch(
"pyatmo.auth.AbstractAsyncAuth.async_post_request",
fake_post_request,
with (
patch(
"pyatmo.auth.AbstractAsyncAuth.async_post_api_request",
fake_post_request,
),
patch(
"pyatmo.auth.AbstractAsyncAuth.async_post_request",
fake_post_request,
),
):
await account.async_update_topology()
yield account
Expand All @@ -51,15 +55,18 @@ async def async_account_multi(async_auth):
"""AsyncAccount fixture."""
account = pyatmo.AsyncAccount(async_auth)

with patch(
"pyatmo.auth.AbstractAsyncAuth.async_post_api_request",
fake_post_request_multi,
), patch(
"pyatmo.auth.AbstractAsyncAuth.async_post_request",
fake_post_request_multi,
with (
patch(
"pyatmo.auth.AbstractAsyncAuth.async_post_api_request",
fake_post_request_multi,
),
patch(
"pyatmo.auth.AbstractAsyncAuth.async_post_request",
fake_post_request_multi,
),
):
await account.async_update_topology(
disabled_homes_ids=["eeeeeeeeeffffffffffaaaaa"]
disabled_homes_ids=["eeeeeeeeeffffffffffaaaaa"],
)
yield account

Expand Down
2 changes: 1 addition & 1 deletion tests/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import json
from unittest.mock import AsyncMock, patch

from pyatmo import DeviceType
import pytest

from pyatmo import DeviceType
from tests.common import MockResponse

# pylint: disable=F6401
Expand Down
26 changes: 16 additions & 10 deletions tests/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import json
from unittest.mock import AsyncMock, patch

import pytest

from pyatmo import DeviceType, NoSchedule
from pyatmo.modules import NATherm1
from pyatmo.modules.device_types import DeviceCategory
import pytest

from tests.common import MockResponse, fake_post_request
from tests.conftest import does_not_raise

Expand Down Expand Up @@ -200,10 +200,13 @@ async def test_async_climate_switch_schedule(
with open("fixtures/status_ok.json", encoding="utf-8") as json_file:
response = json.load(json_file)

with patch(
"pyatmo.auth.AbstractAsyncAuth.async_post_api_request",
AsyncMock(return_value=MockResponse(response, 200)),
), expected:
with (
patch(
"pyatmo.auth.AbstractAsyncAuth.async_post_api_request",
AsyncMock(return_value=MockResponse(response, 200)),
),
expected,
):
await async_home.async_switch_schedule(
schedule_id=t_sched_id,
)
Expand Down Expand Up @@ -351,10 +354,13 @@ async def test_async_climate_set_thermmode(
with open(f"fixtures/{json_fixture}", encoding="utf-8") as json_file:
response = json.load(json_file)

with patch(
"pyatmo.auth.AbstractAsyncAuth.async_post_api_request",
AsyncMock(return_value=MockResponse(response, 200)),
), exception:
with (
patch(
"pyatmo.auth.AbstractAsyncAuth.async_post_api_request",
AsyncMock(return_value=MockResponse(response, 200)),
),
exception,
):
resp = await async_home.async_set_thermmode(
mode=mode,
end_time=end_time,
Expand Down
6 changes: 3 additions & 3 deletions tests/test_energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import json
from unittest.mock import AsyncMock, patch

from pyatmo import ApiHomeReachabilityError, DeviceType
from pyatmo.modules.module import EnergyHistoryMixin, MeasureInterval
import pytest
import time_machine

from pyatmo import ApiHomeReachabilityError, DeviceType
from pyatmo.modules.module import EnergyHistoryMixin, MeasureInterval
from tests.common import MockResponse

# pylint: disable=F6401
Expand Down Expand Up @@ -139,7 +139,7 @@ async def test_disconnected_main_bridge(async_account_multi):
with patch(
"pyatmo.auth.AbstractAsyncAuth.async_post_api_request",
AsyncMock(return_value=mock_home_status_resp),
) as mock_request:
):
try:
await async_account_multi.async_update_status(home_id)
except ApiHomeReachabilityError:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_fan.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Define tests for fan module."""

from pyatmo import DeviceType
import pytest

from pyatmo import DeviceType

# pylint: disable=F6401


Expand Down
4 changes: 2 additions & 2 deletions tests/test_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import json
from unittest.mock import AsyncMock, patch

import pyatmo
from pyatmo import DeviceType, NoDevice
import pytest

import pyatmo
from pyatmo import DeviceType, NoDevice
from tests.common import MockResponse

# pylint: disable=F6401
Expand Down
2 changes: 1 addition & 1 deletion tests/test_shutter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import json
from unittest.mock import AsyncMock, patch

from pyatmo import DeviceType
import pytest

from pyatmo import DeviceType
from tests.common import MockResponse

# pylint: disable=F6401
Expand Down
3 changes: 2 additions & 1 deletion tests/test_switch.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Define tests for switch module."""

from pyatmo import DeviceType
import pytest

from pyatmo import DeviceType

# pylint: disable=F6401


Expand Down
Loading

0 comments on commit e6e20d2

Please sign in to comment.