Skip to content

Commit d61c5ed

Browse files
committed
remove type: ignore and minor refactor for dir/hasattr, swap WriteableBuffer #18
1 parent 4fe2546 commit d61c5ed

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

adafruit_bitmapsaver.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,23 @@
3535

3636
try:
3737
from typing import Tuple, Optional, Union
38-
from io import BufferedWriter
38+
from io import WriteableBuffer
3939
except ImportError:
4040
pass
4141

4242
__version__ = "0.0.0-auto.0"
4343
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BitmapSaver.git"
4444

4545

46-
def _write_bmp_header(output_file: BufferedWriter, filesize: int) -> None:
46+
def _write_bmp_header(output_file: WriteableBuffer, filesize: int) -> None:
4747
output_file.write(bytes("BM", "ascii"))
4848
output_file.write(struct.pack("<I", filesize))
4949
output_file.write(b"\00\x00")
5050
output_file.write(b"\00\x00")
5151
output_file.write(struct.pack("<I", 54))
5252

5353

54-
def _write_dib_header(output_file: BufferedWriter, width: int, height: int) -> None:
54+
def _write_dib_header(output_file: WriteableBuffer, width: int, height: int) -> None:
5555
output_file.write(struct.pack("<I", 40))
5656
output_file.write(struct.pack("<I", width))
5757
output_file.write(struct.pack("<I", height))
@@ -83,7 +83,7 @@ def _rgb565_to_bgr_tuple(color: int) -> Tuple[int, int, int]:
8383

8484
# pylint:disable=too-many-locals
8585
def _write_pixels(
86-
output_file: BufferedWriter,
86+
output_file: WriteableBuffer,
8787
pixel_source: Union[Bitmap, Display],
8888
palette: Optional[Palette],
8989
) -> None:
@@ -95,16 +95,16 @@ def _write_pixels(
9595
if saving_bitmap:
9696
# pixel_source: Bitmap
9797
for x in range(width):
98-
pixel = pixel_source[x, y - 1] # type: ignore
99-
color = palette[pixel] # type: ignore # handled by save_pixel's guardians
98+
pixel = pixel_source[x, y - 1]
99+
color = palette[pixel] # handled by save_pixel's guardians
100100
for _ in range(3):
101101
row_buffer[buffer_index] = color & 0xFF
102102
color >>= 8
103103
buffer_index += 1
104104
else:
105105
# pixel_source: Display
106106
result_buffer = bytearray(2048)
107-
data = pixel_source.fill_row(y - 1, result_buffer) # type: ignore
107+
data = pixel_source.fill_row(y - 1, result_buffer)
108108
for i in range(width):
109109
pixel565 = (data[i * 2] << 8) + data[i * 2 + 1]
110110
for b in _rgb565_to_bgr_tuple(pixel565):
@@ -120,7 +120,7 @@ def _write_pixels(
120120

121121

122122
def save_pixels(
123-
file_or_filename: Union[str, BufferedWriter],
123+
file_or_filename: Union[str, WriteableBuffer],
124124
pixel_source: Union[Display, Bitmap] = None,
125125
palette: Palette = None,
126126
) -> None:
@@ -133,7 +133,7 @@ def save_pixels(
133133
:param palette: the Palette to use for looking up colors in the bitmap
134134
"""
135135
if not pixel_source:
136-
if "DISPLAY" not in dir(board):
136+
if not hasattr(board, "DISPLAY"):
137137
raise ValueError("Second argument must be a Bitmap or Display")
138138
pixel_source = board.DISPLAY
139139

0 commit comments

Comments
 (0)