Skip to content

Commit 28ba6d3

Browse files
authored
Merge pull request #46 from adafruit/tests-linting
Linted tests, fixed duplicate-code failures
2 parents 6587a0b + 2cb93b7 commit 28ba6d3

File tree

7 files changed

+60
-41
lines changed

7 files changed

+60
-41
lines changed

.pre-commit-config.yaml

+9-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ repos:
2323
- id: pylint
2424
name: pylint (library code)
2525
types: [python]
26-
exclude: "^(docs/|examples/|setup.py$)"
26+
exclude: "^(docs/|tests/|examples/|setup.py$)"
2727
- repo: local
2828
hooks:
2929
- id: pylint_examples
@@ -32,3 +32,11 @@ repos:
3232
entry: /usr/bin/env bash -c
3333
args: ['([[ ! -d "examples" ]] || for example in $(find . -path "./examples/*.py"); do pylint --disable=missing-docstring,invalid-name $example; done)']
3434
language: system
35+
- repo: local
36+
hooks:
37+
- id: pylint_tests
38+
name: pylint (tests code)
39+
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 $test; done)']
42+
language: system

adafruit_imageload/bmp/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ def load(file, *, bitmap=None, palette=None):
3232
# bmp_header_length = int.from_bytes(file.read(4), 'little')
3333
# print(bmp_header_length)
3434
file.seek(0x12) # Width of the bitmap in pixels
35-
width = int.from_bytes(file.read(4), "little")
35+
_width = int.from_bytes(file.read(4), "little")
3636
try:
37-
height = int.from_bytes(file.read(4), "little")
37+
_height = int.from_bytes(file.read(4), "little")
3838
except OverflowError as error:
3939
raise NotImplementedError(
4040
"Negative height BMP files are not supported on builds without longint"
@@ -58,8 +58,8 @@ def load(file, *, bitmap=None, palette=None):
5858

5959
return indexed.load(
6060
file,
61-
width,
62-
height,
61+
_width,
62+
_height,
6363
data_start,
6464
colors,
6565
color_depth,

adafruit_imageload/pnm/pgm/ascii.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def load(file, width, height, bitmap=None, palette=None):
2121
Load a PGM ascii file (P2)
2222
"""
2323
data_start = file.tell() # keep this so we can rewind
24-
palette_colors = set()
24+
_palette_colors = set()
2525
pixel = bytearray()
2626
# build a set of all colors present in the file, so palette and bitmap can be constructed
2727
while True:
@@ -30,14 +30,14 @@ def load(file, width, height, bitmap=None, palette=None):
3030
break
3131
if not byte.isdigit():
3232
int_pixel = int("".join(["%c" % char for char in pixel]))
33-
palette_colors.add(int_pixel)
33+
_palette_colors.add(int_pixel)
3434
pixel = bytearray()
3535
pixel += byte
3636
if palette:
37-
palette = build_palette(palette, palette_colors)
37+
palette = build_palette(palette, _palette_colors)
3838
if bitmap:
39-
bitmap = bitmap(width, height, len(palette_colors))
40-
palette_colors = list(palette_colors)
39+
bitmap = bitmap(width, height, len(_palette_colors))
40+
_palette_colors = list(_palette_colors)
4141
file.seek(data_start)
4242
for y in range(height):
4343
for x in range(width):
@@ -48,11 +48,11 @@ def load(file, width, height, bitmap=None, palette=None):
4848
break
4949
pixel += byte
5050
int_pixel = int("".join(["%c" % char for char in pixel]))
51-
bitmap[x, y] = palette_colors.index(int_pixel)
51+
bitmap[x, y] = _palette_colors.index(int_pixel)
5252
return bitmap, palette
5353

5454

55-
def build_palette(palette_class, palette_colors):
55+
def build_palette(palette_class, palette_colors): # pylint: disable=duplicate-code
5656
"""
5757
construct the Palette, and populate it with the set of palette_colors
5858
"""

adafruit_imageload/pnm/pgm/binary.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def build_palette(palette_class, palette_colors):
4444
"""
4545
construct the Palette, and populate it with the set of palette_colors
4646
"""
47-
palette = palette_class(len(palette_colors))
47+
_palette = palette_class(len(palette_colors))
4848
for counter, color in enumerate(palette_colors):
49-
palette[counter] = bytes([color, color, color])
50-
return palette
49+
_palette[counter] = bytes([color, color, color])
50+
return _palette

tests/displayio_shared_bindings.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
====================================================
2525
2626
The classes in this file are designed to emulate Circuitpython's displayio classes
27-
for Bitmap and Palette. These mimic classes should have the same methods and interface as the real interface,
28-
but with extra validation checks, warnings, and messages to facilitate debugging.
27+
for Bitmap and Palette. These mimic classes should have the same methods and interface as the real
28+
interface, but with extra validation checks, warnings, and messages to facilitate debugging.
2929
3030
Code that can be run successfully against these classes will have a good chance of
3131
working correctly on hardware running Circuitpython, but without needing to upload code to a board
@@ -37,7 +37,7 @@
3737
from typing import Union
3838

3939

40-
class Bitmap_C_Interface(object):
40+
class Bitmap_C_Interface:
4141
"""
4242
A class to simulate the displayio.Bitmap class for testing, based on
4343
https://circuitpython.readthedocs.io/en/latest/shared-bindings/displayio/Bitmap.html
@@ -88,8 +88,10 @@ def __getitem__(self, item: Union[tuple, int]) -> bytearray:
8888
raise RuntimeError(f"get position out of range {item}")
8989
try:
9090
return self.data[item]
91-
except KeyError:
92-
raise RuntimeError("no data at {} [{}]".format(self._decode(item), item))
91+
except KeyError as err:
92+
raise RuntimeError(
93+
"no data at {} [{}]".format(self._decode(item), item)
94+
) from err
9395

9496
def validate(self, detect_empty_image=True) -> None:
9597
"""
@@ -103,8 +105,8 @@ def validate(self, detect_empty_image=True) -> None:
103105
for x in range(self.width):
104106
try:
105107
seen_colors.add(self[x, y])
106-
except KeyError:
107-
raise ValueError(f"missing data at {x},{y}")
108+
except KeyError as err:
109+
raise ValueError(f"missing data at {x},{y}") from err
108110
if detect_empty_image and len(seen_colors) < 2:
109111
raise ValueError(
110112
"image detected as only one color. set detect_empty_image=False to ignore"
@@ -131,7 +133,7 @@ def __str__(self) -> str:
131133
return out
132134

133135

134-
class Palette_C_Interface(object):
136+
class Palette_C_Interface:
135137
"""
136138
A class to simulates the displayio.Palette class for testing, based on
137139
https://circuitpython.readthedocs.io/en/latest/shared-bindings/displayio/Palette.html
@@ -191,8 +193,10 @@ def validate(self):
191193
for i in range(self.num_colors):
192194
try:
193195
self.colors
194-
except IndexError:
195-
raise ValueError("missing color `{}` in palette color list".format(i))
196+
except IndexError as err:
197+
raise ValueError(
198+
"missing color `{}` in palette color list".format(i)
199+
) from err
196200

197201
def __str__(self):
198202
"""

tests/test_palette_c_interface.py

+20-13
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,16 @@
3333

3434

3535
class TestPalette_C_Interface(TestCase):
36-
def test_init_mono(self):
36+
@staticmethod
37+
def test_init_mono():
3738
Palette_C_Interface(1)
3839

39-
def test_init_color(self):
40+
@staticmethod
41+
def test_init_color():
4042
Palette_C_Interface(256)
4143

42-
def test_set_int(self):
44+
@staticmethod
45+
def test_set_int():
4346
palette = Palette_C_Interface(1)
4447
palette[0] = 0xFFFFFF
4548

@@ -48,7 +51,8 @@ def test_get_int(self):
4851
palette[0] = 0xFFFFFF
4952
self.assertEqual(0xFFFFFF, palette[0])
5053

51-
def test_set_byte(self):
54+
@staticmethod
55+
def test_set_byte():
5256
palette = Palette_C_Interface(1)
5357
palette[0] = b"\xFF\xFF\xFF"
5458

@@ -57,7 +61,8 @@ def test_get_byte(self):
5761
palette[0] = b"\xFF\xFF\xFF"
5862
self.assertEqual(b"\xFF\xFF\xFF", palette[0])
5963

60-
def test_set_bytearray(self):
64+
@staticmethod
65+
def test_set_bytearray():
6166
palette = Palette_C_Interface(1)
6267
palette[0] = bytearray(b"\xFF\xFF\xFF")
6368

@@ -66,20 +71,21 @@ def test_prevents_out_of_range(self):
6671
try:
6772
palette[1] = 0xFFFFFF
6873
self.fail("exception should have already thrown")
69-
except ValueError as e:
70-
if "greater than allowed" not in str(e):
74+
except ValueError as err:
75+
if "greater than allowed" not in str(err):
7176
raise
7277

7378
def test_prevents_set_non_allowed(self):
7479
palette = Palette_C_Interface(1)
7580
try:
7681
palette[0] = "\xFF\xFF\xFF" # attempt with a string, which is not allowed
7782
self.fail("exception should have thrown")
78-
except ValueError as e:
79-
if "should be" not in str(e):
83+
except ValueError as err:
84+
if "should be" not in str(err):
8085
raise
8186

82-
def test_validate_success(self):
87+
@staticmethod
88+
def test_validate_success():
8389
palette = Palette_C_Interface(1)
8490
palette[0] = b"\xFF\xFF\xFF"
8591
palette.validate()
@@ -90,11 +96,12 @@ def test_validate_fails(self):
9096
try:
9197
palette.validate()
9298
self.fail("exception should have thrown")
93-
except IndexError as e:
94-
if "palette was initialized" not in str(e):
99+
except IndexError as err:
100+
if "palette was initialized" not in str(err):
95101
raise
96102

97-
def test_str(self):
103+
@staticmethod
104+
def test_str():
98105
palette = Palette_C_Interface(1)
99106
palette[0] = b"\xFF\xFF\xFF"
100107
print(str(palette))

tests/test_pbm_load.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@
3535

3636

3737
class TestPbmLoad(TestCase):
38-
def test_load_fails_with_no_header_data(self):
38+
def test_load_fails_with_no_header_data(self): # pylint: disable=invalid-name
3939
file = BytesIO(b"some initial binary data: \x00\x01")
4040
try:
4141
pnm.load(
4242
file, b"P1", bitmap=Bitmap_C_Interface, palette=Palette_C_Interface
4343
)
4444
self.fail("should have failed")
45-
except Exception as caught_exception:
45+
except Exception as caught_exception: # pylint: disable=broad-except
4646
if "Unsupported image format" not in str(caught_exception):
4747
raise
4848

@@ -102,7 +102,7 @@ def test_load_works_p4_binary(self):
102102
self.assertEqual(15, bitmap.height)
103103
bitmap.validate()
104104

105-
def test_load_works_p4_binary_high_res(self):
105+
def test_load_works_p4_binary_high_res(self): # pylint: disable=invalid-name
106106
test_file = os.path.join(
107107
os.path.dirname(__file__),
108108
"..",

0 commit comments

Comments
 (0)