Skip to content

Commit 16f42fc

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 <tsechkova@vmware.com>
1 parent 9daccac commit 16f42fc

File tree

3 files changed

+29
-48
lines changed

3 files changed

+29
-48
lines changed

tuf/ngclient/_internal/download.py

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,37 @@
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+
"""Handles 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 <vladimir.v.diaz@gmail.com>
16-
17-
<Copyright>
18-
See LICENSE-MIT OR LICENSE for licensing information.
196

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
3114

3215
logger = logging.getLogger(__name__)
3316

34-
@contextmanager
35-
def download_file(url, required_length, fetcher):
36-
"""
37-
<Purpose>
38-
Given the url and length of the desired file, this function opens a
39-
connection to 'url' and downloads the file up to 'required_length'.
4017

41-
<Arguments>
42-
url:
43-
A URL string that represents the location of the file.
44-
45-
required_length:
46-
An integer value representing the length of the file or an
47-
upper boundary.
48-
49-
<Side Effects>
50-
A file object is created on disk to store the contents of 'url'.
51-
52-
<Exceptions>
53-
exceptions.DownloadLengthMismatchError, if there was a
54-
mismatch of observed vs expected lengths while downloading the file.
55-
56-
Any other unforeseen runtime exception.
57-
58-
<Returns>
18+
@contextmanager
19+
def download_file(
20+
url: str, required_length: int, fetcher: "FetcherInterface"
21+
) -> Iterator[IO]:
22+
"""Opens a connection to 'url' and downloads the content
23+
up to 'required_length'.
24+
25+
Args:
26+
url: a URL string that represents the location of the file.
27+
required_length: an integer value representing the length of
28+
the file or an upper boundary.
29+
30+
Raises:
31+
DownloadLengthMismatchError: a mismatch of observed vs expected
32+
lengths while downloading the file.
33+
34+
Returns:
5935
A file object that points to the contents of 'url'.
6036
"""
6137
# 'url.replace('\\', '/')' is needed for compatibility with Windows-based
@@ -82,7 +58,9 @@ def download_file(url, required_length, fetcher):
8258
yield temp_file
8359

8460

85-
def download_bytes(url, required_length, fetcher):
61+
def download_bytes(
62+
url: str, required_length: int, fetcher: "FetcherInterface"
63+
) -> bytes:
8664
"""Download bytes from given url
8765
8866
Returns the downloaded bytes, otherwise like download_file()

tuf/ngclient/_internal/requests_fetcher.py

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

88
import logging
99
import time
10-
from typing import Optional
10+
from typing import Iterator, Optional
1111
from urllib import parse
1212

1313
# Imports
@@ -53,7 +53,7 @@ def __init__(self):
5353
self.chunk_size: int = 400000 # bytes
5454
self.sleep_before_round: Optional[int] = None
5555

56-
def fetch(self, url, required_length):
56+
def fetch(self, url: str, required_length: int) -> Iterator[bytes]:
5757
"""Fetches the contents of HTTP/HTTPS url from a remote server.
5858
5959
Ensures the length of the downloaded data is up to 'required_length'.
@@ -92,7 +92,9 @@ def fetch(self, url, required_length):
9292

9393
return self._chunks(response, required_length)
9494

95-
def _chunks(self, response, required_length):
95+
def _chunks(
96+
self, response: "requests.Response", required_length: int
97+
) -> Iterator[bytes]:
9698
"""A generator function to be returned by fetch. This way the
9799
caller of fetch can differentiate between connection and actual data
98100
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)