Skip to content

Commit 7e288a0

Browse files
committed
Add type annotations
Add missing type annotations in download.py, fetcher.py and requests_fetcher.py Update docstrings to match the new style. Signed-off-by: Teodora Sechkova <[email protected]>
1 parent af6fbd6 commit 7e288a0

File tree

3 files changed

+13
-27
lines changed

3 files changed

+13
-27
lines changed

tuf/ngclient/_internal/download.py

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,13 @@
1-
#!/usr/bin/env python
2-
31
# Copyright 2012 - 2017, New York University and the TUF contributors
42
# SPDX-License-Identifier: MIT OR Apache-2.0
53

4+
"""Provides a class handling the download of URL contents to a file"
65
"""
7-
<Program Name>
8-
download.py
9-
10-
<Started>
11-
February 21, 2012. Based on previous version by Geremy Condra.
12-
13-
<Author>
14-
Konstantin Andrianov
15-
Vladimir Diaz <[email protected]>
166

17-
<Copyright>
18-
See LICENSE-MIT OR LICENSE for licensing information.
19-
20-
<Purpose>
21-
Download metadata and target files and check their validity. The hash and
22-
length of a downloaded file has to match the hash and length supplied by the
23-
metadata of that file.
24-
"""
257
import logging
268
import tempfile
279
from contextlib import contextmanager
10+
from typing import IO, Iterator
2811
from urllib import parse
2912

3013
from tuf import exceptions
@@ -41,14 +24,14 @@ class FileDownloader:
4124
the network IO library.
4225
"""
4326

44-
def __init__(self, fetcher):
27+
def __init__(self, fetcher: "FetcherInterface"):
4528
if fetcher is None:
4629
fetcher = RequestsFetcher()
4730

4831
self._fetcher = fetcher
4932

5033
@contextmanager
51-
def download_file(self, url, required_length):
34+
def download_file(self, url: str, required_length: int) -> Iterator[IO]:
5235
"""Opens a connection to 'url' and downloads the content
5336
up to 'required_length'.
5437
@@ -74,8 +57,7 @@ def download_file(self, url, required_length):
7457
logger.debug("Downloading: %s", url)
7558

7659
number_of_bytes_received = 0
77-
# This is the temporary file that we will return to contain the
78-
# contents of the downloaded file.
60+
7961
with tempfile.TemporaryFile() as temp_file:
8062
chunks = self._fetcher.fetch(url, required_length)
8163
for chunk in chunks:
@@ -88,7 +70,7 @@ def download_file(self, url, required_length):
8870
temp_file.seek(0)
8971
yield temp_file
9072

91-
def download_bytes(self, url, required_length):
73+
def download_bytes(self, url: str, required_length: int) -> bytes:
9274
"""Download bytes from given url
9375
9476
Returns the downloaded bytes, otherwise like download_file()

tuf/ngclient/_internal/requests_fetcher.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import logging
99
import time
10+
from typing import Iterator
1011
from urllib import parse
1112

1213
# Imports
@@ -47,7 +48,7 @@ def __init__(self):
4748
# Some cookies may not be HTTP-safe.
4849
self._sessions = {}
4950

50-
def fetch(self, url, required_length):
51+
def fetch(self, url: str, required_length: int) -> Iterator[bytes]:
5152
"""Fetches the contents of HTTP/HTTPS url from a remote server.
5253
5354
Ensures the length of the downloaded data is up to 'required_length'.
@@ -89,7 +90,9 @@ def fetch(self, url, required_length):
8990
return self._chunks(response, required_length)
9091

9192
@staticmethod
92-
def _chunks(response, required_length):
93+
def _chunks(
94+
response: "requests.Response", required_length: int
95+
) -> Iterator[bytes]:
9396
"""A generator function to be returned by fetch. This way the
9497
caller of fetch can differentiate between connection and actual data
9598
download."""

tuf/ngclient/fetcher.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
# Imports
88
import abc
9+
from typing import Iterator
910

1011

1112
# Classes
@@ -20,7 +21,7 @@ class FetcherInterface:
2021
__metaclass__ = abc.ABCMeta
2122

2223
@abc.abstractmethod
23-
def fetch(self, url, required_length):
24+
def fetch(self, url: str, required_length: int) -> Iterator[bytes]:
2425
"""Fetches the contents of HTTP/HTTPS url from a remote server.
2526
2627
Ensures the length of the downloaded data is up to 'required_length'.

0 commit comments

Comments
 (0)