Skip to content

Commit 57f2865

Browse files
authored
Merge pull request #18 from adafruit/patch
Pylint and RTD update patch, and other fixes
2 parents 4e65cee + 437e5b0 commit 57f2865

File tree

5 files changed

+78
-67
lines changed

5 files changed

+78
-67
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ jobs:
4242
# (e.g. - apt-get: gettext, etc; pip: circuitpython-build-tools, requirements.txt; etc.)
4343
run: |
4444
source actions-ci/install.sh
45-
- name: Pip install pylint, Sphinx, pre-commit
45+
- name: Pip install Sphinx, pre-commit
4646
run: |
47-
pip install --force-reinstall pylint Sphinx sphinx-rtd-theme pre-commit
47+
pip install --force-reinstall Sphinx sphinx-rtd-theme pre-commit
4848
- name: Library version
4949
run: git describe --dirty --always --tags
5050
- name: Pre-commit hooks

.pre-commit-config.yaml

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,25 @@ repos:
1818
- id: end-of-file-fixer
1919
- id: trailing-whitespace
2020
- repo: https://github.com/pycqa/pylint
21-
rev: pylint-2.7.1
21+
rev: v2.11.1
2222
hooks:
2323
- id: pylint
2424
name: pylint (library code)
2525
types: [python]
26-
exclude: "^(docs/|tests/|examples/|setup.py$)"
27-
- repo: local
28-
hooks:
29-
- id: pylint_examples
30-
name: pylint (examples code)
26+
args:
27+
- --disable=consider-using-f-string
28+
exclude: "^(docs/|examples/|tests/|setup.py$)"
29+
- id: pylint
30+
name: pylint (example code)
3131
description: Run pylint rules on "examples/*.py" files
32-
entry: /usr/bin/env bash -c
33-
args: ['([[ ! -d "examples" ]] || for example in $(find . -path "./examples/*.py"); do pylint --disable=missing-docstring,invalid-name,consider-using-f-string $example; done)']
34-
language: system
35-
- repo: local
36-
hooks:
37-
- id: pylint_tests
38-
name: pylint (tests code)
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)
3938
description: Run pylint rules on "tests/*.py" files
40-
entry: /usr/bin/env bash -c
41-
args: ['([[ ! -d "tests" ]] || for test in $(find . -path "./tests/*.py"); do pylint --disable=missing-docstring,consider-using-f-string $test; done)']
42-
language: system
39+
types: [python]
40+
files: "^tests/"
41+
args:
42+
- --disable=missing-docstring,consider-using-f-string,duplicate-code

.pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ ignore-docstrings=yes
252252
ignore-imports=yes
253253

254254
# Minimum lines number of a similarity.
255-
min-similarity-lines=12
255+
min-similarity-lines=4
256256

257257

258258
[BASIC]

docs/requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
sphinx>=4.0.0

tests/test_adafruit_radio.py

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -46,111 +46,115 @@ def test_radio_init_channel():
4646
assert r._channel == 7 # pylint: disable=protected-access
4747

4848

49-
def test_radio_configure_channel(radio):
49+
def test_radio_configure_channel(radio_obj):
5050
"""
5151
If a valid channel argument is passed to the configure method, the Radio
5252
instance's channel is updated to reflect this.
5353
"""
54-
assert radio._channel == 42 # pylint: disable=protected-access
55-
radio.configure(channel=7)
56-
assert radio._channel == 7 # pylint: disable=protected-access
54+
assert radio_obj._channel == 42 # pylint: disable=protected-access
55+
radio_obj.configure(channel=7)
56+
assert radio_obj._channel == 7 # pylint: disable=protected-access
5757

5858

59-
def test_radio_configure_channel_out_of_bounds(radio):
59+
def test_radio_configure_channel_out_of_bounds(
60+
radio_obj,
61+
): # pylint: disable=invalid-name
6062
"""
6163
If a channel not in the range 0-255 is passed into the configure method,
6264
then a ValueError exception is raised.
6365
"""
6466
with pytest.raises(ValueError):
65-
radio.configure(channel=-1)
67+
radio_obj.configure(channel=-1)
6668
with pytest.raises(ValueError):
67-
radio.configure(channel=256)
69+
radio_obj.configure(channel=256)
6870
# Add just-in-bounds checks too.
69-
radio.configure(channel=0)
70-
assert radio._channel == 0 # pylint: disable=protected-access
71-
radio.configure(channel=255)
72-
assert radio._channel == 255 # pylint: disable=protected-access
71+
radio_obj.configure(channel=0)
72+
assert radio_obj._channel == 0 # pylint: disable=protected-access
73+
radio_obj.configure(channel=255)
74+
assert radio_obj._channel == 255 # pylint: disable=protected-access
7375

7476

75-
def test_radio_send(radio):
77+
def test_radio_send(radio_obj):
7678
"""
7779
The send method merely encodes to bytes and calls send_bytes.
7880
"""
79-
radio.send_bytes = mock.MagicMock()
81+
radio_obj.send_bytes = mock.MagicMock()
8082
msg = "Testing 1, 2, 3..."
81-
radio.send(msg)
82-
radio.send_bytes.assert_called_once_with(msg.encode("utf-8"))
83+
radio_obj.send(msg)
84+
radio_obj.send_bytes.assert_called_once_with(msg.encode("utf-8"))
8385

8486

85-
def test_radio_send_bytes_too_long(radio):
87+
def test_radio_send_bytes_too_long(radio_obj):
8688
"""
8789
A ValueError is raised if the message to be sent is too long (defined by
8890
MAX_LENGTH).
8991
"""
9092
msg = bytes(adafruit_ble_radio.MAX_LENGTH + 1)
9193
with pytest.raises(ValueError):
92-
radio.send_bytes(msg)
94+
radio_obj.send_bytes(msg)
9395

9496

95-
def test_radio_send_bytes(radio):
97+
def test_radio_send_bytes(radio_obj):
9698
"""
9799
Ensure the expected message is set on an instance of AdafruitRadio, and
98100
broadcast for AD_DURATION period of time.
99101
"""
100-
radio.uid = 255 # set up for cycle back to 0.
102+
radio_obj.uid = 255 # set up for cycle back to 0.
101103
msg = b"Hello"
102104
with mock.patch("adafruit_ble_radio.time.sleep") as mock_sleep:
103-
radio.send_bytes(msg)
105+
radio_obj.send_bytes(msg)
104106
mock_sleep.assert_called_once_with(adafruit_ble_radio.AD_DURATION)
105107
spy_advertisement = (
106-
adafruit_ble_radio._RadioAdvertisement()
108+
adafruit_ble_radio._RadioAdvertisement() # pylint: disable=protected-access
107109
) # pylint: disable=protected-access
108-
chan = struct.pack("<B", radio._channel) # pylint: disable=protected-access
110+
chan = struct.pack("<B", radio_obj._channel) # pylint: disable=protected-access
109111
uid = struct.pack("<B", 255)
110112
assert spy_advertisement.msg == chan + uid + msg
111-
radio.ble.start_advertising.assert_called_once_with(spy_advertisement)
112-
radio.ble.stop_advertising.assert_called_once_with()
113-
assert radio.uid == 0
113+
radio_obj.ble.start_advertising.assert_called_once_with(spy_advertisement)
114+
radio_obj.ble.stop_advertising.assert_called_once_with()
115+
assert radio_obj.uid == 0
114116

115117

116-
def test_radio_receive_no_message(radio):
118+
def test_radio_receive_no_message(radio_obj):
117119
"""
118120
If no message is received from the receive_bytes method, then None is
119121
returned.
120122
"""
121-
radio.receive_full = mock.MagicMock(return_value=None)
122-
assert radio.receive() is None
123-
radio.receive_full.assert_called_once_with()
123+
radio_obj.receive_full = mock.MagicMock(return_value=None)
124+
assert radio_obj.receive() is None
125+
radio_obj.receive_full.assert_called_once_with()
124126

125127

126-
def test_radio_receive(radio):
128+
def test_radio_receive(radio_obj):
127129
"""
128130
If bytes are received from the receive_bytes method, these are decoded
129131
using utf-8 and returned as a string with null characters stripped from the
130132
end.
131133
"""
132134
# Return value contains message bytes, RSSI (signal strength), timestamp.
133135
msg = b"testing 1, 2, 3\x00\x00\x00\x00\x00\x00"
134-
radio.receive_full = mock.MagicMock(return_value=(msg, -20, 1.2))
135-
assert radio.receive() == "testing 1, 2, 3"
136+
radio_obj.receive_full = mock.MagicMock(return_value=(msg, -20, 1.2))
137+
assert radio_obj.receive() == "testing 1, 2, 3"
136138

137139

138-
def test_radio_receive_full_no_messages(radio):
140+
def test_radio_receive_full_no_messages(radio_obj): # pylint: disable=invalid-name
139141
"""
140142
If no messages are detected by receive_full then it returns None.
141143
"""
142-
radio.ble.start_scan.return_value = []
143-
assert radio.receive_full() is None
144-
radio.ble.start_scan.assert_called_once_with(
145-
adafruit_ble_radio._RadioAdvertisement,
144+
radio_obj.ble.start_scan.return_value = []
145+
assert radio_obj.receive_full() is None
146+
radio_obj.ble.start_scan.assert_called_once_with(
147+
adafruit_ble_radio._RadioAdvertisement, # pylint: disable=protected-access
146148
minimum_rssi=-255,
147149
timeout=1,
148150
extended=True,
149151
)
150-
radio.ble.stop_scan.assert_called_once_with()
152+
radio_obj.ble.stop_scan.assert_called_once_with()
151153

152154

153-
def test_radio_receive_full_duplicate_message(radio):
155+
def test_radio_receive_full_duplicate_message(
156+
radio_obj,
157+
): # pylint: disable=invalid-name
154158
"""
155159
If a duplicate message is detected, then receive_full returns None
156160
(indicating no *new* messages received).
@@ -159,12 +163,14 @@ def test_radio_receive_full_duplicate_message(radio):
159163
mock_entry.msg = b"*\x00Hello"
160164
mock_entry.address.address_bytes = b"addr"
161165
mock_entry.rssi = -40
162-
radio.ble.start_scan.return_value = [mock_entry]
163-
radio.msg_pool.add((time.monotonic(), 42, 0, b"addr"))
164-
assert radio.receive_full() is None
166+
radio_obj.ble.start_scan.return_value = [mock_entry]
167+
radio_obj.msg_pool.add((time.monotonic(), 42, 0, b"addr"))
168+
assert radio_obj.receive_full() is None
165169

166170

167-
def test_radio_receive_full_and_remove_expired_message_metadata(radio):
171+
def test_radio_receive_full_and_remove_expired_message_metadata(
172+
radio_obj,
173+
): # pylint: disable=invalid-name
168174
"""
169175
Return the non-duplicate message.
170176
@@ -177,15 +183,15 @@ def test_radio_receive_full_and_remove_expired_message_metadata(radio):
177183
mock_entry.msg = b"*\x01Hello"
178184
mock_entry.address.address_bytes = b"adr2"
179185
mock_entry.rssi = -40
180-
radio.ble.start_scan.return_value = [mock_entry]
181-
radio.msg_pool.add(
186+
radio_obj.ble.start_scan.return_value = [mock_entry]
187+
radio_obj.msg_pool.add(
182188
(time.monotonic() - adafruit_ble_radio.AD_DURATION - 1, 42, 0, b"addr")
183189
)
184-
result = radio.receive_full()
190+
result = radio_obj.receive_full()
185191
assert result[0] == b"Hello"
186192
assert result[1] == -40
187-
assert len(radio.msg_pool) == 1
188-
metadata = radio.msg_pool.pop()
193+
assert len(radio_obj.msg_pool) == 1
194+
metadata = radio_obj.msg_pool.pop()
189195
assert metadata[1] == 42
190196
assert metadata[2] == 1
191197
assert metadata[3] == b"adr2"

0 commit comments

Comments
 (0)