Skip to content

Commit 74cac28

Browse files
committed
Adding ruff as a pre-commit linter.
Adding a pre-commit hook to check the contents of pyproject.toml.
1 parent b6a62e0 commit 74cac28

File tree

6 files changed

+74
-49
lines changed

6 files changed

+74
-49
lines changed

.pre-commit-config.yaml

+42-29
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ repos:
1717
- id: check-case-conflict
1818
- id: check-ast
1919

20+
# Verify the contents of pyproject.toml
21+
- repo: https://github.com/abravalheri/validate-pyproject
22+
rev: v0.13
23+
hooks:
24+
- id: validate-pyproject
25+
2026
# Updating code to use modern python patterns where available.
2127
- repo: https://github.com/asottile/pyupgrade
2228
rev: v3.4.0
@@ -60,6 +66,42 @@ repos:
6066
'--exclude=conf.py' # Ignore the sphinx config as this is used by an external tool.
6167
]
6268

69+
# Lint documentation source.
70+
- repo: https://github.com/sphinx-contrib/sphinx-lint
71+
# Make sure to also update the additional dependency pin
72+
rev: v0.6.7
73+
hooks:
74+
- id: sphinx-lint
75+
76+
# lint documentation
77+
- repo: https://github.com/PyCQA/doc8
78+
rev: v1.1.1
79+
hooks:
80+
- id: doc8
81+
82+
# Lint docstrings.
83+
- repo: https://github.com/PyCQA/pydocstyle
84+
# Waiting for a 3.10 release on pypi
85+
rev: "6.3.0"
86+
hooks:
87+
- id: pydocstyle
88+
additional_dependencies:
89+
# TOML support only added natively in python 3.11
90+
- toml
91+
92+
# Static typehint linting.
93+
- repo: https://github.com/pre-commit/mirrors-mypy
94+
rev: v1.3.0
95+
hooks:
96+
- id: mypy
97+
98+
# A faster python linter
99+
- repo: https://github.com/charliermarsh/ruff-pre-commit
100+
# Ruff version.
101+
rev: 'v0.0.269'
102+
hooks:
103+
- id: ruff
104+
63105
# Lint code using libs in project venv.
64106
- repo: local
65107
hooks:
@@ -92,32 +134,3 @@ repos:
92134
- id: bandit
93135
# Assert is used exclusively in pytest.
94136
args: ['--skip=B101']
95-
96-
# Lint documentation source.
97-
- repo: https://github.com/sphinx-contrib/sphinx-lint
98-
# Make sure to also update the additional dependency pin
99-
rev: v0.6.7
100-
hooks:
101-
- id: sphinx-lint
102-
103-
# lint documentation
104-
- repo: https://github.com/PyCQA/doc8
105-
rev: v1.1.1
106-
hooks:
107-
- id: doc8
108-
109-
# Lint docstrings.
110-
- repo: https://github.com/PyCQA/pydocstyle
111-
# Waiting for a 3.10 release on pypi
112-
rev: "6.3.0"
113-
hooks:
114-
- id: pydocstyle
115-
additional_dependencies:
116-
# TOML support only added natively in python 3.11
117-
- toml
118-
119-
# Static typehint linting.
120-
- repo: https://github.com/pre-commit/mirrors-mypy
121-
rev: v1.3.0
122-
hooks:
123-
- id: mypy

docs/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
sys.path.insert(0, os.path.abspath("../"))
1414

15-
from uoshardware import PROJECT, __author__, __copyright__, __version__
15+
from uoshardware import PROJECT, __author__, __copyright__, __version__ # noqa
1616

1717
project = PROJECT
1818
# Copyright name shadowed by sphinx design.

tests/test_abstractions.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ def test_get_npc_checksum(test_packet_data: tuple, expected_lrc: int):
116116
def test_get_npc_packet(test_packet: Packet):
117117
"""Checks packets are formed correctly from some known data."""
118118
print(
119-
f"\n -> addr_to: {test_packet.address_to}, addr_from: {test_packet.address_from}, "
119+
f"\n -> addr_to: {test_packet.address_to}, "
120+
f"addr_from: {test_packet.address_from}, "
120121
f"payload: {test_packet.payload}, packet: {test_packet.binary!r}"
121122
)
122123
assert (

uoshardware/abstractions.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ def execute_instruction(self, packet: NPCPacket) -> ComResult: # dead: disable
190190
the action.
191191
"""
192192
raise UOSUnsupportedError(
193-
f"UOSInterfaces must over-ride {UOSInterface.execute_instruction.__name__} prototype."
193+
"UOSInterfaces must over-ride "
194+
f"{UOSInterface.execute_instruction.__name__} prototype."
194195
)
195196

196197
@abstractmethod
@@ -208,7 +209,8 @@ def read_response(
208209
the action.
209210
"""
210211
raise UOSUnsupportedError(
211-
f"UOSInterfaces must over-ride {UOSInterface.read_response.__name__} prototype."
212+
"UOSInterfaces must over-ride "
213+
f"{UOSInterface.read_response.__name__} prototype."
212214
)
213215

214216
@abstractmethod
@@ -222,7 +224,8 @@ def hard_reset(self) -> ComResult:
222224
the action.
223225
"""
224226
raise UOSUnsupportedError(
225-
f"UOSInterfaces must over-ride {UOSInterface.hard_reset.__name__} prototype"
227+
"UOSInterfaces must over-ride "
228+
f"{UOSInterface.hard_reset.__name__} prototype"
226229
)
227230

228231
@abstractmethod
@@ -235,7 +238,7 @@ def open(self):
235238
the action.
236239
"""
237240
raise UOSUnsupportedError(
238-
f"UOSInterfaces must over-ride {UOSInterface.open.__name__} prototype."
241+
"UOSInterfaces must over-ride " f"{UOSInterface.open.__name__} prototype."
239242
)
240243

241244
@abstractmethod
@@ -248,7 +251,7 @@ def close(self):
248251
the action.
249252
"""
250253
raise UOSUnsupportedError(
251-
f"UOSInterfaces must over-ride {UOSInterface.close.__name__} prototype."
254+
"UOSInterfaces must over-ride " f"{UOSInterface.close.__name__} prototype."
252255
)
253256

254257
@abstractmethod
@@ -260,7 +263,7 @@ def is_active(self) -> bool:
260263
correctly.
261264
"""
262265
raise UOSUnsupportedError(
263-
f"UOSInterfaces must over-ride {UOSInterface.close.__name__} prototype."
266+
"UOSInterfaces must over-ride " f"{UOSInterface.close.__name__} prototype."
264267
)
265268

266269
@staticmethod
@@ -273,7 +276,8 @@ def enumerate_devices() -> list:
273276
correctly.
274277
"""
275278
raise UOSUnsupportedError(
276-
f"UOSInterfaces must over-ride {UOSInterface.enumerate_devices.__name__} prototype."
279+
"UOSInterfaces must over-ride "
280+
f"{UOSInterface.enumerate_devices.__name__} prototype."
277281
)
278282

279283

@@ -363,7 +367,8 @@ def update_adc_samples(self, result: ComResult):
363367
for sample_index, pin in enumerate(result.tx_packet.payload):
364368
if pin not in self.pins:
365369
raise UOSRuntimeError(
366-
f"Can't update ADC samples on pin {pin} as it's invalid for {self.name}."
370+
f"Can't update ADC samples on pin {pin} "
371+
f"as it's invalid for {self.name}."
367372
)
368373
self.pins[pin].adc_reading = ADCSample(
369374
sample_values[sample_index * 2 : sample_index * 2 + 2],
@@ -389,7 +394,8 @@ def update_gpio_samples(self, result: ComResult):
389394
pin = result.tx_packet.payload[2 * sample_index]
390395
if pin not in self.pins:
391396
raise UOSRuntimeError(
392-
f"Can't update GPIO samples on pin {pin} as it's invalid for {self.name}."
397+
f"Can't update GPIO samples on pin {pin} "
398+
f"as it's invalid for {self.name}."
393399
)
394400
self.pins[pin].gpio_reading = DigitalSample(sample_values[sample_index])
395401
logger.debug(

uoshardware/api.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ def enumerate_system_devices( # dead: disable
2828
) -> list:
2929
"""Iterate through all interfaces and locates available devices.
3030
31-
:param interface_filter: Interface enum to limit the search to a single interface type.
31+
:param interface_filter: Interface enum to limit the search to a
32+
single interface type.
3233
:return: A list of uosinterface objects.
3334
"""
3435
system_devices = []
@@ -80,10 +81,12 @@ def __init__(
8081
"""Instantiate a UOS device instance for communication.
8182
8283
:param identity: Specify the type of device, this must exist in the device LUT.
83-
:param address: Compliant connection string for identifying the device and interface.
84+
:param address: Compliant connection string for identifying the device and
85+
interface.
8486
:param interface: Set the type of interface to use for communication.
8587
loading: Alter the loading strategy for managing the communication.
86-
:param kwargs: Additional optional connection parameters as defined in documentation.
88+
:param kwargs: Additional optional connection parameters as defined in
89+
documentation.
8790
"""
8891
self.address = address
8992
self.__kwargs = kwargs
@@ -137,9 +140,11 @@ def set_gpio_output(
137140
) -> ComResult:
138141
"""Set a pin to digital output mode and sets a level on that pin.
139142
140-
:param pin: The numeric number of the pin as defined in the dictionary for that device.
143+
:param pin: The numeric number of the pin as defined in the dictionary
144+
for that device.
141145
:param level: The output level, 0 - low, 1 - High.
142-
:param volatility: How volatile should the command be, use constants from uoshardware.
146+
:param volatility: How volatile should the command be, use constants
147+
from uoshardware.
143148
:return: ComResult object.
144149
"""
145150
return self.__execute_instruction(
@@ -239,7 +244,8 @@ def open(self):
239244
def close(self):
240245
"""Release connection, must be called explicitly if loading is eager.
241246
242-
:raises: UOSCommunicationError - Problem closing the connection to an active device.
247+
:raises: UOSCommunicationError - Problem closing the connection
248+
to an active device.
243249
"""
244250
self.__device_interface.close()
245251

uoshardware/interface/serial.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,8 @@ def read_response(self, expect_packets: int, timeout_s: float):
137137
byte_index = -1 # tracks the byte position index of the current packet
138138
packet_index = 0 # tracks the packet number being received 0 = ACK
139139
try:
140-
while (
141-
timeout_s * 1000000000
142-
) > time_ns() - start_ns and byte_index > -2: # read until packet or timeout
140+
while (timeout_s * 1000000000) > time_ns() - start_ns and byte_index > -2:
141+
# read until packet or timeout
143142
num_bytes = self._device.in_waiting
144143
for _ in range(num_bytes):
145144
byte_in = self._device.read(1)

0 commit comments

Comments
 (0)