Skip to content

Commit

Permalink
chore: latest pre-commit hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
elagil committed Mar 6, 2023
1 parent cb61a39 commit dd49fdb
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
python-version: '3.10'

- name: Install dependencies
run: |
Expand Down
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ repos:
- id: reorder-python-imports

- repo: https://github.com/psf/black
rev: 22.10.0
rev: 23.1.0
hooks:
- id: black

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
Expand All @@ -30,26 +30,26 @@ repos:
- id: codespell

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.982
rev: v1.0.1
hooks:
- id: mypy
additional_dependencies: [types-protobuf, types-PyYAML, types-retry]

- repo: https://github.com/PyCQA/pydocstyle
rev: 6.1.1
rev: 6.3.0
hooks:
- id: pydocstyle
additional_dependencies: [toml]
args:
- --convention=google

- repo: https://github.com/PyCQA/flake8
rev: 5.0.4
rev: 6.0.0
hooks:
- id: flake8

- repo: https://github.com/PyCQA/pylint
rev: v2.15.5
rev: v2.16.4
hooks:
- id: pylint
args:
Expand Down
7 changes: 2 additions & 5 deletions sigmadsp/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from typing import Callable
from typing import Dict

import grpc
import grpc # type: ignore
from retry.api import retry_call

import sigmadsp
Expand Down Expand Up @@ -395,10 +395,7 @@ def main():
"-s",
"--settings",
required=False,
help=(
"specifies the settings file path for configuring ",
"the sigmadsp backend application",
),
help=("specifies the settings file path for configuring the sigmadsp backend application"),
)
arguments = argument_parser.parse_args()

Expand Down
2 changes: 1 addition & 1 deletion sigmadsp/dsp/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from typing import Literal
from typing import Union

import gpiozero
import gpiozero # type: ignore

from sigmadsp.helper.conversion import bytes_to_int32
from sigmadsp.helper.conversion import clamp
Expand Down
4 changes: 2 additions & 2 deletions sigmadsp/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from ipaddress import IPv4Address
from typing import Union

import click
import grpc
import click # type: ignore
import grpc # type: ignore

from sigmadsp.generated.backend_service.control_pb2 import ControlParameterRequest
from sigmadsp.generated.backend_service.control_pb2 import ControlRequest
Expand Down
30 changes: 15 additions & 15 deletions sigmadsp/helper/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def bytes_to_int(data: bytes, offset: int = 0, length: Union[int, None] = None)
where the complete length of data (after offset) is used.
Returns:
int: Integer representation of the input data stream
int: Integer representation of the input data stream.
"""
if length is not None:
return int.from_bytes(data[offset : offset + length], byteorder=SIGMADSP_ENDIANNESS)
Expand All @@ -158,11 +158,11 @@ def bytes_to_int8(data: bytes, offset: int = 0) -> int:
"""Convert one byte to an 8 bit integer value.
Args:
data (bytes): Input byte
offset (int, optional): Offset in number of bytes, from the beginning of the data buffer
data (bytes): Input byte.
offset (int, optional): Offset in number of bytes, from the beginning of the data buffer.
Returns:
int: 8 bit integer representation of the input data stream
int: 8 bit integer representation of the input data stream.
"""
return bytes_to_int(data, offset, length=1)

Expand All @@ -171,11 +171,11 @@ def bytes_to_int16(data: bytes, offset: int = 0) -> int:
"""Convert two bytes to a 16 bit integer value.
Args:
data (bytes): Input bytes
offset (int, optional): Offset in number of bytes, from the beginning of the data buffer
data (bytes): Input bytes.
offset (int, optional): Offset in number of bytes, from the beginning of the data buffer.
Returns:
int: 16 bit integer representation of the input data stream
int: 16 bit integer representation of the input data stream.
"""
return bytes_to_int(data, offset, length=2)

Expand All @@ -184,23 +184,23 @@ def bytes_to_int32(data: bytes, offset: int = 0) -> int:
"""Convert four bytes to a 32 bit integer value.
Args:
data (bytes): Input bytes
offset (int, optional): Offset in number of bytes, from the beginning of the data buffer
data (bytes): Input bytes.
offset (int, optional): Offset in number of bytes, from the beginning of the data buffer.
Returns:
int: 32 bit integer representation of the input data stream
int: 32 bit integer representation of the input data stream.
"""
return bytes_to_int(data, offset, length=4)


def int_to_bytes(value: int, buffer: bytearray = None, offset: int = 0, length: int = 1):
def int_to_bytes(value: int, buffer: bytearray | None = None, offset: int = 0, length: int = 1):
"""Fill a buffer with values. If no buffer is provided, a new one is created.
Args:
buffer (bytearray): The buffer to fill
value (int): The value to pack into the buffer
offset (int, optional): Offset in number of bytes, from the beginning of the data buffer
length (int): Number of bytes to be written
buffer (bytearray | None): The buffer to fill, or None, for creating a new one.
value (int): The value to pack into the buffer.
offset (int, optional): Offset in number of bytes, from the beginning of the data buffer.
length (int): Number of bytes to be written.
"""
if buffer is None:
buffer = bytearray(length + offset)
Expand Down
2 changes: 0 additions & 2 deletions sigmadsp/helper/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,9 @@ def extract_cell(self, cell_lines: List[str]) -> Union[Cell, None]:

if split_line:
if split_line[0] == "Cell" and split_line[1] == "Name":

name = " ".join(split_line[3:])

elif split_line[0] == "Parameter":

if split_line[1] == "Name":
parameter_name = " ".join(split_line[3:])

Expand Down
4 changes: 2 additions & 2 deletions sigmadsp/helper/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ class SigmadspSettings:

default_config_path = "/var/lib/sigmadsp/config.yaml"

def __init__(self, config_path: str = None):
def __init__(self, config_path: str | None = None):
"""Load a config file in *.yaml format from a specified path.
If no file is provided, the default path is used for loading settings.
Args:
config_path (str, optional): The input path of the settings file.
config_path (str | None, optional): The input path of the settings file.
Defaults to None.
"""
if config_path is None:
Expand Down
3 changes: 3 additions & 0 deletions sigmadsp/protocols/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
class DspProtocol(ABC):
"""Base class for communication handlers talking to SigmaDSP chipsets."""

transmit_queue: Queue
receive_queue: Queue

def run(self):
"""Start the DSP protocol thread."""
# Generate queues, for communicating with the protocol handler thread within this class.
Expand Down
4 changes: 2 additions & 2 deletions sigmadsp/protocols/i2c.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""This module implements an I2C handler that talks to Sigma DSP devices."""
import logging

from smbus2 import i2c_msg
from smbus2 import SMBus
from smbus2 import i2c_msg # type: ignore
from smbus2 import SMBus # type: ignore

from .common import DspProtocol
from sigmadsp.helper.conversion import int16_to_bytes
Expand Down
2 changes: 1 addition & 1 deletion sigmadsp/protocols/spi.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""This module implements an SPI handler that talks to Sigma DSP devices."""
import logging

import spidev
import spidev # type: ignore

from .common import DspProtocol

Expand Down
28 changes: 16 additions & 12 deletions sigmadsp/sigmastudio/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class OperationKey(Enum):
WRITE_KEY = 0x09


ValidFieldNames = Literal[
FieldName = Literal[
"operation", "safeload", "channel", "total_length", "chip_address", "data_length", "address", "success", "reserved"
]

Expand All @@ -37,7 +37,7 @@ class Field:
"""A class that represents a single field in the header."""

# The name of the field.
name: ValidFieldNames
name: FieldName

# The offset of the field in bytes from the start of the header.
offset: int
Expand Down Expand Up @@ -159,7 +159,7 @@ def _sort_fields_by_offset(self):
"""Sorts the fields in this header by their offset."""
self._fields = OrderedDict(sorted(self._fields.items(), key=lambda item: item[1].offset))

def add(self, name: ValidFieldNames, offset: int, size: int):
def add(self, name: FieldName, offset: int, size: int):
"""Create and add a new field.
Args:
Expand Down Expand Up @@ -246,7 +246,7 @@ def __iter__(self) -> Iterator[Field]:
for item in self._fields.values():
yield item

def __setitem__(self, name: ValidFieldNames, value: Union[int, bytes, bytearray]):
def __setitem__(self, name: FieldName, value: Union[int, bytes, bytearray]):
"""Set a field value.
Args:
Expand All @@ -259,7 +259,7 @@ def __setitem__(self, name: ValidFieldNames, value: Union[int, bytes, bytearray]
# FIXME: Type is correct, see https://github.com/python/mypy/issues/3004.
self._fields[name].value = value # type: ignore

def __getitem__(self, name: ValidFieldNames) -> Field:
def __getitem__(self, name: FieldName) -> Field:
"""Get a field by its name.
Args:
Expand All @@ -273,7 +273,7 @@ def __getitem__(self, name: ValidFieldNames) -> Field:
"""
return self._fields[name]

def __contains__(self, name: ValidFieldNames) -> bool:
def __contains__(self, name: FieldName) -> bool:
"""Magic methods for using ``in``.
Args:
Expand Down Expand Up @@ -303,13 +303,15 @@ def _new_read_request_header() -> PacketHeader:
def _new_read_response_header() -> PacketHeader:
"""Generate a new header for a read response packet."""

def new_header_from_operation_byte(self, operation_byte: bytes, template: PacketHeader = None) -> PacketHeader:
def new_header_from_operation_byte(
self, operation_byte: bytes, template: PacketHeader | None = None
) -> PacketHeader:
"""Generate a header from an operation byte.
Args:
operation_key (bytes): The operation byte that determines the header composition.
template (PacketHeader, optional): The template, if any, from which to copy fields that fit the new header.
Defaults to None.
template (PacketHeader | None, optional): The template, if any, from which to copy fields that fit the
new header. Defaults to None.
Returns:
PacketHeader: The new packet header.
Expand Down Expand Up @@ -338,13 +340,15 @@ def new_header_from_operation_byte(self, operation_byte: bytes, template: Packet

return header

def new_header_from_operation_key(self, operation_key: OperationKey, template: PacketHeader = None) -> PacketHeader:
def new_header_from_operation_key(
self, operation_key: OperationKey, template: PacketHeader | None = None
) -> PacketHeader:
"""Generate a header from an operation key.
Args:
operation_key (OperationKey): The operation key that determines the header composition.
template (PacketHeader, optional): The template, if any, from which to copy fields that fit the new header.
Defaults to None.
template (PacketHeader | None, optional): The template, if any, from which to copy fields that fit the
new header. Defaults to None.
Returns:
PacketHeader: The new packet header.
Expand Down
2 changes: 1 addition & 1 deletion tests/application/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import Tuple
from typing import Union

from pytest import fixture
from pytest import fixture # type: ignore

from sigmadsp.backend import BackendService
from sigmadsp.dsp.adau14xx import Adau14xx
Expand Down
4 changes: 2 additions & 2 deletions tests/dsp/test_adau14xx.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from typing import Callable
from typing import Union

from hypothesis import given
from hypothesis.strategies import floats
from hypothesis import given # type: ignore
from hypothesis.strategies import floats # type: ignore

from sigmadsp.dsp.factory import dsp_factory
from sigmadsp.dsp.factory import VALID_ADAU14XX
Expand Down
4 changes: 2 additions & 2 deletions tests/helper/test_conversion.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Test conversion functions."""
import pytest
import pytest # type: ignore

from sigmadsp.helper.conversion import clamp


@pytest.mark.parametrize("lower,upper,input_value,expected", [(-10, 20, 8, 8), (-5, 32, 34, 32), (-12, 0, -123, -12)])
def test_clamp(lower: float, upper: float, input_value: float, expected: float):
def test_clamp(lower: float, upper: float, input_value: float, expected: float) -> None:
"""Test the clamping function.
Args:
Expand Down
3 changes: 2 additions & 1 deletion tests/helper/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
TEST_FILE_PATH = os.path.join(os.path.dirname(__file__), "test_parameter_file.params")


def test_parser():
def test_parser() -> None:
"""Test the parser for correct extraction of parameters from a file."""
p = Parser()
p.run(TEST_FILE_PATH)
Expand All @@ -26,6 +26,7 @@ def test_parser():

# Test safety hash register
safety_hash = p.safety_hash_cell
assert safety_hash is not None
assert safety_hash.is_safety_hash
assert not safety_hash.is_volume_cell
assert not safety_hash.is_adjustable
Expand Down
2 changes: 1 addition & 1 deletion tests/mock/dummy_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self):
self.reset()
self.run()

def reset(self):
def reset(self) -> None:
"""Empty all recorded data."""
self._memory: Dict[int, bytes] = {}
self._write_requests = []
Expand Down
Loading

0 comments on commit dd49fdb

Please sign in to comment.