Skip to content

Commit 71d3858

Browse files
committed
Fix type annotations
1 parent e563a1e commit 71d3858

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

pycheribuild/mtree.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import typing
3939
from collections import OrderedDict
4040
from pathlib import Path, PurePath, PurePosixPath
41-
from typing import Optional, Union
41+
from typing import Iterator, Optional, Union
4242

4343
from .utils import fatal_error, status_update, warning_message
4444

@@ -117,7 +117,7 @@ def __repr__(self) -> str:
117117

118118
class MtreeSubtree(collections.abc.MutableMapping):
119119
def __init__(self):
120-
self.entry: MtreeEntry = None
120+
self.entry: "Optional[MtreeEntry]" = None
121121
self.children: "dict[str, MtreeSubtree]" = OrderedDict()
122122

123123
@staticmethod
@@ -172,7 +172,7 @@ def __len__(self):
172172
ret += len(c)
173173
return ret
174174

175-
def _glob(self, patfrags, prefix, *, case_sensitive=False):
175+
def _glob(self, patfrags: "list[str]", prefix: MtreePath, *, case_sensitive=False) -> Iterator[MtreePath]:
176176
if len(patfrags) == 0:
177177
if self.entry is not None:
178178
yield prefix
@@ -185,11 +185,12 @@ def _glob(self, patfrags, prefix, *, case_sensitive=False):
185185
return
186186
for k, v in self.children.items():
187187
if fnmatch.fnmatch(k, patfrag):
188+
# noinspection PyProtectedMember
188189
yield from v._glob(patfrags, prefix / k, case_sensitive=case_sensitive)
189190

190-
def glob(self, pattern, *, case_sensitive=False):
191+
def glob(self, pattern: str, *, case_sensitive=False) -> Iterator[MtreePath]:
191192
if len(pattern) == 0:
192-
return
193+
raise StopIteration() # pytype does not like plain "return"
193194
head, tail = os.path.split(pattern)
194195
patfrags = [tail]
195196
while head:
@@ -202,7 +203,7 @@ def _walk(self, top, prefix):
202203
if split is not None:
203204
return self.children[split[0]]._walk(split[1], prefix / split[0])
204205
if self.entry is not None and self.entry.attributes["type"] != "dir":
205-
return
206+
raise StopIteration() # pytype does not like plain "return"
206207
files = []
207208
dirs = []
208209
for k, v in self.children.items():
@@ -268,7 +269,7 @@ def _ensure_mtree_mode_fmt(mode: "Union[str, int]") -> str:
268269
return mode
269270

270271
@staticmethod
271-
def _ensure_mtree_path_fmt(path: str) -> str:
272+
def _ensure_mtree_path_fmt(path: str) -> MtreePath:
272273
# The path in mtree always starts with ./
273274
assert not path.endswith("/")
274275
assert path, "PATH WAS EMPTY?"
@@ -394,7 +395,7 @@ def add_dir(self, path, mode=None, uname="root", gname="wheel", print_status=Tru
394395
status_update("Adding dir", path, "to mtree as", entry, file=sys.stderr)
395396
self._mtree[mtree_path] = entry
396397

397-
def add_from_mtree(self, mtree_file, path, print_status=True):
398+
def add_from_mtree(self, mtree_file: "MtreeFile", path: "Union[PurePath, str]", print_status=True):
398399
if isinstance(path, PurePath):
399400
path = str(path)
400401
assert not path.startswith("/")
@@ -471,7 +472,7 @@ def get(self, key):
471472
def root(self):
472473
return self._mtree
473474

474-
def glob(self, pattern, *, case_sensitive=False):
475+
def glob(self, pattern: str, *, case_sensitive=False) -> Iterator[MtreePath]:
475476
return self._mtree.glob(pattern, case_sensitive=case_sensitive)
476477

477478
def walk(self, top):

pycheribuild/projects/disk_image.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@
3333
import shutil
3434
import sys
3535
import tempfile
36+
import typing
3637
from enum import Enum
3738
from functools import cached_property
38-
from pathlib import Path
39-
from typing import Optional
39+
from pathlib import Path, PurePath
40+
from typing import Optional, Union
4041

4142
from .cross.cheribsd import BuildCHERIBSD, BuildFreeBSD, BuildFreeBSDWithDefaultOptions
4243
from .cross.gdb import BuildGDB, BuildKGDB
@@ -144,7 +145,7 @@ class BuildDiskImageBase(SimpleProject):
144145

145146
@classproperty
146147
def default_architecture(self) -> CrossCompileTarget:
147-
return self._source_class.default_architecture
148+
return typing.cast(CrossCompileTarget, self._source_class.default_architecture)
148149

149150
@classproperty
150151
def supported_architectures(self) -> "tuple[CrossCompileTarget, ...]":
@@ -241,6 +242,7 @@ def __init__(self, *args, **kwargs) -> None:
241242
self.hostname = os.path.expandvars(self.hostname) # Expand env vars in hostname to allow $CHERI_BITS
242243
# MIPS needs big-endian disk images
243244
self.big_endian = self.compiling_for_mips(include_purecap=True)
245+
self.stripped_contents: "dict[Union[str,Path], Path]" = {}
244246

245247
@cached_property
246248
def source_project(self) -> BuildFreeBSD:
@@ -324,7 +326,13 @@ def create_file_for_image(
324326
self.write_file(target_file, contents, never_print_cmd=True, overwrite=False, mode=mode)
325327
self.add_file_to_image(target_file, base_directory=base_dir)
326328

327-
def add_from_mtree(self, mtree_file, mtree_path, strip_binaries: "Optional[bool]" = None, print_status=None):
329+
def add_from_mtree(
330+
self,
331+
mtree_file: MtreeFile,
332+
mtree_path: "Union[str, PurePath]",
333+
strip_binaries: "Optional[bool]" = None,
334+
print_status=None,
335+
):
328336
if strip_binaries is None:
329337
strip_binaries = self.strip_binaries
330338
kwargs = {}
@@ -1114,7 +1122,6 @@ def __init__(self, *args, **kwargs):
11141122
self.file_templates = BuildMinimalCheriBSDDiskImage._MinimalFileTemplates()
11151123
self.is_minimal = True
11161124
self.ref_mtree = MtreeFile(verbose=self.config.verbose)
1117-
self.stripped_contents: "dict[str, Path]" = {}
11181125

11191126
def setup(self):
11201127
super().setup()
@@ -1229,7 +1236,7 @@ def add_unlisted_files_to_metalog(self):
12291236
extra_files.append(new_file)
12301237
# Stripping kernel modules makes them unloadable:
12311238
# kldload: /boot/kernel/smbfs.ko: file must have exactly one symbol table
1232-
self.add_from_mtree(new_file, strip_binaries=False)
1239+
self.add_from_mtree(self.ref_mtree, new_file, strip_binaries=False)
12331240
self.verbose_print("Boot files:\n\t", "\n\t".join(map(str, sorted(extra_files))))
12341241
self.verbose_print("Not adding unlisted files to METALOG since we are building a minimal image")
12351242

0 commit comments

Comments
 (0)