From 50617a0f57d33579cf4ff8f11c705645b9766a2f Mon Sep 17 00:00:00 2001 From: yankeexe Date: Thu, 15 Apr 2021 18:42:05 +0545 Subject: [PATCH] Update: type hints for functions --- deta/__init__.py | 19 +++++++++++++++---- deta/base.py | 20 +++++++++++++++----- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/deta/__init__.py b/deta/__init__.py index 765fe98..42a6cb9 100644 --- a/deta/__init__.py +++ b/deta/__init__.py @@ -1,7 +1,7 @@ import os +import typing import urllib.error import urllib.request - from .base import Base try: @@ -18,20 +18,31 @@ class Deta: - def __init__(self, project_key: str = None, *, project_id: str = None, host: str = None): + def __init__( + self, + project_key: typing.Optional[str] = None, + *, + project_id: typing.Optional[str] = None, + host: typing.Optional[str] = None, + ): self.project_key = project_key or os.getenv("DETA_PROJECT_KEY") self.project_id = project_id if not self.project_id: self.project_id = self.project_key.split("_")[0] - def Base(self, name: str, host: str = None): + def Base(self, name: str, host: typing.Optional[str] = None): return Base(name, self.project_key, self.project_id, host) def send_email(self, to, subject, message, charset="UTF-8"): return send_email(to, subject, message, charset) -def send_email(to, subject, message, charset="UTF-8"): +def send_email( + to: typing.Union[str, typing.List[str]], + subject: str, + message: str, + charset: str = "UTF-8", +): pid = os.getenv("AWS_LAMBDA_FUNCTION_NAME") url = os.getenv("DETA_MAILER_URL") api_key = os.getenv("DETA_PROJECT_KEY") diff --git a/deta/base.py b/deta/base.py index 109081a..b488b0f 100644 --- a/deta/base.py +++ b/deta/base.py @@ -72,7 +72,9 @@ def _is_socket_closed(self): return True return False - def _request(self, path: str, method: str, data: dict = None): + def _request( + self, path: str, method: str, data: typing.Optional[dict] = None + ) -> typing.Tuple[int, typing.Mapping]: url = self.base_path + path # close connection if socket is closed @@ -93,7 +95,7 @@ def _request(self, path: str, method: str, data: dict = None): return status, json.loads(payload) if status != 404 else None raise urllib.error.HTTPError(url, status, res.reason, res.headers, res.fp) - def get(self, key: str) -> dict: + def get(self, key: str) -> typing.Mapping: if key == "": raise ValueError("Key is empty") @@ -114,7 +116,11 @@ def delete(self, key: str) -> bool: _, _ = self._request("/items/{}".format(key), "DELETE") return None - def insert(self, data: typing.Union[dict, list, str, int, bool], key: str = None): + def insert( + self, + data: typing.Union[dict, list, str, int, bool], + key: typing.Optional[str] = None, + ): if not isinstance(data, dict): data = {"value": data} else: @@ -129,7 +135,11 @@ def insert(self, data: typing.Union[dict, list, str, int, bool], key: str = None elif code == 409: raise Exception("Item with key '{4}' already exists".format(key)) - def put(self, data: typing.Union[dict, list, str, int, bool], key: str = None): + def put( + self, + data: typing.Union[dict, list, str, int, bool], + key: typing.Optional[str] = None, + ): """store (put) an item in the database. Overrides an item if key already exists. `key` could be provided as function argument or a field in the data dict. If `key` is not provided, the server will generate a random 12 chars key. @@ -163,7 +173,7 @@ def _fetch( query: typing.Union[dict, list] = None, buffer: int = None, last: str = None, - ) -> typing.Tuple[int, list]: + ) -> typing.Tuple[int, typing.Mapping]: """This is where actual fetch happens.""" payload = { "limit": buffer,