Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3f15840

Browse files
authoredMay 15, 2025··
Merge pull request #24 from adafruit/use_ruff
change to ruff
2 parents ca9a18d + 20711f2 commit 3f15840

11 files changed

+181
-490
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 & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +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: 24.10.0
8-
hooks:
9-
- id: black
10-
- repo: https://github.com/fsfe/reuse-tool
11-
rev: v4.0.3
12-
hooks:
13-
- id: reuse
146
- repo: https://github.com/pre-commit/pre-commit-hooks
15-
rev: v5.0.0
7+
rev: v4.5.0
168
hooks:
179
- id: check-yaml
1810
- id: end-of-file-fixer
1911
- id: trailing-whitespace
20-
- id: mixed-line-ending
21-
args:
22-
- --fix=lf
23-
- repo: https://github.com/pycqa/pylint
24-
rev: v3.3.1
12+
- repo: https://github.com/astral-sh/ruff-pre-commit
13+
rev: v0.3.4
2514
hooks:
26-
- id: pylint
27-
name: pylint (library code)
28-
types: [python]
29-
args:
30-
- --disable=consider-using-f-string
31-
exclude: "^(docs/|examples/|tests/|setup.py$)"
32-
- id: pylint
33-
name: pylint (example code)
34-
description: Run pylint rules on "examples/*.py" files
35-
types: [python]
36-
files: "^examples/"
37-
args:
38-
- --disable=missing-docstring,invalid-name,consider-using-f-string,duplicate-code
39-
- id: pylint
40-
name: pylint (test code)
41-
description: Run pylint rules on "tests/*.py" files
42-
types: [python]
43-
files: "^tests/"
44-
args:
45-
- --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
@@ -17,9 +17,9 @@ Introduction
1717
:alt: Build Status
1818

1919

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

2424
CircuitPython module for interacting with the VL53L1X distance sensor.
2525

‎adafruit_vl53l1x.py

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
2727
"""
2828

29-
import time
3029
import struct
30+
import time
31+
3132
from adafruit_bus_device import i2c_device
3233
from micropython import const
3334

@@ -56,23 +57,23 @@
5657

5758
TB_SHORT_DIST = {
5859
# ms: (MACROP_A_HI, MACROP_B_HI)
59-
15: (b"\x00\x1D", b"\x00\x27"),
60-
20: (b"\x00\x51", b"\x00\x6E"),
61-
33: (b"\x00\xD6", b"\x00\x6E"),
62-
50: (b"\x01\xAE", b"\x01\xE8"),
63-
100: (b"\x02\xE1", b"\x03\x88"),
64-
200: (b"\x03\xE1", b"\x04\x96"),
65-
500: (b"\x05\x91", b"\x05\xC1"),
60+
15: (b"\x00\x1d", b"\x00\x27"),
61+
20: (b"\x00\x51", b"\x00\x6e"),
62+
33: (b"\x00\xd6", b"\x00\x6e"),
63+
50: (b"\x01\xae", b"\x01\xe8"),
64+
100: (b"\x02\xe1", b"\x03\x88"),
65+
200: (b"\x03\xe1", b"\x04\x96"),
66+
500: (b"\x05\x91", b"\x05\xc1"),
6667
}
6768

6869
TB_LONG_DIST = {
6970
# ms: (MACROP_A_HI, MACROP_B_HI)
70-
20: (b"\x00\x1E", b"\x00\x22"),
71-
33: (b"\x00\x60", b"\x00\x6E"),
72-
50: (b"\x00\xAD", b"\x00\xC6"),
73-
100: (b"\x01\xCC", b"\x01\xEA"),
74-
200: (b"\x02\xD9", b"\x02\xF8"),
75-
500: (b"\x04\x8F", b"\x04\xA4"),
71+
20: (b"\x00\x1e", b"\x00\x22"),
72+
33: (b"\x00\x60", b"\x00\x6e"),
73+
50: (b"\x00\xad", b"\x00\xc6"),
74+
100: (b"\x01\xcc", b"\x01\xea"),
75+
200: (b"\x02\xd9", b"\x02\xf8"),
76+
500: (b"\x04\x8f", b"\x04\xa4"),
7677
}
7778

7879

@@ -90,13 +91,12 @@ def __init__(self, i2c, address=41):
9091
self.timing_budget = 50
9192

9293
def _sensor_init(self):
93-
# pylint: disable=line-too-long
9494
init_seq = bytes(
9595
[ # value addr : description
9696
0x00, # 0x2d : set bit 2 and 5 to 1 for fast plus mode (1MHz I2C), else don't touch
97-
0x00, # 0x2e : bit 0 if I2C pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD)
98-
0x00, # 0x2f : bit 0 if GPIO pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD)
99-
0x01, # 0x30 : set bit 4 to 0 for active high interrupt and 1 for active low (bits 3:0 must be 0x1), use SetInterruptPolarity()
97+
0x00, # 0x2e : bit 0 if I2C pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD) # noqa: E501
98+
0x00, # 0x2f : bit 0 if GPIO pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD) # noqa: E501
99+
0x01, # 0x30 : set bit 4 to 0 for active high interrupt and 1 for active low (bits 3:0 must be 0x1), use SetInterruptPolarity() # noqa: E501
100100
0x02, # 0x31 : bit 1 = interrupt depending on the polarity
101101
0x00, # 0x32 : not user-modifiable
102102
0x02, # 0x33 : not user-modifiable
@@ -118,7 +118,7 @@ def _sensor_init(self):
118118
0x00, # 0x43 : not user-modifiable
119119
0x00, # 0x44 : not user-modifiable
120120
0x00, # 0x45 : not user-modifiable
121-
0x20, # 0x46 : interrupt configuration 0->level low detection, 1-> level high, 2-> Out of window, 3->In window, 0x20-> New sample ready , TBC
121+
0x20, # 0x46 : interrupt configuration 0->level low detection, 1-> level high, 2-> Out of window, 3->In window, 0x20-> New sample ready , TBC # noqa: E501
122122
0x0B, # 0x47 : not user-modifiable
123123
0x00, # 0x48 : not user-modifiable
124124
0x00, # 0x49 : not user-modifiable
@@ -148,7 +148,7 @@ def _sensor_init(self):
148148
0x01, # 0x61 : not user-modifiable
149149
0xF1, # 0x62 : not user-modifiable
150150
0x0D, # 0x63 : not user-modifiable
151-
0x01, # 0x64 : Sigma threshold MSB (mm in 14.2 format for MSB+LSB), default value 90 mm
151+
0x01, # 0x64 : Sigma threshold MSB (mm in 14.2 format for MSB+LSB), default value 90 mm # noqa: E501
152152
0x68, # 0x65 : Sigma threshold LSB
153153
0x00, # 0x66 : Min count Rate MSB (MCPS in 9.7 format for MSB+LSB)
154154
0x80, # 0x67 : Min count Rate LSB
@@ -206,9 +206,7 @@ def distance(self):
206206
"""The distance in units of centimeters."""
207207
if self._read_register(_VL53L1X_RESULT__RANGE_STATUS)[0] != 0x09:
208208
return None
209-
dist = self._read_register(
210-
_VL53L1X_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD0, 2
211-
)
209+
dist = self._read_register(_VL53L1X_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD0, 2)
212210
dist = struct.unpack(">H", dist)[0]
213211
return dist / 10
214212

@@ -227,10 +225,7 @@ def clear_interrupt(self):
227225
@property
228226
def data_ready(self):
229227
"""Returns true if new data is ready, otherwise false."""
230-
if (
231-
self._read_register(_GPIO__TIO_HV_STATUS)[0] & 0x01
232-
== self._interrupt_polarity
233-
):
228+
if self._read_register(_GPIO__TIO_HV_STATUS)[0] & 0x01 == self._interrupt_polarity:
234229
return True
235230
return False
236231

@@ -287,12 +282,12 @@ def distance_mode(self, mode):
287282
self._write_register(_SD_CONFIG__INITIAL_PHASE_SD0, b"\x06\x06")
288283
elif mode == 2:
289284
# long distance
290-
self._write_register(_PHASECAL_CONFIG__TIMEOUT_MACROP, b"\x0A")
291-
self._write_register(_RANGE_CONFIG__VCSEL_PERIOD_A, b"\x0F")
292-
self._write_register(_RANGE_CONFIG__VCSEL_PERIOD_B, b"\x0D")
293-
self._write_register(_RANGE_CONFIG__VALID_PHASE_HIGH, b"\xB8")
294-
self._write_register(_SD_CONFIG__WOI_SD0, b"\x0F\x0D")
295-
self._write_register(_SD_CONFIG__INITIAL_PHASE_SD0, b"\x0E\x0E")
285+
self._write_register(_PHASECAL_CONFIG__TIMEOUT_MACROP, b"\x0a")
286+
self._write_register(_RANGE_CONFIG__VCSEL_PERIOD_A, b"\x0f")
287+
self._write_register(_RANGE_CONFIG__VCSEL_PERIOD_B, b"\x0d")
288+
self._write_register(_RANGE_CONFIG__VALID_PHASE_HIGH, b"\xb8")
289+
self._write_register(_SD_CONFIG__WOI_SD0, b"\x0f\x0d")
290+
self._write_register(_SD_CONFIG__INITIAL_PHASE_SD0, b"\x0e\x0e")
296291
else:
297292
raise ValueError("Unsupported mode.")
298293
self.timing_budget = self._timing_budget
@@ -318,13 +313,11 @@ def roi_xy(self, data):
318313
if x > 10 or y > 10:
319314
optical_center = 199
320315

321-
self._write_register(
322-
_ROI_CONFIG__USER_ROI_CENTRE_SPAD, optical_center.to_bytes()
323-
)
316+
self._write_register(_ROI_CONFIG__USER_ROI_CENTRE_SPAD, optical_center.to_bytes())
324317
self._write_register(
325318
_ROI_CONFIG__USER_ROI_REQUESTED_GLOBAL_XY_SIZE,
326319
((y - 1) << 4 | (x - 1)).to_bytes(),
327-
)
320+
) # noqa: E501
328321

329322
@property
330323
def roi_center(self):
@@ -355,7 +348,5 @@ def set_address(self, new_address):
355348
multiple VL53L0X sensors on the same I2C bus (SDA & SCL pins). See also the
356349
`example <examples.html#multiple-vl53l1x-on-same-i2c-bus>`_ for proper usage.
357350
"""
358-
self._write_register(
359-
_VL53L1X_I2C_SLAVE_DEVICE_ADDRESS, struct.pack(">B", new_address)
360-
)
351+
self._write_register(_VL53L1X_I2C_SLAVE_DEVICE_ADDRESS, struct.pack(">B", new_address))
361352
self.i2c_device = i2c_device.I2CDevice(self._i2c, new_address)

‎docs/api.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
.. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py)
55
.. use this format as the module name: "adafruit_foo.foo"
66
7+
API Reference
8+
#############
9+
710
.. automodule:: adafruit_vl53l1x
811
:members:

‎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: 2017 Scott Shawcroft, written 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

@@ -55,9 +53,7 @@
5553
creation_year = "2021"
5654
current_year = str(datetime.datetime.now().year)
5755
year_duration = (
58-
current_year
59-
if current_year == creation_year
60-
else creation_year + " - " + current_year
56+
current_year if current_year == creation_year else creation_year + " - " + current_year
6157
)
6258
copyright = year_duration + " Carter Nelson"
6359
author = "Carter Nelson"

‎examples/vl53l1x_displayio_simpletest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
# SPDX-License-Identifier: MIT
77

88
import time
9+
910
import board
1011
from adafruit_display_text.bitmap_label import Label
11-
from terminalio import FONT
1212
from displayio import Group
13+
from terminalio import FONT
14+
1315
import adafruit_vl53l1x
1416

1517
# create a main_group to hold anything we want to show on the display.

‎examples/vl53l1x_set_address_multiple_sensors.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
"""
1313

1414
import time
15+
1516
import board
1617
import digitalio
18+
1719
import adafruit_vl53l1x
1820

1921
# Define the I2C pins.
@@ -64,6 +66,6 @@
6466
# the sensor distance readings for all available sensors.
6567
for sensor_number, sensor in enumerate(vl53l1x):
6668
if sensor.data_ready:
67-
print("Sensor {}: {}".format(sensor_number + 1, sensor.distance))
69+
print(f"Sensor {sensor_number + 1}: {sensor.distance}")
6870
sensor.clear_interrupt()
6971
time.sleep(0.5)

‎examples/vl53l1x_simpletest.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
# Will print the sensed range/distance every second.
88

99
import time
10+
1011
import board
12+
1113
import adafruit_vl53l1x
1214

1315
i2c = board.I2C() # uses board.SCL and board.SDA
@@ -22,23 +24,23 @@
2224
print("VL53L1X Simple Test.")
2325
print("--------------------")
2426
model_id, module_type, mask_rev = vl53.model_info
25-
print("Model ID: 0x{:0X}".format(model_id))
26-
print("Module Type: 0x{:0X}".format(module_type))
27-
print("Mask Revision: 0x{:0X}".format(mask_rev))
27+
print(f"Model ID: 0x{model_id:0X}")
28+
print(f"Module Type: 0x{module_type:0X}")
29+
print(f"Mask Revision: 0x{mask_rev:0X}")
2830
print("Distance Mode: ", end="")
2931
if vl53.distance_mode == 1:
3032
print("SHORT")
3133
elif vl53.distance_mode == 2:
3234
print("LONG")
3335
else:
3436
print("UNKNOWN")
35-
print("Timing Budget: {}".format(vl53.timing_budget))
37+
print(f"Timing Budget: {vl53.timing_budget}")
3638
print("--------------------")
3739

3840
vl53.start_ranging()
3941

4042
while True:
4143
if vl53.data_ready:
42-
print("Distance: {} cm".format(vl53.distance))
44+
print(f"Distance: {vl53.distance} cm")
4345
vl53.clear_interrupt()
4446
time.sleep(1.0)

‎ruff.toml

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
]
105+
106+
[format]
107+
line-ending = "lf"

0 commit comments

Comments
 (0)
Please sign in to comment.