Skip to content

Commit 044296b

Browse files
committed
Stop linting tuf/exceptions.py with mypy
Stop linting tuf/exceptions.py with mypy as we are going to use tuf/api/exceptions.py for exceptions in the new code. Signed-off-by: Martin Vrachev <[email protected]>
1 parent ee49e9a commit 044296b

File tree

3 files changed

+12
-67
lines changed

3 files changed

+12
-67
lines changed

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ commands =
4949
# work, unfortunately each subdirectory has to be ignored explicitly.
5050
pylint -j 0 tuf --ignore=tuf/api,tuf/api/serialization,tuf/ngclient,tuf/ngclient/_internal
5151

52-
mypy {[testenv:lint]lint_dirs} tuf/exceptions.py
52+
mypy {[testenv:lint]lint_dirs}
5353

5454
bandit -r tuf
5555

tuf/api/exceptions.py

Lines changed: 9 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
there is a good reason not to, and provide that reason in those cases.
88
"""
99

10-
from typing import Optional
1110

1211
#### General errors ####
1312

@@ -39,29 +38,16 @@ class URLParsingError(Exception):
3938
cannot be isoalted."""
4039

4140

42-
class RepositoryError(Exception):
43-
"""An error with a repository's state, such as a missing file."""
44-
45-
4641
#### Repository errors ####
4742

4843

49-
class UnsignedMetadataError(RepositoryError):
50-
"""An error about metadata object with insufficient threshold of signatures.
51-
52-
Args:
53-
message: The error message
54-
"""
55-
56-
def __init__(self, message: str) -> None:
57-
super().__init__()
58-
self.exception_message = message
44+
class RepositoryError(Exception):
45+
"""An error with a repository's state, such as a missing file."""
5946

60-
def __str__(self) -> str:
61-
return self.exception_message
6247

63-
def __repr__(self) -> str:
64-
return self.__class__.__name__ + " : " + str(self)
48+
class UnsignedMetadataError(RepositoryError):
49+
"""An error about metadata object with insufficient threshold of
50+
signatures."""
6551

6652

6753
class BadVersionNumberError(RepositoryError):
@@ -92,17 +78,11 @@ def __init__(
9278

9379
def __str__(self) -> str:
9480
return (
95-
"Downloaded "
96-
+ repr(self.metadata_role)
97-
+ " is older ("
98-
+ repr(self.downloaded_version)
99-
+ ") than the version currently "
100-
"installed (" + repr(self.current_version) + ")."
81+
f"Downloaded {self.metadata_role} is older ("
82+
f"{self.downloaded_version}) than the version currently installed"
83+
f"({self.current_version})"
10184
)
10285

103-
def __repr__(self) -> str:
104-
return self.__class__.__name__ + " : " + str(self)
105-
10686

10787
#### Download Errors ####
10888

@@ -114,42 +94,6 @@ class DownloadError(Exception):
11494
class DownloadLengthMismatchError(DownloadError):
11595
"""Indicate that a mismatch of lengths was seen while downloading a file."""
11696

117-
def __init__(self, expected_length: int, observed_length: int) -> None:
118-
super().__init__()
119-
120-
self.expected_length = expected_length # bytes
121-
self.observed_length = observed_length # bytes
122-
123-
def __str__(self) -> str:
124-
return (
125-
"Observed length ("
126-
+ repr(self.observed_length)
127-
+ ") < expected length ("
128-
+ repr(self.expected_length)
129-
+ ")."
130-
)
131-
132-
def __repr__(self) -> str:
133-
return self.__class__.__name__ + " : " + str(self)
134-
13597

13698
class SlowRetrievalError(DownloadError):
137-
""" "Indicate that downloading a file took an unreasonably long time."""
138-
139-
def __init__(self, average_download_speed: Optional[int] = None) -> None:
140-
super().__init__()
141-
self.__average_download_speed = average_download_speed # bytes/second
142-
143-
def __str__(self) -> str:
144-
msg = "Download was too slow."
145-
if self.__average_download_speed is not None:
146-
msg = (
147-
"Download was too slow. Average speed: "
148-
+ repr(self.__average_download_speed)
149-
+ " bytes per second."
150-
)
151-
152-
return msg
153-
154-
def __repr__(self) -> str:
155-
return self.__class__.__name__ + " : " + str(self)
99+
"""Indicate that downloading a file took an unreasonably long time."""

tuf/ngclient/fetcher.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ def download_file(self, url: str, max_length: int) -> Iterator[IO]:
7171
number_of_bytes_received += len(chunk)
7272
if number_of_bytes_received > max_length:
7373
raise exceptions.DownloadLengthMismatchError(
74-
max_length, number_of_bytes_received
74+
f"Downloaded {number_of_bytes_received} bytes exceeding"
75+
f" the maximum allowed length of {max_length}"
7576
)
7677

7778
temp_file.write(chunk)

0 commit comments

Comments
 (0)