Skip to content

Commit

Permalink
Update library tooling to use uv and ruff (#77)
Browse files Browse the repository at this point in the history
* Switch from poetry to uv

* Fix typo

* Switch from black to ruff for formatting.

* Formatted files using ruff.

* Switch to ruff.

* Fix ruff error.

* Fix ruff error.

* Fix lint errors.

* Fix line lengths.

* Fix line length.

* Add ruff flags

* Update publish to latest actions.
  • Loading branch information
gwww authored Aug 21, 2024
1 parent 5bd4c02 commit c090e70
Show file tree
Hide file tree
Showing 14 changed files with 396 additions and 506 deletions.
68 changes: 24 additions & 44 deletions .github/workflows/code-quality.yml
Original file line number Diff line number Diff line change
@@ -1,66 +1,46 @@
name: CI
on:
push:
branches: [main]
pull_request: ~
pull_request:
branches:
- main

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

env:
DEFAULT_UV: "0.3.0"

jobs:
check-quality:
strategy:
fail-fast: false

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.12"
- name: Check out code from GitHub
uses: actions/[email protected]

- name: Install poetry
uses: snok/install-poetry@v1
with:
version: 1.2.2
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
- name: Set up uv
run: curl -LsSf https://astral.sh/uv/${{ env.DEFAULT_UV }}/install.sh | sh

- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v3
- name: "Set up Python"
uses: actions/setup-python@v5
with:
path: .venv
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
python-version-file: "pyproject.toml"

- name: Install dependencies with poetry
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction --no-root
- name: Create venv and install
run: uv sync --all-extras --dev

- name: Run pylint
run: |
source .venv/bin/activate
pylint elkm1_lib
- name: Check formatting
run: uv run ruff format --check

- name: Run black check
run: |
source .venv/bin/activate
black --check .
- name: Ruff lint/sort/etc checking
run: uv run ruff check --no-fix elkm1_lib

- name: Run mypy
run: |
source .venv/bin/activate
mypy --show-error-codes --strict elkm1_lib
- name: Run pylint
run: uv run pylint elkm1_lib

- name: Run isort check
run: |
source .venv/bin/activate
isort --check-only --profile black .
- name: Check typing
run: uv run mypy --show-error-codes --strict elkm1_lib

- name: Run pytest
run: |
source .venv/bin/activate
poetry install --no-interaction
pytest test
run: uv run pytest test
16 changes: 7 additions & 9 deletions .github/workflows/publish-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Check out code from GitHub
uses: actions/[email protected]

- name: Setup Python
uses: actions/setup-python@v3
- name: "Set up Python"
uses: actions/setup-python@v5
with:
python-version: "3.12"
python-version-file: "pyproject.toml"

- name: Install dependencies
- name: Build package
run: |
python -m pip install --upgrade pip
pip install build
- name: Build package
run: python -m build
python -m build
- name: Publish package
uses: pypa/gh-action-pypi-publish@release/v1
Expand Down
3 changes: 3 additions & 0 deletions elkm1_lib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
"""ElkM1 library"""

from .elk import Elk

__all__ = ["Elk"]
2 changes: 1 addition & 1 deletion elkm1_lib/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async def _read_stream(self, reader: asyncio.StreamReader) -> None:

async def _write_stream(self) -> None:
async def write_msg() -> None:
if not q_entry.raw:
if not q_entry.raw: # pylint: disable=possibly-used-before-assignment
cksum = (256 - reduce(lambda x, y: x + y, map(ord, q_entry.msg))) % 256
msg = f"{q_entry.msg}{cksum:02X}\r\n"
else:
Expand Down
5 changes: 2 additions & 3 deletions elkm1_lib/elements.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Base of all the elements found on the Elk panel... Zone, Keypad, etc.
Base of all the elements found on the Elk panel... Zone, Keypad, etc.
"""

from __future__ import annotations
Expand Down Expand Up @@ -117,8 +117,7 @@ def __init__(
notifier.attach("SD", self._sd_handler)

def __iter__(self) -> Generator[Element, None, None]:
for element in self.elements:
yield element
yield from self.elements

def __getitem__(self, key: int) -> Element:
return self.elements[key]
Expand Down
1 change: 1 addition & 0 deletions elkm1_lib/keypads.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Definition of an ElkM1 Keypad."""

import datetime as dt
from typing import Optional

Expand Down
7 changes: 5 additions & 2 deletions elkm1_lib/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def _is_valid_length_and_checksum(msg: str) -> tuple[bool, str]:
if int(msg[:2], 16) != (len(msg) - 2):
return (
False,
f"Incorrect message length, expected {msg[:2]}, got {len(msg)-2:02X}. Msg {msg}",
f"Incorrect message length, expected {msg[:2]}, got {len(msg)-2:02X}. Msg {msg}", # noqa: E501
)
checksum = int(msg[-2:], 16)
for char in msg[:-2]:
Expand Down Expand Up @@ -485,7 +485,10 @@ def dm_encode(
) -> MessageEncode:
"""dm: Display message on keypad."""
return MessageEncode(
f"2Edm{keypad_area + 1:1}{clear:1}{beep:1}{timeout:05}{line1:^<16.16}{line2:^<16.16}00",
(
f"2Edm{keypad_area + 1:1}{clear:1}{beep:1}"
f"{timeout:05}{line1:^<16.16}{line2:^<16.16}00"
),
None,
)

Expand Down
2 changes: 1 addition & 1 deletion elkm1_lib/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def detach(self, notify_type: str, handler: NotifyHandler) -> None:

def notify(self, notify_type: str, notify_parameters: dict[str, Any]) -> None:
"""Call the observers."""
# Copy the obervers list as add/remove could be called when invoking the observers
# Dup obervers list; add/remove could be called when invoking the observers
observers = list(self._observers.get(notify_type, []))
for observer in observers:
try:
Expand Down
1 change: 1 addition & 0 deletions elkm1_lib/panel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Definition of an ElkM1 Area"""

import datetime as dt
import time
from typing import Any, Optional
Expand Down
1 change: 1 addition & 0 deletions elkm1_lib/tasks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Definition of an ElkM1 Task"""

from time import time

from .connection import Connection
Expand Down
2 changes: 1 addition & 1 deletion elkm1_lib/thermostats.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def set(
) -> None:
"""(Helper) Set thermostat"""
if ( # pylint: disable=unidiomatic-typecheck
type(val) != SETTING_TYPING[element_to_set]
type(val) is not SETTING_TYPING[element_to_set]
):
raise ValueError("Wrong type for thermostat setting.")
if isinstance(val, bool):
Expand Down
Loading

0 comments on commit c090e70

Please sign in to comment.