Skip to content

Commit b8cf1c0

Browse files
committed
doc: annotate types in hash bin delegation example
Signed-off-by: Lukas Puehringer <[email protected]>
1 parent e90acfe commit b8cf1c0

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

examples/repo_example/hashed_bin_delegation.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from collections import OrderedDict
2323
from datetime import datetime, timedelta
2424
from pathlib import Path
25+
from typing import Any, Dict, Iterator, List, Tuple
2526

2627
from securesystemslib.keys import generate_ed25519_key
2728
from securesystemslib.signer import SSlibSigner
@@ -37,14 +38,14 @@
3738
from tuf.api.serialization.json import JSONSerializer
3839

3940

40-
def _in(days):
41+
def _in(days: float) -> datetime:
4142
"""Adds 'days' to now and returns datetime object w/o microseconds."""
4243
return datetime.utcnow().replace(microsecond=0) + timedelta(days=days)
4344

4445

4546
SPEC_VERSION = "1.0.19"
46-
roles = {}
47-
keys = {}
47+
roles: Dict[str, Metadata] = {}
48+
keys: Dict[str, Dict[str, Any]] = {}
4849

4950
# Hash bin delegation
5051
# ===================
@@ -83,7 +84,7 @@ def _in(days):
8384

8485
# Helpers
8586
# -------
86-
def _bin_name(low, high):
87+
def _bin_name(low: int, high: int) -> str:
8788
"""Generates a bin name according to the hash prefixes the bin serves.
8889
8990
The name is either a single hash prefix for bin size 1, or a range of hash
@@ -96,7 +97,7 @@ def _bin_name(low, high):
9697
return f"{low:0{PREFIX_LEN}x}-{high:0{PREFIX_LEN}x}"
9798

9899

99-
def generate_hash_bins():
100+
def generate_hash_bins() -> Iterator[Tuple[str, List[str]]]:
100101
"""Returns generator for bin names and hash prefixes per bin."""
101102
# Iterate over the total number of hash prefixes in 'bin size'-steps to
102103
# generate bin names and a list of hash prefixes served by each bin.
@@ -110,7 +111,7 @@ def generate_hash_bins():
110111
yield bin_name, hash_prefixes
111112

112113

113-
def find_hash_bin(path):
114+
def find_hash_bin(path: str) -> str:
114115
"""Returns name of bin for target file based on the target path hash."""
115116
# Generate hash digest of passed target path and take its prefix, given the
116117
# global prefix length for the given number of bins.
@@ -201,7 +202,7 @@ def find_hash_bin(path):
201202
# about adding target file infos to targets metadata.
202203
local_path = Path(__file__).resolve()
203204
target_path = f"{local_path.parts[-2]}/{local_path.parts[-1]}"
204-
target_file_info = TargetFile.from_file(target_path, local_path)
205+
target_file_info = TargetFile.from_file(target_path, str(local_path))
205206

206207
# The right bin for a target file is determined by the 'target_path' hash, e.g.:
207208
#

0 commit comments

Comments
 (0)