Skip to content

Commit

Permalink
Fix broken pre-commit (#262)
Browse files Browse the repository at this point in the history
* Fix broken pre-commit

* Add types

* Fix toml

* Fix CI

* Try pre-commit CI

* Revert "Try pre-commit CI"

This reverts commit ebdbaf1.

* Another CI fix

* FiX CI

* Fix CI again

* Fix CI

* Fix mypy

* Fix CI
  • Loading branch information
bachya authored Oct 6, 2021
1 parent 0e1cdfe commit df68dc1
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 21 deletions.
14 changes: 7 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repos:
- --quiet
- --format=custom
- --configfile=.bandit.yaml
files: ^aiowatttime/.+\.py$
files: ^simplipy/.+\.py$
- repo: https://github.com/python/black
rev: 21.7b0
hooks:
Expand All @@ -17,7 +17,7 @@ repos:
- --safe
- --quiet
language_version: python3
files: ^((aiowatttime|tests)/.+)?[^/]+\.py$
files: ^((simplipy|tests)/.+)?[^/]+\.py$
- repo: https://github.com/codespell-project/codespell
rev: v1.16.0
hooks:
Expand All @@ -34,19 +34,19 @@ repos:
additional_dependencies:
- flake8-docstrings==1.5.0
- pydocstyle==5.0.1
files: ^aiowatttime/.+\.py$
files: ^simplipy/.+\.py$
- repo: https://github.com/pre-commit/mirrors-isort
rev: v4.3.21
hooks:
- id: isort
additional_dependencies:
- toml
files: ^(aiowatttime|tests)/.+\.py$
files: ^(simplipy|tests)/.+\.py$
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.910
rev: v0.910-1
hooks:
- id: mypy
files: ^aiowatttime/.+\.py$
files: ^simplipy/.+\.py$
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
hooks:
Expand All @@ -59,7 +59,7 @@ repos:
rev: 5.0.2
hooks:
- id: pydocstyle
files: ^((aiowatttime|tests)/.+)?[^/]+\.py$
files: ^((simplipy|tests)/.+)?[^/]+\.py$
- repo: https://github.com/gruntwork-io/pre-commit
rev: v0.1.12
hooks:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ aiohttp = "^3.7.4.post0"
backoff = "^1.11.1"
python = "^3.8.0"
pytz = ">=2019.3,<2022.0"
types-pytz = "^2021.1.0"
voluptuous = ">=0.11.7,<0.13.0"
websockets = ">=8.1,<10.0"

Expand All @@ -76,6 +75,7 @@ pytest = "^6.0.0"
pytest-aiohttp = "^0.3.0"
pytest-cov = "^3.0.0"
sphinx-rtd-theme = "^1.0.0"
types-pytz = "^2021.1.0"

[tool.pylint.BASIC]
class-const-naming-style = "any"
Expand Down
1 change: 1 addition & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ pytest-aiohttp==0.3.0
pytest-cov==2.12.1
pytest==6.2.5
pytz==2021.1
types-pytz==2021.1.2
voluptuous==0.12.2
4 changes: 2 additions & 2 deletions simplipy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ async def from_refresh_token(
async def _handle_on_backoff(self, _: dict[str, Any]) -> None:
"""Handle a backoff retry."""
err_info = sys.exc_info()
err = err_info[1].with_traceback(err_info[2]) # type: ignore
err: ClientResponseError = err_info[1].with_traceback(err_info[2]) # type: ignore

if err.status == 401 or err.status == 403:
LOGGER.info("401 detected; attempting refresh token")
Expand Down Expand Up @@ -244,7 +244,7 @@ def add_refresh_token_listener(
"""
self._refresh_token_listeners.append(callback)

def remove():
def remove() -> None:
"""Remove the callback."""
self._refresh_token_listeners.remove(callback)

Expand Down
4 changes: 2 additions & 2 deletions simplipy/util/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def get_auth0_code_challenge(code_verifier: str) -> str:
:type code_verifier: ``str``
:rtype: ``str``
"""
challenge = hashlib.sha256(code_verifier.encode("utf-8")).digest()
challenge = base64.urlsafe_b64encode(challenge).decode("utf-8")
verifier = hashlib.sha256(code_verifier.encode("utf-8")).digest()
challenge = base64.urlsafe_b64encode(verifier).decode("utf-8")
return challenge.replace("=", "")


Expand Down
5 changes: 3 additions & 2 deletions simplipy/util/dt.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Define datetime utilities."""
from datetime import datetime
from typing import cast

import pytz
import pytz # type: ignore

UTC = pytz.utc

Expand All @@ -13,4 +14,4 @@ def utc_from_timestamp(timestamp: float) -> datetime:
:type timestamp: ``float``
:rtype: ``datetime.datetime``
"""
return UTC.localize(datetime.utcfromtimestamp(timestamp))
return cast(datetime, UTC.localize(datetime.utcfromtimestamp(timestamp)))
18 changes: 11 additions & 7 deletions simplipy/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import asyncio
from dataclasses import InitVar, dataclass, field
from datetime import datetime, timedelta
from typing import TYPE_CHECKING, Any, Callable
from typing import TYPE_CHECKING, Any, Callable, Dict, cast

from aiohttp import ClientWebSocketResponse, WSMsgType
from aiohttp.client_exceptions import (
Expand Down Expand Up @@ -148,7 +148,7 @@ class WebsocketEvent: # pylint: disable=too-many-instance-attributes
event_cid: InitVar[int]
info: str
system_id: int
timestamp: datetime
timestamp: float

event_type: str | None = field(init=False)

Expand All @@ -157,7 +157,7 @@ class WebsocketEvent: # pylint: disable=too-many-instance-attributes
sensor_serial: str | None = None
sensor_type: DeviceTypes | None = None

def __post_init__(self, event_cid):
def __post_init__(self, event_cid: int) -> None:
"""Run post-init initialization."""
if event_cid in EVENT_MAPPING:
object.__setattr__(self, "event_type", EVENT_MAPPING[event_cid])
Expand Down Expand Up @@ -185,7 +185,7 @@ def __post_init__(self, event_cid):
object.__setattr__(self, "sensor_type", None)


def websocket_event_from_payload(payload: dict):
def websocket_event_from_payload(payload: dict[str, Any]) -> WebsocketEvent:
"""Create a Message object from a websocket event payload."""
return WebsocketEvent(
payload["data"]["eventCid"],
Expand Down Expand Up @@ -234,13 +234,13 @@ def _add_listener(
"""Add a listener callback to a particular list."""
listener_list.append(callback)

def remove():
def remove() -> None:
"""Remove the callback."""
listener_list.remove(callback)

return remove

async def _async_receive_json(self) -> dict:
async def _async_receive_json(self) -> dict[str, Any]:
"""Receive a JSON response from the websocket server."""
assert self._client
msg = await self._client.receive()
Expand All @@ -261,7 +261,7 @@ async def _async_receive_json(self) -> dict:

LOGGER.debug("Received data from websocket server: %s", data)

return data
return cast(Dict[str, Any], data)

async def _async_send_json(self, payload: dict[str, Any]) -> None:
"""Send a JSON message to the websocket server.
Expand Down Expand Up @@ -340,6 +340,8 @@ async def async_connect(self) -> None:

async def async_disconnect(self) -> None:
"""Disconnect from the websocket server."""
assert self._client

await self._client.close()

LOGGER.info("Disconnected from websocket server")
Expand All @@ -349,6 +351,8 @@ async def async_disconnect(self) -> None:

async def async_listen(self) -> None:
"""Start listening to the websocket server."""
assert self._client

now = datetime.utcnow()
now_ts = round(now.timestamp() * 1000)
now_utc_iso = f"{now.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3]}Z"
Expand Down

0 comments on commit df68dc1

Please sign in to comment.