Skip to content

Commit 18e2836

Browse files
madigschriftgestalt
andcommitted
Break off typing part of #3826
Co-authored-by: schriftgestalt <[email protected]>
1 parent 3c18225 commit 18e2836

File tree

5 files changed

+677
-112
lines changed

5 files changed

+677
-112
lines changed

Lib/fontTools/misc/configTools.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,9 @@ def register_option(
224224

225225
def __init__(
226226
self,
227-
values: Union[AbstractConfig, Dict[Union[Option, str], Any]] = {},
227+
values: Union[
228+
AbstractConfig, Dict[Union[Option, str], Any], Mapping[str, Any]
229+
] = {},
228230
parse_values: bool = False,
229231
skip_unknown: bool = False,
230232
):

Lib/fontTools/misc/fixedTools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
MAX_F2DOT14 = 0x7FFF / (1 << 14)
4040

4141

42-
def fixedToFloat(value, precisionBits):
42+
def fixedToFloat(value: float, precisionBits: int) -> float:
4343
"""Converts a fixed-point number to a float given the number of
4444
precision bits.
4545

Lib/fontTools/misc/xmlWriter.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"""xmlWriter.py -- Simple XML authoring class"""
22

3+
from __future__ import annotations
4+
5+
from typing import BinaryIO, Callable, TextIO
36
from fontTools.misc.textTools import byteord, strjoin, tobytes, tostr
47
import sys
58
import os
@@ -25,17 +28,19 @@
2528
class XMLWriter(object):
2629
def __init__(
2730
self,
28-
fileOrPath,
29-
indentwhite=INDENT,
30-
idlefunc=None,
31-
encoding="utf_8",
32-
newlinestr="\n",
33-
):
31+
fileOrPath: str | os.PathLike[str] | BinaryIO | TextIO,
32+
indentwhite: str = INDENT,
33+
idlefunc: Callable[[], None] | None = None,
34+
encoding: str = "utf_8",
35+
newlinestr: str | bytes = "\n",
36+
) -> None:
3437
if encoding.lower().replace("-", "").replace("_", "") != "utf8":
3538
raise Exception("Only UTF-8 encoding is supported.")
3639
if fileOrPath == "-":
3740
fileOrPath = sys.stdout
41+
self.filename: str | os.PathLike[str] | None
3842
if not hasattr(fileOrPath, "write"):
43+
assert isinstance(fileOrPath, (str, os.PathLike))
3944
self.filename = fileOrPath
4045
self.file = open(fileOrPath, "wb")
4146
self._closeStream = True
@@ -74,8 +79,9 @@ def __enter__(self):
7479
def __exit__(self, exception_type, exception_value, traceback):
7580
self.close()
7681

77-
def close(self):
82+
def close(self) -> None:
7883
if self._closeStream:
84+
assert not isinstance(self.file, (str, os.PathLike))
7985
self.file.close()
8086

8187
def write(self, string, indent=True):
@@ -196,7 +202,7 @@ def escape(data):
196202
if len(data) > maxLen:
197203
preview = repr(data[:maxLen])[1:-1] + "..."
198204
TTX_LOG.warning(
199-
"Illegal XML character(s) found; replacing offending " "string %r with %r",
205+
"Illegal XML character(s) found; replacing offending string %r with %r",
200206
preview,
201207
REPLACEMENT,
202208
)

Lib/fontTools/ttLib/sfnt.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
a table's length changes you need to rewrite the whole file anyway.
1414
"""
1515

16+
from collections.abc import KeysView
1617
from io import BytesIO
1718
from types import SimpleNamespace
1819
from fontTools.misc.textTools import Tag
@@ -84,7 +85,7 @@ def __init__(self, file, checkChecksums=0, fontNumber=-1):
8485

8586
if self.sfntVersion not in ("\x00\x01\x00\x00", "OTTO", "true"):
8687
raise TTLibError("Not a TrueType or OpenType font (bad sfntVersion)")
87-
tables = {}
88+
tables: dict[Tag, DirectoryEntry] = {}
8889
for i in range(self.numTables):
8990
entry = self.DirectoryEntry()
9091
entry.fromFile(self.file)
@@ -96,15 +97,15 @@ def __init__(self, file, checkChecksums=0, fontNumber=-1):
9697
if self.flavor == "woff":
9798
self.flavorData = WOFFFlavorData(self)
9899

99-
def has_key(self, tag):
100+
def has_key(self, tag: str | bytes) -> bool:
100101
return tag in self.tables
101102

102103
__contains__ = has_key
103104

104-
def keys(self):
105+
def keys(self) -> KeysView[Tag]:
105106
return self.tables.keys()
106107

107-
def __getitem__(self, tag):
108+
def __getitem__(self, tag: str | bytes) -> bytes:
108109
"""Fetch the raw table data."""
109110
entry = self.tables[Tag(tag)]
110111
data = entry.loadData(self.file)
@@ -122,10 +123,10 @@ def __getitem__(self, tag):
122123
log.warning("bad checksum for '%s' table", tag)
123124
return data
124125

125-
def __delitem__(self, tag):
126+
def __delitem__(self, tag: str | bytes) -> None:
126127
del self.tables[Tag(tag)]
127128

128-
def close(self):
129+
def close(self) -> None:
129130
self.file.close()
130131

131132
# We define custom __getstate__ and __setstate__ to make SFNTReader pickle-able

0 commit comments

Comments
 (0)