Skip to content

Commit 99806b5

Browse files
committed
all: Lint Python code with ruff.
Signed-off-by: Christian Clauss <cclauss@me.com>
1 parent c113611 commit 99806b5

File tree

18 files changed

+128
-44
lines changed

18 files changed

+128
-44
lines changed

.github/workflows/build_packages.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ jobs:
99
build:
1010
runs-on: ubuntu-latest
1111
steps:
12-
- uses: actions/checkout@v2
13-
- uses: actions/setup-python@v1
12+
- uses: actions/checkout@v3
13+
- uses: actions/setup-python@v4
14+
with: {python-version: 3.x}
1415
- name: Setup environment
1516
run: source tools/ci.sh && ci_build_packages_setup
1617
- name: Check manifest files

.github/workflows/cleanup_published_packages.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ jobs:
77
runs-on: ubuntu-latest
88
if: vars.MICROPY_PUBLISH_MIP_INDEX
99
steps:
10-
- uses: actions/checkout@v2
10+
- uses: actions/checkout@v3
1111
- name: Clean up published files
1212
run: source tools/ci.sh && ci_cleanup_package_index ${{ github.event.ref }}

.github/workflows/code_formatting.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ jobs:
66
build:
77
runs-on: ubuntu-latest
88
steps:
9-
- uses: actions/checkout@v2
10-
- uses: actions/setup-python@v1
9+
- uses: actions/checkout@v3
10+
- run: pip install --user ruff
11+
- run: ruff .
12+
- uses: actions/setup-python@v4
13+
with: {python-version: 3.x}
1114
- name: Install packages
1215
run: source tools/ci.sh && ci_code_formatting_setup
1316
- name: Run code formatting

micropython/drivers/codec/wm8960/wm8960.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ def set_data_route(self, route):
578578
raise ValueError("Invalid route")
579579

580580
def set_left_input(self, input):
581-
if not input in self._input_config_table.keys():
581+
if input not in self._input_config_table:
582582
raise ValueError("Invalid input")
583583

584584
input = self._input_config_table[input]
@@ -595,7 +595,7 @@ def set_left_input(self, input):
595595
regs[_LINVOL] = input[1]
596596

597597
def set_right_input(self, input):
598-
if not input in self._input_config_table.keys():
598+
if input not in self._input_config_table:
599599
raise ValueError("Invalid input name")
600600

601601
input = self._input_config_table[input]
@@ -629,7 +629,7 @@ def config_data_format(self, sysclk, sample_rate, bits):
629629
self.regs[_IFACE1] = (_IFACE1_WL_MASK, wl << _IFACE1_WL_SHIFT)
630630

631631
def volume(self, module, volume_l=None, volume_r=None):
632-
if not module in self._volume_config_table.keys():
632+
if module not in self._volume_config_table:
633633
raise ValueError("Invalid module")
634634

635635
if volume_l is None: # get volume
@@ -644,7 +644,7 @@ def volume(self, module, volume_l=None, volume_r=None):
644644

645645
if not ((0 <= volume_l <= 100) and (0 <= volume_r <= 100)):
646646
raise ValueError("Invalid value for volume")
647-
elif not module in self._volume_config_table.keys():
647+
elif module not in self._volume_config_table:
648648
raise ValueError("Invalid module")
649649

650650
vol_max, regnum, flags = self._volume_config_table[module]

micropython/drivers/imu/bmi270/bmi270.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -524,13 +524,13 @@ def __init__(
524524
# Sanity checks
525525
if not self._use_i2c:
526526
raise ValueError("SPI mode is not supported")
527-
if not gyro_odr in ODR:
527+
if gyro_odr not in ODR:
528528
raise ValueError("Invalid gyro sampling rate: %d" % gyro_odr)
529-
if not gyro_scale in GYRO_SCALE:
529+
if gyro_scale not in GYRO_SCALE:
530530
raise ValueError("Invalid gyro scaling: %d" % gyro_scale)
531-
if not accel_odr in ODR:
531+
if accel_odr not in ODR:
532532
raise ValueError("Invalid accelerometer sampling rate: %d" % accel_odr)
533-
if not accel_scale in ACCEL_SCALE:
533+
if accel_scale not in ACCEL_SCALE:
534534
raise ValueError("Invalid accelerometer scaling: %d" % accel_scale)
535535
if self._read_reg(_CHIP_ID) != 0x24:
536536
raise OSError("No BMI270 device was found at address 0x%x" % (self.address))

micropython/drivers/imu/bmm150/bmm150.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def __init__(
8080
# Sanity checks
8181
if not self._use_i2c:
8282
raise ValueError("SPI mode is not supported")
83-
if not magnet_odr in _ODR:
83+
if magnet_odr not in _ODR:
8484
raise ValueError("Invalid sampling rate: %d" % magnet_odr)
8585

8686
# Perform soft reset, and power on.

micropython/drivers/imu/lsm6dsox/lsm6dsox.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,20 @@ def __init__(
130130
accel_odr = round(accel_odr, 2)
131131

132132
# Sanity checks
133-
if not gyro_odr in ODR:
133+
if gyro_odr not in ODR:
134134
raise ValueError("Invalid sampling rate: %d" % gyro_odr)
135-
if not gyro_scale in SCALE_GYRO:
135+
if gyro_scale not in SCALE_GYRO:
136136
raise ValueError("invalid gyro scaling: %d" % gyro_scale)
137-
if not accel_odr in ODR:
137+
if accel_odr not in ODR:
138138
raise ValueError("Invalid sampling rate: %d" % accel_odr)
139-
if not accel_scale in SCALE_ACCEL:
139+
if accel_scale not in SCALE_ACCEL:
140140
raise ValueError("invalid accelerometer scaling: %d" % accel_scale)
141141

142142
# Soft-reset the device.
143143
self.reset()
144144

145145
# Load and configure MLC if UCF file is provided
146-
if ucf != None:
146+
if ucf is not None:
147147
self.load_mlc(ucf)
148148

149149
# Set Gyroscope datarate and scale.

micropython/drivers/imu/lsm6dsox/lsm6dsox_mlc.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def imu_int_handler(pin):
1717
INT_FLAG = True
1818

1919

20-
if INT_MODE == True:
20+
if INT_MODE is True:
2121
int_pin = Pin(24)
2222
int_pin.irq(handler=imu_int_handler, trigger=Pin.IRQ_RISING)
2323

@@ -44,5 +44,5 @@ def imu_int_handler(pin):
4444
print(UCF_LABELS[lsm.mlc_output()[0]])
4545
else:
4646
buf = lsm.mlc_output()
47-
if buf != None:
47+
if buf is not None:
4848
print(UCF_LABELS[buf[0]])

micropython/drivers/imu/lsm9ds1/lsm9ds1.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,19 @@ def __init__(
9595
self.address_magnet = address_magnet
9696

9797
# Sanity checks
98-
if not gyro_odr in _ODR_IMU:
98+
if gyro_odr not in _ODR_IMU:
9999
raise ValueError("Invalid gyro sampling rate: %d" % gyro_odr)
100-
if not gyro_scale in _GYRO_SCALE:
100+
if gyro_scale not in _GYRO_SCALE:
101101
raise ValueError("Invalid gyro scaling: %d" % gyro_scale)
102102

103-
if not accel_odr in _ODR_IMU:
103+
if accel_odr not in _ODR_IMU:
104104
raise ValueError("Invalid accelerometer sampling rate: %d" % accel_odr)
105-
if not accel_scale in _ACCEL_SCALE:
105+
if accel_scale not in _ACCEL_SCALE:
106106
raise ValueError("Invalid accelerometer scaling: %d" % accel_scale)
107107

108-
if not magnet_odr in _ODR_MAGNET:
108+
if magnet_odr not in _ODR_MAGNET:
109109
raise ValueError("Invalid magnet sampling rate: %d" % magnet_odr)
110-
if not magnet_scale in _MAGNET_SCALE:
110+
if magnet_scale not in _MAGNET_SCALE:
111111
raise ValueError("Invalid magnet scaling: %d" % magnet_scale)
112112

113113
if (self.magent_id() != b"=") or (self.gyro_id() != b"h"):

micropython/mip/mip/__init__.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,8 @@ def _install_json(package_json_url, index, target, version, mpy):
123123

124124

125125
def _install_package(package, index, target, version, mpy):
126-
if (
127-
package.startswith("http://")
128-
or package.startswith("https://")
129-
or package.startswith("github:")
130-
):
131-
if package.endswith(".py") or package.endswith(".mpy"):
126+
if package.startswith(("http://", "https://", "github:")):
127+
if package.endswith((".py", ".mpy")):
132128
print("Downloading {} to {}".format(package, target))
133129
return _download_file(
134130
_rewrite_url(package, version), target + "/" + package.rsplit("/")[-1]

micropython/senml/examples/custom_record.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def __init__(self, name, **kwargs):
4343

4444
def _check_value_type(self, value):
4545
"""overriding the check on value type to make certain that only an array with 3 values is assigned: lat,lon/alt"""
46-
if not value == None:
46+
if value is not None:
4747
if not isinstance(value, list):
4848
raise Exception("invalid data type: array with 3 elements expected lat, lon, alt")
4949

micropython/senml/examples/gateway_actuators.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def gateway_callback(record, **kwargs):
5656
:param kwargs: optional extra parameters (device can be found here)
5757
:return: None
5858
"""
59-
if "device" in kwargs and kwargs["device"] != None:
59+
if "device" in kwargs and kwargs["device"] is not None:
6060
print("for device: " + kwargs["device"].name)
6161
else:
6262
print("for gateway: ")

micropython/senml/senml/senml_pack.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def __next__(self):
4747
self._index += 1
4848
return res
4949
else:
50-
raise StopIteration()
50+
raise StopIteration
5151

5252

5353
class SenmlPack(SenmlBase):
@@ -156,7 +156,7 @@ def _check_value_type(self, value, field_name):
156156
checks if the type of value is allowed for senml
157157
:return: None, raisee exception if not ok.
158158
"""
159-
if not value == None:
159+
if value is not None:
160160
if not (isinstance(value, int) or isinstance(value, float)):
161161
raise Exception("invalid type for " + field_name + ", only numbers allowed")
162162

@@ -330,7 +330,7 @@ def add(self, item):
330330
"""
331331
if not (isinstance(item, SenmlBase)):
332332
raise Exception("invalid type of param, SenmlRecord or SenmlPack expected")
333-
if not item._parent == None:
333+
if item._parent is not None:
334334
raise Exception("item is already part of a pack")
335335

336336
self._data.append(item)

micropython/senml/senml/senml_record.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def _check_value_type(self, value):
7979
checks if the type of value is allowed for senml
8080
:return: None, raisee exception if not ok.
8181
"""
82-
if not value == None:
82+
if value is not None:
8383
if not (
8484
isinstance(value, bool)
8585
or isinstance(value, int)
@@ -96,7 +96,7 @@ def _check_number_type(self, value, field_name):
9696
checks if the type of value is allowed for senml
9797
:return: None, raisee exception if not ok.
9898
"""
99-
if not value == None:
99+
if value is not None:
100100
if not (isinstance(value, int) or isinstance(value, float)):
101101
raise Exception("invalid type for " + field_name + ", only numbers allowed")
102102

pyproject.toml

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
[tool.ruff]
2+
exclude = [
3+
"python-stdlib",
4+
"unix-ffi",
5+
]
6+
select = [
7+
"C4", # flake8-comprehensions
8+
"C90", # McCabe cyclomatic complexity
9+
"DTZ", # flake8-datetimez
10+
"E", # pycodestyle
11+
"EXE", # flake8-executable
12+
"F", # Pyflakes
13+
"G", # flake8-logging-format
14+
"ICN", # flake8-import-conventions
15+
"INT", # flake8-gettext
16+
"ISC", # flake8-implicit-str-concat
17+
"PGH", # pygrep-hooks
18+
"PIE", # flake8-pie
19+
"PL", # Pylint
20+
"PYI", # flake8-pyi
21+
"RSE", # flake8-raise
22+
"RUF", # Ruff-specific rules
23+
"T10", # flake8-debugger
24+
"TCH", # flake8-type-checking
25+
"W", # pycodestyle
26+
"YTT", # flake8-2020
27+
# "A", # flake8-builtins
28+
# "ANN", # flake8-annotations
29+
# "ARG", # flake8-unused-arguments
30+
# "B", # flake8-bugbear
31+
# "BLE", # flake8-blind-except
32+
# "COM", # flake8-commas
33+
# "D", # pydocstyle
34+
# "DJ", # flake8-django
35+
# "EM", # flake8-errmsg
36+
# "ERA", # eradicate
37+
# "FBT", # flake8-boolean-trap
38+
# "I", # isort
39+
# "INP", # flake8-no-pep420
40+
# "N", # pep8-naming
41+
# "NPY", # NumPy-specific rules
42+
# "PD", # pandas-vet
43+
# "PT", # flake8-pytest-style
44+
# "PTH", # flake8-use-pathlib
45+
# "Q", # flake8-quotes
46+
# "RET", # flake8-return
47+
# "S", # flake8-bandit
48+
# "SIM", # flake8-simplify
49+
# "SLF", # flake8-self
50+
# "T20", # flake8-print
51+
# "TID", # flake8-tidy-imports
52+
# "TRY", # tryceratops
53+
# "UP", # pyupgrade
54+
]
55+
ignore = [
56+
"E401",
57+
"E402",
58+
"E722",
59+
"E741",
60+
"F401",
61+
"F403",
62+
"F405",
63+
"F541",
64+
"F821",
65+
"F841",
66+
"PLC1901",
67+
"PLR1701",
68+
"PLR5501",
69+
"PLW0602",
70+
"PLW0603",
71+
"PLW2901",
72+
]
73+
line-length = 260
74+
target-version = "py37"
75+
76+
[tool.ruff.mccabe]
77+
max-complexity = 61
78+
79+
[tool.ruff.pylint]
80+
allow-magic-value-types = ["bytes", "int", "str"]
81+
max-args = 14
82+
max-branches = 58
83+
max-returns = 13
84+
max-statements = 166
85+
86+
[tool.ruff.per-file-ignores]
87+
"micropython/aiorepl/aiorepl.py" = ["PGH001"]

python-ecosys/cbor2/cbor2/encoder.py

-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ def encode_semantic(encoder, tag, value):
5353

5454
def encode_float(encoder, value):
5555
# Handle special values efficiently
56-
import math
57-
5856
if math.isnan(value):
5957
encoder.write(b"\xf9\x7e\x00")
6058
elif math.isinf(value):

python-ecosys/iperf3/iperf3.py

-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ def report_receiver(self, stats):
156156
st["errors"],
157157
" receiver",
158158
)
159-
return
160159

161160

162161
def recvn(s, n):

python-ecosys/urequests/urequests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def request(
9292
if proto == "https:":
9393
s = ussl.wrap_socket(s, server_hostname=host)
9494
s.write(b"%s /%s HTTP/1.0\r\n" % (method, path))
95-
if not "Host" in headers:
95+
if "Host" not in headers:
9696
s.write(b"Host: %s\r\n" % host)
9797
# Iterate over keys to avoid tuple alloc
9898
for k in headers:

0 commit comments

Comments
 (0)