Skip to content

Commit

Permalink
Enhance all dict type annotations
Browse files Browse the repository at this point in the history
For each dict that we control properly annotate the type of key and values.

For JSON controlled by others (i.e. returned by API)... it's pretty
hard! For the moment just annotate all of them via a generic `dict[Any,
Any]'.

(We can define a custom JSON type annotation as documented by
<python/typing#182> but we will still have
several problems and at the end we will always return Any.)

All of this was pointed out via `mypy --strict .'.
  • Loading branch information
iamleot committed May 20, 2023
1 parent 9b351b9 commit 85d02c7
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions transferwee.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
will be shared via emails or link.
"""

from typing import List, Optional
from typing import Any, List, Optional, Union
import binascii
import functools
import hashlib
Expand Down Expand Up @@ -172,7 +172,7 @@ def download(url: str, file: str = "") -> None:
f.write(chunk)


def _file_name_and_size(file: str) -> dict:
def _file_name_and_size(file: str) -> dict[str, Union[int, str]]:
"""Given a file, prepare the "item_type", "name" and "size" dictionary.
Return a dictionary with "item_type", "name" and "size" keys.
Expand Down Expand Up @@ -223,7 +223,7 @@ def _prepare_email_upload(
sender: str,
recipients: List[str],
session: requests.Session,
) -> dict:
) -> dict[Any, Any]:
"""Given a list of filenames, message a sender and recipients prepare for
the email upload.
Expand All @@ -242,7 +242,9 @@ def _prepare_email_upload(
return r.json()


def _verify_email_upload(transfer_id: str, session: requests.Session) -> dict:
def _verify_email_upload(
transfer_id: str, session: requests.Session
) -> dict[Any, Any]:
"""Given a transfer_id, read the code from standard input.
Return the parsed JSON response.
Expand All @@ -265,7 +267,7 @@ def _prepare_link_upload(
display_name: str,
message: str,
session: requests.Session,
) -> dict:
) -> dict[Any, Any]:
"""Given a list of filenames and a message prepare for the link upload.
Return the parsed JSON response.
Expand All @@ -281,7 +283,9 @@ def _prepare_link_upload(
return r.json()


def _storm_preflight_item(file: str) -> dict:
def _storm_preflight_item(
file: str,
) -> dict[str, Union[List[dict[str, int]], str]]:
"""Given a file, prepare the item block dictionary.
Return a dictionary with "blocks", "item_type" and "path" keys.
Expand All @@ -296,7 +300,9 @@ def _storm_preflight_item(file: str) -> dict:
}


def _storm_preflight(authorization: str, filenames: List[str]) -> dict:
def _storm_preflight(
authorization: str, filenames: List[str]
) -> dict[Any, Any]:
"""Given an Authorization token and filenames do preflight for upload.
Return the parsed JSON response.
Expand Down Expand Up @@ -335,7 +341,7 @@ def _md5(file: str) -> str:
return h.hexdigest()


def _storm_prepare_item(file: str) -> dict:
def _storm_prepare_item(file: str) -> dict[str, Union[int, str]]:
"""Given a file, prepare the block for blocks dictionary.
Return a dictionary with "content_length" and "content_md5_hex" keys.
Expand All @@ -345,7 +351,7 @@ def _storm_prepare_item(file: str) -> dict:
return {"content_length": filesize, "content_md5_hex": _md5(file)}


def _storm_prepare(authorization: str, filenames: List[str]) -> dict:
def _storm_prepare(authorization: str, filenames: List[str]) -> dict[Any, Any]:
"""Given an Authorization token and filenames prepare for block uploads.
Return the parsed JSON response.
Expand Down Expand Up @@ -373,7 +379,9 @@ def _storm_prepare(authorization: str, filenames: List[str]) -> dict:
return r.json()


def _storm_finalize_item(file: str, block_id: str) -> dict:
def _storm_finalize_item(
file: str, block_id: str
) -> dict[str, Union[List[str], str]]:
"""Given a file and block_id prepare the item block dictionary.
Return a dictionary with "block_ids", "item_type" and "path" keys.
Expand All @@ -396,7 +404,7 @@ def _storm_finalize_item(file: str, block_id: str) -> dict:

def _storm_finalize(
authorization: str, filenames: List[str], block_ids: List[str]
) -> dict:
) -> dict[Any, Any]:
"""Given an Authorization token, filenames and block ids finalize upload.
Return the parsed JSON response.
Expand Down Expand Up @@ -469,7 +477,9 @@ def _storm_upload(url: str, file: str) -> None:
)


def _finalize_upload(transfer_id: str, session: requests.Session) -> dict:
def _finalize_upload(
transfer_id: str, session: requests.Session
) -> dict[Any, Any]:
"""Given a transfer_id finalize the upload.
Return the parsed JSON response.
Expand Down

0 comments on commit 85d02c7

Please sign in to comment.