Skip to content

Commit 8429037

Browse files
Merge pull request #41 from source-foundry/dev
Add type hints and static type checking with mypy
2 parents 748b459 + d40e624 commit 8429037

File tree

8 files changed

+42
-35
lines changed

8 files changed

+42
-35
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ matrix:
1010
dist: xenial
1111
env: TOX_ENV=static-type-checks
1212
install:
13-
- pip install --upgrade pytype
13+
- pip install --upgrade mypy
1414
- pip install -r requirements.txt
1515
script: make test-type-check
1616
- python: 3.7

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ test-lint:
3232
flake8 --ignore=E501,W50 lib/dehinter
3333

3434
test-type-check:
35-
pytype lib/dehinter
35+
mypy lib/dehinter
3636

3737
test-unit:
3838
tox

lib/dehinter/__main__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
import os
1717
import pprint
1818
import sys
19+
from typing import List
1920

20-
from fontTools.ttLib import TTFont
21+
from fontTools.ttLib import TTFont # type: ignore
2122

2223
from dehinter import __version__
2324
from dehinter.font import is_truetype_font
@@ -46,11 +47,11 @@
4647
from dehinter.system import get_filesize
4748

4849

49-
def main(): # pragma: no cover
50+
def main() -> None: # pragma: no cover
5051
run(sys.argv[1:])
5152

5253

53-
def run(argv):
54+
def run(argv: List[str]) -> None:
5455
# instantiate pretty printer
5556
pp = pprint.PrettyPrinter(indent=4)
5657

lib/dehinter/bitops.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
# limitations under the License.
1414

1515

16-
def is_bit_k_set(int_val, k):
16+
def is_bit_k_set(int_val: int, k: int) -> bool:
1717
"""Tests if the value of bit at offset k in an integer is set"""
1818
return (int_val & (1 << k)) != 0
1919

2020

21-
def clear_bit_k(int_val, k):
21+
def clear_bit_k(int_val: int, k: int) -> int:
2222
"""Clears the bit at offset k"""
2323
return int_val & ~(1 << k)
2424

2525

26-
def set_bit_k(int_val, k):
26+
def set_bit_k(int_val: int, k: int) -> int:
2727
"""Sets the bit at offset k"""
2828
return int_val | (1 << k)

lib/dehinter/font.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,67 +13,69 @@
1313
# limitations under the License.
1414

1515
import array
16+
import os
17+
from typing import Union
1618

1719
from dehinter.bitops import is_bit_k_set, clear_bit_k
1820

1921

2022
# ========================================================
2123
# Utilities
2224
# ========================================================
23-
def has_cvt_table(tt):
25+
def has_cvt_table(tt) -> bool:
2426
"""Tests for the presence of a cvt table in a TrueType font."""
2527
return "cvt " in tt
2628

2729

28-
def has_fpgm_table(tt):
30+
def has_fpgm_table(tt) -> bool:
2931
"""Tests for the presence of a fpgm table in a TrueType font."""
3032
return "fpgm" in tt
3133

3234

33-
def has_gasp_table(tt):
35+
def has_gasp_table(tt) -> bool:
3436
"""Tests for the presence of a gasp table in a TrueType font."""
3537
return "gasp" in tt
3638

3739

38-
def has_hdmx_table(tt):
40+
def has_hdmx_table(tt) -> bool:
3941
"""Tests for the presence of a hdmx table in a TrueType font."""
4042
return "hdmx" in tt
4143

4244

43-
def has_ltsh_table(tt):
45+
def has_ltsh_table(tt) -> bool:
4446
"""Tests for the presence of a LTSH table in a TrueType font."""
4547
return "LTSH" in tt
4648

4749

48-
def has_prep_table(tt):
50+
def has_prep_table(tt) -> bool:
4951
"""Tests for the presence of a prep table in a TrueType font."""
5052
return "prep" in tt
5153

5254

53-
def has_ttfa_table(tt):
55+
def has_ttfa_table(tt) -> bool:
5456
"""Tests for the presence of a TTFA table in a TrueType font."""
5557
return "TTFA" in tt
5658

5759

58-
def has_vdmx_table(tt):
60+
def has_vdmx_table(tt) -> bool:
5961
"""Tests for the presence of a VDMX table in a TrueType font."""
6062
return "VDMX" in tt
6163

6264

63-
def is_truetype_font(filepath):
65+
def is_truetype_font(filepath: Union[bytes, str, "os.PathLike[str]"]) -> bool:
6466
"""Tests that a font has the TrueType file signature of either:
6567
1) b'\x00\x01\x00\x00'
6668
2) b'\x74\x72\x75\x65' == 'true'"""
6769
with open(filepath, "rb") as f:
68-
file_signature = f.read(4)
70+
file_signature: bytes = f.read(4)
6971

7072
return file_signature in (b"\x00\x01\x00\x00", b"\x74\x72\x75\x65")
7173

7274

7375
# ========================================================
7476
# OpenType table removal
7577
# ========================================================
76-
def remove_cvt_table(tt):
78+
def remove_cvt_table(tt) -> None:
7779
"""Removes cvt table from a fontTools.ttLib.TTFont object"""
7880
try:
7981
del tt["cvt "]
@@ -82,7 +84,7 @@ def remove_cvt_table(tt):
8284
pass
8385

8486

85-
def remove_fpgm_table(tt):
87+
def remove_fpgm_table(tt) -> None:
8688
"""Removes fpgm table from a fontTools.ttLib.TTFont object"""
8789
try:
8890
del tt["fpgm"]
@@ -91,7 +93,7 @@ def remove_fpgm_table(tt):
9193
pass
9294

9395

94-
def remove_hdmx_table(tt):
96+
def remove_hdmx_table(tt) -> None:
9597
"""Removes hdmx table from a fontTools.ttLib.TTFont object"""
9698
try:
9799
del tt["hdmx"]
@@ -100,7 +102,7 @@ def remove_hdmx_table(tt):
100102
pass
101103

102104

103-
def remove_ltsh_table(tt):
105+
def remove_ltsh_table(tt) -> None:
104106
"""Removes LTSH table from a fontTools.ttLib.TTFont object."""
105107
try:
106108
del tt["LTSH"]
@@ -109,7 +111,7 @@ def remove_ltsh_table(tt):
109111
pass
110112

111113

112-
def remove_prep_table(tt):
114+
def remove_prep_table(tt) -> None:
113115
"""Removes prep table from a fontTools.ttLib.TTFont object"""
114116
try:
115117
del tt["prep"]
@@ -118,7 +120,7 @@ def remove_prep_table(tt):
118120
pass
119121

120122

121-
def remove_ttfa_table(tt):
123+
def remove_ttfa_table(tt) -> None:
122124
"""Removes TTFA table from a fontTools.ttLib.TTFont object"""
123125
try:
124126
del tt["TTFA"]
@@ -127,7 +129,7 @@ def remove_ttfa_table(tt):
127129
pass
128130

129131

130-
def remove_vdmx_table(tt):
132+
def remove_vdmx_table(tt) -> None:
131133
"""Removes TTFA table from a fontTools.ttLib.TTFont object"""
132134
try:
133135
del tt["VDMX"]
@@ -139,9 +141,9 @@ def remove_vdmx_table(tt):
139141
# ========================================================
140142
# glyf table instruction set bytecode removal
141143
# ========================================================
142-
def remove_glyf_instructions(tt):
144+
def remove_glyf_instructions(tt) -> int:
143145
"""Removes instruction set bytecode from glyph definitions in the glyf table."""
144-
glyph_number = 0
146+
glyph_number: int = 0
145147
for glyph in tt["glyf"].glyphs.values():
146148
glyph.expand(tt["glyf"])
147149
if hasattr(glyph, "program") and glyph.program.bytecode != array.array("B", []):
@@ -157,7 +159,7 @@ def remove_glyf_instructions(tt):
157159
# ========================================================
158160
# gasp table edit
159161
# ========================================================
160-
def update_gasp_table(tt):
162+
def update_gasp_table(tt) -> bool:
161163
"""Modifies the following gasp table fields:
162164
1) rangeMaxPPEM changed to 65535
163165
2) rangeGaspBehavior changed to 0x000a (symmetric grayscale, no gridfit)"""
@@ -171,9 +173,9 @@ def update_gasp_table(tt):
171173
# =========================================
172174
# maxp table edits
173175
# =========================================
174-
def update_maxp_table(tt):
176+
def update_maxp_table(tt) -> bool:
175177
"""Update the maxp table with new values based on elimination of instruction sets."""
176-
changed = False
178+
changed: bool = False
177179
if tt["maxp"].maxZones != 0:
178180
tt["maxp"].maxZones = 0
179181
changed = True
@@ -198,7 +200,7 @@ def update_maxp_table(tt):
198200
# =========================================
199201
# head table edits
200202
# =========================================
201-
def update_head_table_flags(tt):
203+
def update_head_table_flags(tt) -> bool:
202204
if is_bit_k_set(tt["head"].flags, 4):
203205
# confirm that there is no LTSH or hdmx table
204206
# bit 4 should be set if either of these tables are present in font

lib/dehinter/paths.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313
# limitations under the License.
1414

1515
import os
16+
from typing import Union
1617

1718

18-
def filepath_exists(filepath):
19+
def filepath_exists(filepath: Union[bytes, str, "os.PathLike[str]"]) -> bool:
1920
"""Tests a file path string to confirm that the file exists on the file system"""
2021
return os.path.isfile(filepath)
2122

2223

23-
def get_default_out_path(filepath):
24+
def get_default_out_path(
25+
filepath: os.PathLike,
26+
) -> Union[str, bytes]:
2427
"""Returns an updated file path that is used as dehinted file default when user
2528
does not specify an out file path."""
2629
dir_path, file_path = os.path.split(filepath)

lib/dehinter/system.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
# limitations under the License.
1414

1515
import os
16+
from typing import Tuple, Union
1617

1718

18-
def get_filesize(filepath):
19+
def get_filesize(filepath: Union[str, bytes, "os.PathLike[str]"]) -> Tuple[str, str]:
1920
"""Returns formatted file size tuple fit for printing to end user."""
2021
filesize = os.path.getsize(filepath)
2122
kb_factor = 1 << 10

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# Optional packages
1919
EXTRAS_REQUIRES = {
2020
# for developer installs
21-
"dev": ["coverage", "pytest", "tox", "flake8", "pytype"],
21+
"dev": ["coverage", "pytest", "tox", "flake8", "mypy"],
2222
# for maintainer installs
2323
"maintain": ["wheel", "setuptools", "twine"],
2424
}

0 commit comments

Comments
 (0)