Skip to content

Commit 154bcae

Browse files
authored
Merge pull request #60 from matt-land/add-typing
Add types for #56 Missing type annotations
2 parents 7c18538 + aaf9590 commit 154bcae

18 files changed

+375
-102
lines changed

.pylintrc

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

254254
# Minimum lines number of a similarity.
255-
min-similarity-lines=4
256-
255+
min-similarity-lines=14
257256

258257
[BASIC]
259258

adafruit_imageload/__init__.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# SPDX-FileCopyrightText: 2018 Scott Shawcroft for Adafruit Industries
2+
# SPDX-FileCopyrightText: 2022 Matt Land
23
#
34
# SPDX-License-Identifier: MIT
45

@@ -8,22 +9,42 @@
89
910
Load pixel values (indices or colors) into a bitmap and colors into a palette.
1011
11-
* Author(s): Scott Shawcroft
12+
* Author(s): Scott Shawcroft, Matt Land
1213
1314
"""
1415
# pylint: disable=import-outside-toplevel
1516

17+
try:
18+
from typing import (
19+
Tuple,
20+
Iterator,
21+
Optional,
22+
List,
23+
Iterable,
24+
Union,
25+
)
26+
from io import BufferedReader
27+
from displayio import Palette, Bitmap
28+
from .displayio_types import PaletteConstructor, BitmapConstructor
29+
except ImportError:
30+
pass
31+
1632
__version__ = "0.0.0-auto.0"
1733
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
1834

1935

20-
def load(file_or_filename, *, bitmap=None, palette=None):
36+
def load(
37+
file_or_filename: Union[str, BufferedReader],
38+
*,
39+
bitmap: Optional[BitmapConstructor] = None,
40+
palette: Optional[PaletteConstructor] = None
41+
) -> Tuple[Bitmap, Optional[Palette]]:
2142
"""Load pixel values (indices or colors) into a bitmap and colors into a palette.
2243
2344
bitmap is the desired type. It must take width, height and color_depth in the constructor. It
2445
must also have a _load_row method to load a row's worth of pixel data.
2546
26-
palette is the desired pallete type. The constructor should take the number of colors and
47+
palette is the desired palette type. The constructor should take the number of colors and
2748
support assignment to indices via [].
2849
"""
2950
if not bitmap or not palette:

adafruit_imageload/bmp/__init__.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# SPDX-FileCopyrightText: 2018 Scott Shawcroft for Adafruit Industries
2+
# SPDX-FileCopyrightText: 2022 Matt Land
23
#
34
# SPDX-License-Identifier: MIT
45

@@ -8,16 +9,29 @@
89
910
Load pixel values (indices or colors) into a bitmap and colors into a palette from a BMP file.
1011
11-
* Author(s): Scott Shawcroft
12+
* Author(s): Scott Shawcroft, Matt Land
1213
1314
"""
1415
# pylint: disable=import-outside-toplevel
1516

17+
try:
18+
from typing import Tuple, Optional, Set, List
19+
from io import BufferedReader
20+
from displayio import Palette, Bitmap
21+
from ..displayio_types import PaletteConstructor, BitmapConstructor
22+
except ImportError:
23+
pass
24+
1625
__version__ = "0.0.0-auto.0"
1726
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
1827

1928

20-
def load(file, *, bitmap=None, palette=None):
29+
def load(
30+
file: BufferedReader,
31+
*,
32+
bitmap: BitmapConstructor = None,
33+
palette: PaletteConstructor = None
34+
) -> Tuple[Bitmap, Optional[Palette]]:
2135
"""Loads a bmp image from the open ``file``.
2236
2337
Returns tuple of bitmap object and palette object.

adafruit_imageload/bmp/indexed.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# SPDX-FileCopyrightText: 2018 Scott Shawcroft for Adafruit Industries
2+
# SPDX-FileCopyrightText: 2022 Matt Land
23
#
34
# SPDX-License-Identifier: MIT
45

@@ -8,33 +9,43 @@
89
910
Load pixel values (indices or colors) into a bitmap and colors into a palette from an indexed BMP.
1011
11-
* Author(s): Scott Shawcroft
12+
* Author(s): Scott Shawcroft, Matt Land
1213
1314
"""
1415

15-
__version__ = "0.0.0-auto.0"
16-
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
1716

1817
import sys
1918

19+
try:
20+
from typing import Tuple, Optional
21+
from io import BufferedReader
22+
from displayio import Palette, Bitmap
23+
from ..displayio_types import PaletteConstructor, BitmapConstructor
24+
except ImportError:
25+
pass
26+
2027
try:
2128
from bitmaptools import readinto as _bitmap_readinto
2229
except ImportError:
23-
_bitmap_readinto = None # pylint: disable=invalid-name
30+
_bitmap_readinto = None # pylint: disable=invalid-name # type: Callable
31+
32+
33+
__version__ = "0.0.0-auto.0"
34+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
2435

2536

2637
def load(
27-
file,
28-
width,
29-
height,
30-
data_start,
31-
colors,
32-
color_depth,
33-
compression,
38+
file: BufferedReader,
39+
width: int,
40+
height: int,
41+
data_start: int,
42+
colors: int,
43+
color_depth: int,
44+
compression: int,
3445
*,
35-
bitmap=None,
36-
palette=None
37-
):
46+
bitmap: BitmapConstructor = None,
47+
palette: PaletteConstructor = None,
48+
) -> Tuple[Bitmap, Optional[Palette]]:
3849
"""Loads indexed bitmap data into bitmap and palette objects.
3950
4051
:param file file: The open bmp file
@@ -46,7 +57,7 @@ def load(
4657
:param int compression: 0 - none, 1 - 8bit RLE, 2 - 4bit RLE"""
4758
# pylint: disable=too-many-arguments,too-many-locals,too-many-branches
4859
if palette:
49-
palette = palette(colors)
60+
palette = palette(colors) # type: Palette
5061

5162
file.seek(data_start - colors * 4)
5263
for value in range(colors):
@@ -67,7 +78,7 @@ def load(
6778

6879
# convert unsigned int to signed int when height is negative
6980
height = negative_height_check(height)
70-
bitmap = bitmap(width, abs(height), colors)
81+
bitmap = bitmap(width, abs(height), colors) # type: Bitmap
7182
file.seek(data_start)
7283
line_size = width // (8 // color_depth)
7384
if width % (8 // color_depth) != 0:
@@ -122,7 +133,13 @@ def load(
122133
return bitmap, palette
123134

124135

125-
def decode_rle(bitmap, file, compression, y_range, width):
136+
def decode_rle(
137+
bitmap: Bitmap,
138+
file: BufferedReader,
139+
compression: int,
140+
y_range: Tuple[int, int, int],
141+
width: int,
142+
) -> None:
126143
"""Helper to decode RLE images"""
127144
# pylint: disable=too-many-locals,too-many-nested-blocks,too-many-branches
128145

adafruit_imageload/bmp/negative_height_check.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# SPDX-FileCopyrightText: 2018 Scott Shawcroft for Adafruit Industries
2+
# SPDX-FileCopyrightText: 2022 Matt Land
23
#
34
# SPDX-License-Identifier: MIT
45

56
"""
67
Check for negative height on the BMP.
78
Seperated into it's own file to support builds
89
without longint.
10+
11+
* Author(s): Tim Cocks, Matt Land
912
"""
1013

1114

12-
def negative_height_check(height):
15+
def negative_height_check(height: int) -> int:
1316
"""Check the height return modified if negative."""
1417
if height > 0x7FFFFFFF:
1518
return height - 4294967296

adafruit_imageload/displayio_types.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# SPDX-FileCopyrightText: 2022 Matt Land
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""
5+
`adafruit_imageload.displayio_types`
6+
====================================================
7+
8+
This is a utility file for type aliases.
9+
https://mypy.readthedocs.io/en/stable/kinds_of_types.html#type-aliases
10+
Type aliases contain compound declarations (used many places in the project) with a single
11+
definition readable by humans.
12+
13+
* Author(s): Matt Land
14+
15+
"""
16+
try:
17+
from typing import Callable
18+
from displayio import Palette, Bitmap
19+
20+
PaletteConstructor = Callable[[int], Palette]
21+
BitmapConstructor = Callable[[int, int, int], Bitmap]
22+
except ImportError:
23+
pass
24+
25+
__version__ = "0.0.0-auto.0"
26+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"

adafruit_imageload/gif.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# SPDX-FileCopyrightText: 2019 Radomir Dopieralski for Adafruit Industries
2+
# SPDX-FileCopyrightText: 2022 Matt Land
23
#
34
# SPDX-License-Identifier: MIT
45

@@ -9,30 +10,45 @@
910
Load pixel values (indices or colors) into a bitmap and colors into a palette
1011
from a GIF file.
1112
12-
* Author(s): Radomir Dopieralski
13+
* Author(s): Radomir Dopieralski, Matt Land
1314
1415
"""
1516

1617
import struct
1718

19+
try:
20+
from typing import Tuple, Iterator, Optional, List
21+
from io import BufferedReader
22+
from displayio import Palette, Bitmap
23+
from .displayio_types import PaletteConstructor, BitmapConstructor
24+
except ImportError:
25+
pass
1826

1927
__version__ = "0.0.0-auto.0"
2028
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
2129

2230

23-
def load(file, *, bitmap=None, palette=None):
31+
def load(
32+
file: BufferedReader,
33+
*,
34+
bitmap: BitmapConstructor,
35+
palette: PaletteConstructor = None
36+
) -> Tuple[Bitmap, Optional[Palette]]:
2437
"""Loads a GIF image from the open ``file``.
2538
2639
Returns tuple of bitmap object and palette object.
2740
41+
:param BufferedReader file: The *.gif file being loaded
2842
:param object bitmap: Type to store bitmap data. Must have API similar to `displayio.Bitmap`.
2943
Will be skipped if None
3044
:param object palette: Type to store the palette. Must have API similar to
3145
`displayio.Palette`. Will be skipped if None"""
3246
header = file.read(6)
3347
if header not in {b"GIF87a", b"GIF89a"}:
3448
raise ValueError("Not a GIF file")
35-
width, height, flags, _, _ = struct.unpack("<HHBBB", file.read(7))
49+
width, height, flags, _, _ = struct.unpack( # pylint: disable=no-member
50+
"<HHBBB", file.read(7)
51+
)
3652
if (flags & 0x80) != 0:
3753
palette_size = 1 << ((flags & 0x07) + 1)
3854
palette_obj = palette(palette_size)
@@ -57,9 +73,11 @@ def load(file, *, bitmap=None, palette=None):
5773
return bitmap_obj, palette_obj
5874

5975

60-
def _read_frame(file, bitmap):
61-
"""Read a signle frame and apply it to the bitmap."""
62-
ddx, ddy, width, _, flags = struct.unpack("<HHHHB", file.read(9))
76+
def _read_frame(file: BufferedReader, bitmap: Bitmap) -> None:
77+
"""Read a single frame and apply it to the bitmap."""
78+
ddx, ddy, width, _, flags = struct.unpack( # pylint: disable=no-member
79+
"<HHHHB", file.read(9)
80+
)
6381
if (flags & 0x40) != 0:
6482
raise NotImplementedError("Interlacing not supported")
6583
if (flags & 0x80) != 0:
@@ -78,7 +96,7 @@ def _read_frame(file, bitmap):
7896
y += 1
7997

8098

81-
def _read_blockstream(file):
99+
def _read_blockstream(file: BufferedReader) -> Iterator[int]:
82100
"""Read a block from a file."""
83101
while True:
84102
size = file.read(1)[0]
@@ -95,21 +113,21 @@ class EndOfData(Exception):
95113
class LZWDict:
96114
"""A dictionary of LZW codes."""
97115

98-
def __init__(self, code_size):
116+
def __init__(self, code_size: int) -> None:
99117
self.code_size = code_size
100118
self.clear_code = 1 << code_size
101119
self.end_code = self.clear_code + 1
102-
self.codes = []
103-
self.last = None
120+
self.codes = [] # type: List[bytes]
121+
self.last = b""
104122
self.clear()
105123

106-
def clear(self):
124+
def clear(self) -> None:
107125
"""Reset the dictionary to default codes."""
108126
self.last = b""
109127
self.code_len = self.code_size + 1
110128
self.codes[:] = []
111129

112-
def decode(self, code):
130+
def decode(self, code: int) -> bytes:
113131
"""Decode a code."""
114132
if code == self.clear_code:
115133
self.clear()
@@ -133,7 +151,7 @@ def decode(self, code):
133151
return value
134152

135153

136-
def lzw_decode(data, code_size):
154+
def lzw_decode(data: Iterator[int], code_size: int) -> Iterator[bytes]:
137155
"""Decode LZW-compressed data."""
138156
dictionary = LZWDict(code_size)
139157
bit = 0

0 commit comments

Comments
 (0)