Skip to content

Commit

Permalink
Merge pull request #141 from dknowles2/ruff
Browse files Browse the repository at this point in the history
Replace flake8 with ruff
  • Loading branch information
dknowles2 committed Jun 9, 2024
2 parents 791c04e + e64a0da commit b8bdcb9
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"customizations": {
"vscode": {
"extensions": [
"charliermarsh.ruff",
"streetsidesoftware.code-spell-checker",
"github.vscode-pull-request-github",
"ms-python.black-formatter",
"ms-python.flake8",
"ms-python.mypy-type-checker",
"ms-python.python",
"ms-python.vscode-pylance"
Expand Down
31 changes: 20 additions & 11 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ on:
branches: [ "main" ]

jobs:
build:

build_and_test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10"]

steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
Expand All @@ -27,18 +25,29 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest
python -m pip install -r requirements.txt
python -m pip install -r requirements-test.txt
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
ruff:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements-test.txt
- name: Run ruff
run: |
ruff check --output-format=github .
mypy:
runs-on: ubuntu-latest
strategy:
Expand Down
19 changes: 6 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,13 @@ repos:
- id: pyupgrade
args: [--py310-plus]
stages: [manual]
- repo: https://github.com/PyCQA/autoflake
rev: v2.0.0
hooks:
- id: autoflake
args:
- --in-place
- --remove-all-unused-imports
stages: [manual]
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
args:
- --quiet
files: ^.*\.py$
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
- id: flake8
stages: [manual]
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
Expand Down Expand Up @@ -54,3 +41,9 @@ repos:
- --force
- --keep-updates
files: ^.*\.py$
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.230
hooks:
- id: ruff
args:
- --fix
7 changes: 5 additions & 2 deletions pyschlage/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ class Lock(Mutable):
"""Whether the device is currently locked or None if lock is unavailable."""

is_jammed: bool | None = False
"""Whether the lock has identified itself as jammed or None if lock is unavailable."""
"""Whether the lock has identified itself as jammed.
Returns None if lock is unavailable.
"""

lock_state_metadata: LockStateMetadata | None = None
"""Metadata about the current lock state."""
Expand Down Expand Up @@ -331,7 +334,7 @@ def logs(self, limit: int | None = None, sort_desc: bool = False) -> list[LockLo
if sort_desc:
params["sort"] = "desc"
resp = self._auth.request("get", path, params=params)
return [LockLog.from_json(l) for l in resp.json()]
return [LockLog.from_json(lock_log) for lock_log in resp.json()]

def refresh_access_codes(self) -> None:
"""Fetches access codes for this lock.
Expand Down
5 changes: 4 additions & 1 deletion pyschlage/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ def from_json(cls, json):
# datetime.fromisoformat() doesn't like fractional seconds with a "Z"
# suffix. This seems to fix it.
created_at_str = json["createdAt"].rstrip("Z") + "+00:00"
none_if_default = lambda x: None if x == _DEFAULT_UUID else x

def none_if_default(attr):
return None if attr == _DEFAULT_UUID else attr

return cls(
created_at=_utc2local(datetime.fromisoformat(created_at_str)),
accessor_id=none_if_default(json["message"]["accessorUuid"]),
Expand Down
2 changes: 2 additions & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
-r requirements.txt
mypy==1.10.0
pytest==8.2.2
pytest-timeout==2.3.1
ruff==0.4.8
3 changes: 1 addition & 2 deletions tests/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from datetime import datetime
from unittest import mock

import pyschlage
from pyschlage.auth import Auth
from pyschlage.code import AccessCode, DaysOfWeek, RecurringSchedule, TemporarySchedule

Expand Down Expand Up @@ -121,4 +120,4 @@ def test_delete(self, access_code_json):
assert code._auth is None
assert code.access_code_id is None
assert code.device_id is None
assert code.disabled == True
assert code.disabled
2 changes: 1 addition & 1 deletion tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ def test_redact_partial(json_dict: dict[Any, Any]):
},
"d": ["<REDACTED>"],
}
assert common.redact(json_dict, allowed=["a", "b", "c.c0"])
assert common.redact(json_dict, allowed=["a", "b", "c.c0"]) == want
30 changes: 14 additions & 16 deletions tests/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from typing import Any
from unittest import mock

import pytest

from pyschlage.code import AccessCode
from pyschlage.lock import Lock
from pyschlage.log import LockLog
Expand Down Expand Up @@ -185,7 +183,7 @@ def test_unlock_wifi(self, mock_auth, wifi_lock_json):
mock_auth.request.assert_called_once_with(
"put", "devices/__wifi_uuid__", json={"attributes": {"lockState": 0}}
)
assert lock.is_locked == False
assert not lock.is_locked

def test_lock_ble(self, mock_auth, ble_lock_json):
lock = Lock.from_json(mock_auth, ble_lock_json)
Expand Down Expand Up @@ -221,7 +219,7 @@ def test_unlock_ble(self, mock_auth, ble_lock_json):
mock_auth.request.assert_called_once_with(
"post", "devices/__ble_uuid__/commands", json=command_json
)
assert lock.is_locked == False
assert not lock.is_locked

def test_refresh_access_codes(
self, mock_auth: mock.Mock, lock_json: dict, access_code_json: dict
Expand Down Expand Up @@ -267,7 +265,7 @@ def test_add_access_code(self, mock_auth, lock_json, access_code_json):


class TestKeypadDisabled:
def test_true(self, wifi_lock: Lock) -> None:
def test_true(self, wifi_lock: mock.Mock) -> None:
logs = [
LockLog(
created_at=datetime(2023, 1, 1, 0, 0, 0),
Expand All @@ -280,7 +278,7 @@ def test_true(self, wifi_lock: Lock) -> None:
]
assert wifi_lock.keypad_disabled(logs) is True

def test_true_unsorted(self, wifi_lock: Lock) -> None:
def test_true_unsorted(self, wifi_lock: mock.Mock) -> None:
logs = [
LockLog(
created_at=datetime(2023, 1, 1, 1, 0, 0),
Expand All @@ -293,7 +291,7 @@ def test_true_unsorted(self, wifi_lock: Lock) -> None:
]
assert wifi_lock.keypad_disabled(logs) is True

def test_false(self, wifi_lock: Lock) -> None:
def test_false(self, wifi_lock: mock.Mock) -> None:
logs = [
LockLog(
created_at=datetime(2023, 1, 1, 0, 0, 0),
Expand All @@ -306,7 +304,7 @@ def test_false(self, wifi_lock: Lock) -> None:
]
assert wifi_lock.keypad_disabled(logs) is False

def test_fetches_logs(self, wifi_lock: Lock) -> None:
def test_fetches_logs(self, wifi_lock: mock.Mock) -> None:
with mock.patch.object(wifi_lock, "logs") as logs_mock:
logs_mock.return_value = [
LockLog(
Expand All @@ -321,41 +319,41 @@ def test_fetches_logs(self, wifi_lock: Lock) -> None:
assert wifi_lock.keypad_disabled() is True
wifi_lock.logs.assert_called_once_with()

def test_fetches_logs_no_logs(self, wifi_lock: Lock) -> None:
def test_fetches_logs_no_logs(self, wifi_lock: mock.Mock) -> None:
with mock.patch.object(wifi_lock, "logs") as logs_mock:
logs_mock.return_value = []
assert wifi_lock.keypad_disabled() is False
wifi_lock.logs.assert_called_once_with()


class TestChangedBy:
def test_thumbturn(self, wifi_lock: Lock) -> None:
def test_thumbturn(self, wifi_lock: mock.Mock) -> None:
wifi_lock.lock_state_metadata.action_type = "thumbTurn"
assert wifi_lock.last_changed_by() == "thumbturn"

def test_nfc_device(self, wifi_lock: Lock) -> None:
def test_nfc_device(self, wifi_lock: mock.Mock) -> None:
wifi_lock.lock_state_metadata.action_type = "AppleHomeNFC"
wifi_lock.lock_state_metadata.uuid = "user-uuid"
assert wifi_lock.last_changed_by() == "apple nfc device - asdf"

def test_nfc_device_no_uuid(self, wifi_lock: Lock) -> None:
def test_nfc_device_no_uuid(self, wifi_lock: mock.Mock) -> None:
wifi_lock.lock_state_metadata.action_type = "AppleHomeNFC"
wifi_lock.lock_state_metadata.uuid = None
assert wifi_lock.last_changed_by() == "apple nfc device"

def test_keypad(self, wifi_lock: Lock) -> None:
def test_keypad(self, wifi_lock: mock.Mock) -> None:
wifi_lock.lock_state_metadata.action_type = "accesscode"
wifi_lock.lock_state_metadata.name = "secret code"
assert wifi_lock.last_changed_by() == "keypad - secret code"

def test_mobile_device(self, wifi_lock: Lock) -> None:
def test_mobile_device(self, wifi_lock: mock.Mock) -> None:
wifi_lock.lock_state_metadata.action_type = "virtualKey"
wifi_lock.lock_state_metadata.uuid = "user-uuid"
assert wifi_lock.last_changed_by() == "mobile device - asdf"

def test_unknown(self, wifi_lock: Lock) -> None:
def test_unknown(self, wifi_lock: mock.Mock) -> None:
assert wifi_lock.last_changed_by() == "unknown"

def test_no_metadata(self, wifi_lock: Lock) -> None:
def test_no_metadata(self, wifi_lock: mock.Mock) -> None:
wifi_lock.lock_state_metadata = None
assert wifi_lock.last_changed_by() is None

0 comments on commit b8bdcb9

Please sign in to comment.