Skip to content

Commit d3fbbca

Browse files
committed
Updated type annotations
1 parent 7c47d9e commit d3fbbca

File tree

8 files changed

+45
-29
lines changed

8 files changed

+45
-29
lines changed

adafruit_bitmap_font/bdf.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
"""
2424

2525
try:
26-
from typing import Union, Optional, Tuple
26+
from typing import Union, Optional, Tuple, Iterable
27+
from io import FileIO
28+
from displayio import Bitmap
2729
except ImportError:
2830
pass
2931

3032
import gc
31-
from io import FileIO
3233
from fontio import Glyph
33-
from displayio import Bitmap
3434
from .glyph_cache import GlyphCache
3535

3636
__version__ = "0.0.0-auto.0"
@@ -120,7 +120,7 @@ def get_bounding_box(self) -> Tuple[int, int, int, int]:
120120
"""Return the maximum glyph size as a 4-tuple of: width, height, x_offset, y_offset"""
121121
return self._boundingbox
122122

123-
def load_glyphs(self, code_points: Union[int, str, set]) -> None:
123+
def load_glyphs(self, code_points: Union[int, str, Iterable]) -> None:
124124
# pylint: disable=too-many-statements,too-many-branches,too-many-nested-blocks,too-many-locals
125125
metadata = True
126126
character = False

adafruit_bitmap_font/bitmap_font.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,39 @@
2424

2525
try:
2626
from typing import Optional, Union
27+
from displayio import Bitmap
28+
from . import bdf
29+
from . import pcf
30+
from . import ttf
2731
except ImportError:
2832
pass
2933

30-
from displayio import Bitmap
31-
from . import bdf
32-
from . import pcf
33-
from . import ttf
34-
3534
__version__ = "0.0.0-auto.0"
3635
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Bitmap_Font.git"
3736

3837

3938
def load_font(
4039
filename: str, bitmap: Optional[Bitmap] = None
41-
) -> Union[bdf.BDF, pcf.PCF, ttf.TTF, None]:
40+
) -> Union[bdf.BDF, pcf.PCF, ttf.TTF]:
4241
"""Loads a font file. Returns None if unsupported."""
43-
# pylint: disable=import-outside-toplevel, consider-using-with
42+
# pylint: disable=import-outside-toplevel, redefined-outer-name, consider-using-with
4443
if not bitmap:
45-
bitmap = Bitmap
44+
import displayio
45+
46+
bitmap = displayio.Bitmap
4647
font_file = open(filename, "rb")
4748
first_four = font_file.read(4)
4849
if filename.endswith("bdf") and first_four == b"STAR":
50+
from . import bdf
51+
4952
return bdf.BDF(font_file, bitmap)
5053
if filename.endswith("pcf") and first_four == b"\x01fcp":
54+
from . import pcf
55+
5156
return pcf.PCF(font_file, bitmap)
5257
if filename.endswith("ttf") and first_four == b"\x00\x01\x00\x00":
58+
from . import ttf
59+
5360
return ttf.TTF(font_file, bitmap)
5461

5562
raise ValueError("Unknown magic number %r" % first_four)

adafruit_bitmap_font/glyph_cache.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
"""
2424

2525
try:
26-
from typing import Union, Optional
26+
from typing import Union, Iterable
27+
from fontio import Glyph
2728
except ImportError:
2829
pass
2930

3031
import gc
31-
from fontio import Glyph
3232

3333
__version__ = "0.0.0-auto.0"
3434
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Bitmap_Font.git"
@@ -40,10 +40,10 @@ class GlyphCache:
4040
def __init__(self) -> None:
4141
self._glyphs = {}
4242

43-
def load_glyphs(self, code_points: Union[int, str, set]) -> None:
43+
def load_glyphs(self, code_points: Union[int, str, Iterable]) -> None:
4444
"""Loads displayio.Glyph objects into the GlyphCache from the font."""
4545

46-
def get_glyph(self, code_point: int) -> Optional[Glyph]:
46+
def get_glyph(self, code_point: int) -> Glyph:
4747
"""Returns a displayio.Glyph for the given code point or None is unsupported."""
4848
if code_point in self._glyphs:
4949
return self._glyphs[code_point]

adafruit_bitmap_font/pcf.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@
2323
"""
2424

2525
try:
26-
from typing import Union, Generator
26+
from typing import Union, Tuple, Iterator, Iterable
27+
from io import FileIO
28+
from displayio import Bitmap as displayioBitmap
2729
except ImportError:
2830
pass
2931

3032
from collections import namedtuple
3133
import gc
3234
import struct
33-
from io import FileIO
3435
from micropython import const
3536
from fontio import Glyph
36-
from displayio import Bitmap as displayioBitmap
3737
from .glyph_cache import GlyphCache
3838

3939
try:
@@ -148,18 +148,18 @@ def descent(self) -> int:
148148
"""The number of pixels below the baseline of a typical descender"""
149149
return self._descent
150150

151-
def get_bounding_box(self) -> tuple:
151+
def get_bounding_box(self) -> Tuple[int, int, int, int]:
152152
"""Return the maximum glyph size as a 4-tuple of: width, height, x_offset, y_offset"""
153153
return self._bounding_box
154154

155-
def _read(self, format_: str) -> tuple:
155+
def _read(self, format_: str) -> Tuple:
156156
size = struct.calcsize(format_)
157157
if size != len(self.buffer):
158158
self.buffer = bytearray(size)
159159
self.file.readinto(self.buffer)
160160
return struct.unpack_from(format_, self.buffer)
161161

162-
def _seek_table(self, table: dict) -> int:
162+
def _seek_table(self, table: Table) -> int:
163163
self.file.seek(table.offset)
164164
(format_,) = self._read("<I")
165165

@@ -266,7 +266,7 @@ def _read_accelerator_tables(self) -> Accelerators:
266266
ink_maxbounds,
267267
)
268268

269-
def _read_properties(self) -> Generator[tuple, None, None]:
269+
def _read_properties(self) -> Iterator[Tuple[bytes, Union[bytes, int]]]:
270270
property_table_offset = self.tables[_PCF_PROPERTIES]["offset"]
271271
self.file.seek(property_table_offset)
272272
(format_,) = self._read("<I")
@@ -297,7 +297,7 @@ def _read_properties(self) -> Generator[tuple, None, None]:
297297
else:
298298
yield (string_map[name_offset], value)
299299

300-
def load_glyphs(self, code_points: Union[int, str, set]) -> None:
300+
def load_glyphs(self, code_points: Union[int, str, Iterable]) -> None:
301301
# pylint: disable=too-many-statements,too-many-branches,too-many-nested-blocks,too-many-locals
302302
if isinstance(code_points, int):
303303
code_points = (code_points,)

adafruit_bitmap_font/ttf.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
# pylint: skip-file
66
# Remove the above when TTF is actually supported.
77

8+
try:
9+
from typing import Tuple
10+
from io import FileIO
11+
from displayio import Bitmap
12+
except ImportError:
13+
pass
14+
815
import struct
9-
from io import FileIO
10-
from displayio import Bitmap
1116

1217
# https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6glyf.html
1318

@@ -19,7 +24,7 @@ def __init__(self, f: FileIO, bitmap: Bitmap) -> None:
1924

2025
self.characters = {}
2126

22-
def read(format: str) -> tuple:
27+
def read(format: str) -> Tuple:
2328
s = struct.calcsize(format)
2429
return struct.unpack_from(format, f.read(s))
2530

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# Uncomment the below if you use native CircuitPython modules such as
2626
# digitalio, micropython and busio. List the modules you use. Without it, the
2727
# autodoc module docs will fail to generate with a warning.
28-
autodoc_mock_imports = ["displayio", "fontio"]
28+
autodoc_mock_imports = ["fontio"]
2929

3030

3131
intersphinx_mapping = {

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
# SPDX-License-Identifier: Unlicense
44

55
Adafruit-Blinka
6+
Adafruit-Blinka-displayio

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@
3333
# Author details
3434
author="Adafruit Industries",
3535
author_email="[email protected]",
36-
install_requires=["Adafruit-Blinka"],
36+
install_requires=[
37+
"Adafruit-Blinka",
38+
"Adafruit-Blinka-displayio",
39+
],
3740
# Choose your license
3841
license="MIT",
3942
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers

0 commit comments

Comments
 (0)