Skip to content

Commit 5aa547d

Browse files
committed
Address more mypy warnings on tests files
All of the changes are made manual. The target files are only those who test the new code. Signed-off-by: Martin Vrachev <[email protected]>
1 parent 8d73330 commit 5aa547d

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

tests/test_examples.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import tempfile
1111
import unittest
1212
from pathlib import Path
13+
from typing import ClassVar, List
1314

1415

1516
class TestRepoExamples(unittest.TestCase):
@@ -20,26 +21,30 @@ class TestRepoExamples(unittest.TestCase):
2021
2122
"""
2223

24+
repo_examples_dir: ClassVar[Path]
25+
2326
@classmethod
24-
def setUpClass(cls):
27+
def setUpClass(cls) -> None:
2528
"""Locate and cache 'repo_example' dir."""
2629
base = Path(__file__).resolve().parents[1]
2730
cls.repo_examples_dir = base / "examples" / "repo_example"
2831

29-
def setUp(self):
32+
def setUp(self) -> None:
3033
"""Create and change into test dir.
3134
NOTE: Test scripts are expected to create dirs/files in new CWD."""
3235
self.original_cwd = os.getcwd()
3336
self.base_test_dir = os.path.realpath(tempfile.mkdtemp())
3437
os.chdir(self.base_test_dir)
3538

36-
def tearDown(self):
39+
def tearDown(self) -> None:
3740
"""Change back to original dir and remove test dir, which may contain
3841
dirs/files the test created at test-time CWD."""
3942
os.chdir(self.original_cwd)
4043
shutil.rmtree(self.base_test_dir)
4144

42-
def _run_script_and_assert_files(self, script_name, filenames_created):
45+
def _run_script_and_assert_files(
46+
self, script_name: str, filenames_created: List[str]
47+
) -> None:
4348
"""Run script in 'repo_example' dir and assert that it created the
4449
files corresponding to the passed filenames inside a 'tmp*' test dir at
4550
CWD."""
@@ -63,7 +68,7 @@ def _run_script_and_assert_files(self, script_name, filenames_created):
6368
metadata_path.exists(), f"missing '{metadata_path}' file"
6469
)
6570

66-
def test_basic_repo(self):
71+
def test_basic_repo(self) -> None:
6772
"""Run 'basic_repo.py' and assert creation of metadata files."""
6873
self._run_script_and_assert_files(
6974
"basic_repo.py",

tests/test_updater_consistent_snapshot.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from tuf.api.metadata import (
1717
SPECIFICATION_VERSION,
1818
TOP_LEVEL_ROLE_NAMES,
19+
TargetFile,
1920
Targets,
2021
)
2122
from tuf.ngclient import Updater
@@ -108,7 +109,9 @@ def _assert_targets_files_exist(self, filenames: Iterable[str]) -> None:
108109
}
109110

110111
@utils.run_sub_tests_with_dataset(top_level_roles_data)
111-
def test_top_level_roles_update(self, test_case_data: Dict[str, Any]):
112+
def test_top_level_roles_update(
113+
self, test_case_data: Dict[str, Any]
114+
) -> None:
112115
# Test if the client fetches and stores metadata files with the
113116
# correct version prefix, depending on 'consistent_snapshot' config
114117
consistent_snapshot: bool = test_case_data["consistent_snapshot"]
@@ -140,7 +143,9 @@ def test_top_level_roles_update(self, test_case_data: Dict[str, Any]):
140143
}
141144

142145
@utils.run_sub_tests_with_dataset(delegated_roles_data)
143-
def test_delegated_roles_update(self, test_case_data: Dict[str, Any]):
146+
def test_delegated_roles_update(
147+
self, test_case_data: Dict[str, Any]
148+
) -> None:
144149
# Test if the client fetches and stores delegated metadata files with
145150
# the correct version prefix, depending on 'consistent_snapshot' config
146151
consistent_snapshot: bool = test_case_data["consistent_snapshot"]
@@ -191,7 +196,7 @@ def test_delegated_roles_update(self, test_case_data: Dict[str, Any]):
191196
}
192197

193198
@utils.run_sub_tests_with_dataset(targets_download_data)
194-
def test_download_targets(self, test_case_data: Dict[str, Any]):
199+
def test_download_targets(self, test_case_data: Dict[str, Any]) -> None:
195200
# Test if the client fetches and stores target files with
196201
# the correct hash prefix, depending on 'consistent_snapshot'
197202
# and 'prefix_targets_with_hash' config
@@ -213,6 +218,7 @@ def test_download_targets(self, test_case_data: Dict[str, Any]):
213218

214219
for targetpath in targetpaths:
215220
info = updater.get_targetinfo(targetpath)
221+
assert isinstance(info, TargetFile)
216222
updater.download_target(info)
217223

218224
# target files are always persisted without hash prefix

tests/test_updater_key_rotations.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
import tempfile
1111
import unittest
1212
from dataclasses import dataclass
13-
from typing import Dict, List, Optional, Type
13+
from typing import ClassVar, Dict, List, Optional, Type
1414

1515
from securesystemslib.signer import SSlibSigner
1616

1717
from tests import utils
1818
from tests.repository_simulator import RepositorySimulator
1919
from tests.utils import run_sub_tests_with_dataset
20-
from tuf.api.metadata import Key, Metadata, Root
20+
from tuf.api.metadata import Key, Root
2121
from tuf.exceptions import UnsignedMetadataError
2222
from tuf.ngclient import Updater
2323

@@ -35,17 +35,18 @@ class TestUpdaterKeyRotations(unittest.TestCase):
3535

3636
# set dump_dir to trigger repository state dumps
3737
dump_dir: Optional[str] = None
38+
temp_dir: ClassVar[tempfile.TemporaryDirectory]
39+
keys: ClassVar[List[Key]]
40+
signers: ClassVar[List[SSlibSigner]]
3841

3942
@classmethod
4043
def setUpClass(cls) -> None:
41-
cls.sim: RepositorySimulator
42-
cls.metadata_dir: str
4344
# pylint: disable-next=consider-using-with
4445
cls.temp_dir = tempfile.TemporaryDirectory()
4546

4647
# Pre-create a bunch of keys and signers
47-
cls.keys: List[Key] = []
48-
cls.signers: List[SSlibSigner] = []
48+
cls.keys = []
49+
cls.signers = []
4950
for _ in range(10):
5051
key, signer = RepositorySimulator.create_key()
5152
cls.keys.append(key)
@@ -267,7 +268,7 @@ def test_non_root_rotations(self, md_version: MdVersion) -> None:
267268
self._run_refresh()
268269

269270
# Call fetch_metadata to sign metadata with new keys
270-
expected_local_md: Metadata = self.sim._fetch_metadata(role)
271+
expected_local_md: bytes = self.sim._fetch_metadata(role)
271272
# assert local metadata role is on disk as expected
272273
md_path = os.path.join(self.metadata_dir, f"{role}.json")
273274
with open(md_path, "rb") as f:

0 commit comments

Comments
 (0)