Skip to content

Commit 883bbe9

Browse files
committed
Made tests pass, added new pylint check for tests
1 parent 6587a0b commit 883bbe9

4 files changed

+46
-27
lines changed

.pre-commit-config.yaml

Lines changed: 9 additions & 1 deletion
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

tests/displayio_shared_bindings.py

Lines changed: 14 additions & 10 deletions
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

Lines changed: 20 additions & 13 deletions
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

Lines changed: 3 additions & 3 deletions
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)