Skip to content

Commit b6bba08

Browse files
authoredMay 16, 2025··
Merge pull request #103 from adafruit/use_ruff
change to ruff
2 parents 87dd7ca + 0a9696d commit b6bba08

File tree

11 files changed

+152
-466
lines changed

11 files changed

+152
-466
lines changed
 

‎.gitattributes

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
.py text eol=lf
6+
.rst text eol=lf
7+
.txt text eol=lf
8+
.yaml text eol=lf
9+
.toml text eol=lf
10+
.license text eol=lf
11+
.md text eol=lf

‎.pre-commit-config.yaml

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,21 @@
1-
# SPDX-FileCopyrightText: 2020 Diego Elio Pettenò
1+
# SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries
22
#
33
# SPDX-License-Identifier: Unlicense
44

55
repos:
6-
- repo: https://github.com/python/black
7-
rev: 23.3.0
8-
hooks:
9-
- id: black
10-
- repo: https://github.com/fsfe/reuse-tool
11-
rev: v1.1.2
12-
hooks:
13-
- id: reuse
146
- repo: https://github.com/pre-commit/pre-commit-hooks
15-
rev: v4.4.0
7+
rev: v4.5.0
168
hooks:
179
- id: check-yaml
1810
- id: end-of-file-fixer
1911
- id: trailing-whitespace
20-
- repo: https://github.com/pycqa/pylint
21-
rev: v2.17.4
12+
- repo: https://github.com/astral-sh/ruff-pre-commit
13+
rev: v0.3.4
2214
hooks:
23-
- id: pylint
24-
name: pylint (library code)
25-
types: [python]
26-
args:
27-
- --disable=consider-using-f-string
28-
exclude: "^(docs/|examples/|tests/|setup.py$)"
29-
- id: pylint
30-
name: pylint (example code)
31-
description: Run pylint rules on "examples/*.py" files
32-
types: [python]
33-
files: "^examples/"
34-
args:
35-
- --disable=missing-docstring,invalid-name,consider-using-f-string,duplicate-code
36-
- id: pylint
37-
name: pylint (test code)
38-
description: Run pylint rules on "tests/*.py" files
39-
types: [python]
40-
files: "^tests/"
41-
args:
42-
- --disable=missing-docstring,consider-using-f-string,duplicate-code
15+
- id: ruff-format
16+
- id: ruff
17+
args: ["--fix"]
18+
- repo: https://github.com/fsfe/reuse-tool
19+
rev: v3.0.1
20+
hooks:
21+
- id: reuse

‎.pylintrc

Lines changed: 0 additions & 399 deletions
This file was deleted.

‎README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ Adafruit CircuitPython BusDevice
1313
:target: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice/actions/
1414
:alt: Build Status
1515

16-
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
17-
:target: https://github.com/psf/black
18-
:alt: Code Style: Black
16+
.. image:: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json
17+
:target: https://github.com/astral-sh/ruff
18+
:alt: Code Style: Ruff
1919

2020
The ``I2CDevice`` and ``SPIDevice`` helper classes make managing transaction state
2121
on a bus easy. For example, they manage locking the bus to prevent other

‎adafruit_bus_device/i2c_device.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
import time
1111

1212
try:
13-
from typing import Optional, Type
1413
from types import TracebackType
15-
from circuitpython_typing import ReadableBuffer, WriteableBuffer
14+
from typing import Optional, Type
1615

1716
# Used only for type annotations.
1817
from busio import I2C
18+
from circuitpython_typing import ReadableBuffer, WriteableBuffer
1919
except ImportError:
2020
pass
2121

@@ -61,9 +61,7 @@ def __init__(self, i2c: I2C, device_address: int, probe: bool = True) -> None:
6161
if probe:
6262
self.__probe_for_device()
6363

64-
def readinto(
65-
self, buf: WriteableBuffer, *, start: int = 0, end: Optional[int] = None
66-
) -> None:
64+
def readinto(self, buf: WriteableBuffer, *, start: int = 0, end: Optional[int] = None) -> None:
6765
"""
6866
Read into ``buf`` from the device. The number of bytes read will be the
6967
length of ``buf``.
@@ -80,9 +78,7 @@ def readinto(
8078
end = len(buf)
8179
self.i2c.readfrom_into(self.device_address, buf, start=start, end=end)
8280

83-
def write(
84-
self, buf: ReadableBuffer, *, start: int = 0, end: Optional[int] = None
85-
) -> None:
81+
def write(self, buf: ReadableBuffer, *, start: int = 0, end: Optional[int] = None) -> None:
8682
"""
8783
Write the bytes from ``buffer`` to the device, then transmit a stop
8884
bit.
@@ -99,7 +95,6 @@ def write(
9995
end = len(buf)
10096
self.i2c.writeto(self.device_address, buf, start=start, end=end)
10197

102-
# pylint: disable-msg=too-many-arguments
10398
def write_then_readinto(
10499
self,
105100
out_buffer: ReadableBuffer,
@@ -108,7 +103,7 @@ def write_then_readinto(
108103
out_start: int = 0,
109104
out_end: Optional[int] = None,
110105
in_start: int = 0,
111-
in_end: Optional[int] = None
106+
in_end: Optional[int] = None,
112107
) -> None:
113108
"""
114109
Write the bytes from ``out_buffer`` to the device, then immediately
@@ -147,8 +142,6 @@ def write_then_readinto(
147142
in_end=in_end,
148143
)
149144

150-
# pylint: enable-msg=too-many-arguments
151-
152145
def __enter__(self) -> "I2CDevice":
153146
while not self.i2c.try_lock():
154147
time.sleep(0)
@@ -180,8 +173,6 @@ def __probe_for_device(self) -> None:
180173
result = bytearray(1)
181174
self.i2c.readfrom_into(self.device_address, result)
182175
except OSError:
183-
# pylint: disable=raise-missing-from
184176
raise ValueError("No I2C device at address: 0x%x" % self.device_address)
185-
# pylint: enable=raise-missing-from
186177
finally:
187178
self.i2c.unlock()

‎adafruit_bus_device/spi_device.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5-
# pylint: disable=too-few-public-methods
6-
75
"""
86
`adafruit_bus_device.spi_device` - SPI Bus Device
97
====================================================
@@ -12,8 +10,8 @@
1210
import time
1311

1412
try:
15-
from typing import Optional, Type
1613
from types import TracebackType
14+
from typing import Optional, Type
1715

1816
# Used only for type annotations.
1917
from busio import SPI
@@ -79,7 +77,7 @@ def __init__(
7977
baudrate: int = 100000,
8078
polarity: int = 0,
8179
phase: int = 0,
82-
extra_clocks: int = 0
80+
extra_clocks: int = 0,
8381
) -> None:
8482
self.spi = spi
8583
self.baudrate = baudrate
@@ -94,9 +92,7 @@ def __init__(
9492
def __enter__(self) -> SPI:
9593
while not self.spi.try_lock():
9694
time.sleep(0)
97-
self.spi.configure(
98-
baudrate=self.baudrate, polarity=self.polarity, phase=self.phase
99-
)
95+
self.spi.configure(baudrate=self.baudrate, polarity=self.polarity, phase=self.phase)
10096
if self.chip_select:
10197
self.chip_select.value = self.cs_active_value
10298
return self.spi

‎docs/api.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11

22
.. If you created a package, create one automodule per module in the package.
33
4+
API Reference
5+
#############
6+
47
.. automodule:: adafruit_bus_device.i2c_device
58
:members:
69

‎docs/conf.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
# -*- coding: utf-8 -*-
2-
31
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
42
#
53
# SPDX-License-Identifier: MIT
64

5+
import datetime
76
import os
87
import sys
9-
import datetime
108

119
sys.path.insert(0, os.path.abspath(".."))
1210

@@ -49,9 +47,7 @@
4947
creation_year = "2016"
5048
current_year = str(datetime.datetime.now().year)
5149
year_duration = (
52-
current_year
53-
if current_year == creation_year
54-
else creation_year + " - " + current_year
50+
current_year if current_year == creation_year else creation_year + " - " + current_year
5551
)
5652
copyright = year_duration + " Scott Shawcroft and Tony Dicola for Adafruit Industries"
5753
author = "Scott Shawcroft and Tony Dicola"

‎examples/busdevice_read_register_i2c_simpletest.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import busio
54
import board
5+
import busio
6+
67
from adafruit_bus_device.i2c_device import I2CDevice
78

89
DEVICE_ADDRESS = 0x68 # device address of DS3231 board
@@ -17,4 +18,4 @@
1718
result = bytearray(1)
1819
bus_device.readinto(result)
1920

20-
print("".join("{:02x}".format(x) for x in result))
21+
print("".join(f"{x:02x}" for x in result))
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import busio
54
import board
5+
import busio
66
import digitalio
7+
78
from adafruit_bus_device.spi_device import SPIDevice
89

910
A_DEVICE_REGISTER = 0xD0 # device id register on the BMP280 board
@@ -13,10 +14,9 @@
1314
comm_port = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
1415
device = SPIDevice(comm_port, cs)
1516

16-
# pylint: disable-msg=no-member
1717
with device as bus_device:
1818
bus_device.write(bytes([A_DEVICE_REGISTER]))
1919
result = bytearray(1)
2020
bus_device.readinto(result)
2121

22-
print("".join("{:02x}".format(x) for x in result))
22+
print("".join(f"{x:02x}" for x in result))

‎ruff.toml

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
target-version = "py38"
6+
line-length = 100
7+
8+
[lint]
9+
preview = true
10+
select = ["I", "PL", "UP"]
11+
12+
extend-select = [
13+
"D419", # empty-docstring
14+
"E501", # line-too-long
15+
"W291", # trailing-whitespace
16+
"PLC0414", # useless-import-alias
17+
"PLC2401", # non-ascii-name
18+
"PLC2801", # unnecessary-dunder-call
19+
"PLC3002", # unnecessary-direct-lambda-call
20+
"E999", # syntax-error
21+
"PLE0101", # return-in-init
22+
"F706", # return-outside-function
23+
"F704", # yield-outside-function
24+
"PLE0116", # continue-in-finally
25+
"PLE0117", # nonlocal-without-binding
26+
"PLE0241", # duplicate-bases
27+
"PLE0302", # unexpected-special-method-signature
28+
"PLE0604", # invalid-all-object
29+
"PLE0605", # invalid-all-format
30+
"PLE0643", # potential-index-error
31+
"PLE0704", # misplaced-bare-raise
32+
"PLE1141", # dict-iter-missing-items
33+
"PLE1142", # await-outside-async
34+
"PLE1205", # logging-too-many-args
35+
"PLE1206", # logging-too-few-args
36+
"PLE1307", # bad-string-format-type
37+
"PLE1310", # bad-str-strip-call
38+
"PLE1507", # invalid-envvar-value
39+
"PLE2502", # bidirectional-unicode
40+
"PLE2510", # invalid-character-backspace
41+
"PLE2512", # invalid-character-sub
42+
"PLE2513", # invalid-character-esc
43+
"PLE2514", # invalid-character-nul
44+
"PLE2515", # invalid-character-zero-width-space
45+
"PLR0124", # comparison-with-itself
46+
"PLR0202", # no-classmethod-decorator
47+
"PLR0203", # no-staticmethod-decorator
48+
"UP004", # useless-object-inheritance
49+
"PLR0206", # property-with-parameters
50+
"PLR0904", # too-many-public-methods
51+
"PLR0911", # too-many-return-statements
52+
"PLR0912", # too-many-branches
53+
"PLR0913", # too-many-arguments
54+
"PLR0914", # too-many-locals
55+
"PLR0915", # too-many-statements
56+
"PLR0916", # too-many-boolean-expressions
57+
"PLR1702", # too-many-nested-blocks
58+
"PLR1704", # redefined-argument-from-local
59+
"PLR1711", # useless-return
60+
"C416", # unnecessary-comprehension
61+
"PLR1733", # unnecessary-dict-index-lookup
62+
"PLR1736", # unnecessary-list-index-lookup
63+
64+
# ruff reports this rule is unstable
65+
#"PLR6301", # no-self-use
66+
67+
"PLW0108", # unnecessary-lambda
68+
"PLW0120", # useless-else-on-loop
69+
"PLW0127", # self-assigning-variable
70+
"PLW0129", # assert-on-string-literal
71+
"B033", # duplicate-value
72+
"PLW0131", # named-expr-without-context
73+
"PLW0245", # super-without-brackets
74+
"PLW0406", # import-self
75+
"PLW0602", # global-variable-not-assigned
76+
"PLW0603", # global-statement
77+
"PLW0604", # global-at-module-level
78+
79+
# fails on the try: import typing used by libraries
80+
#"F401", # unused-import
81+
82+
"F841", # unused-variable
83+
"E722", # bare-except
84+
"PLW0711", # binary-op-exception
85+
"PLW1501", # bad-open-mode
86+
"PLW1508", # invalid-envvar-default
87+
"PLW1509", # subprocess-popen-preexec-fn
88+
"PLW2101", # useless-with-lock
89+
"PLW3301", # nested-min-max
90+
]
91+
92+
ignore = [
93+
"PLR2004", # magic-value-comparison
94+
"UP030", # format literals
95+
"PLW1514", # unspecified-encoding
96+
"PLR0913", # too-many-arguments
97+
"PLR0915", # too-many-statements
98+
"PLR0917", # too-many-positional-arguments
99+
"PLR0904", # too-many-public-methods
100+
"PLR0912", # too-many-branches
101+
"PLR0916", # too-many-boolean-expressions
102+
"PLR6301", # could-be-static no-self-use
103+
"PLC0415", # import outside toplevel
104+
"PLC2701", # private import
105+
]
106+
107+
[format]
108+
line-ending = "lf"

0 commit comments

Comments
 (0)
Please sign in to comment.