Skip to content

Commit

Permalink
feat: use annotations from __future__
Browse files Browse the repository at this point in the history
  • Loading branch information
leynier committed Nov 1, 2021
1 parent 55c03a6 commit b16356d
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 62 deletions.
26 changes: 14 additions & 12 deletions gotrue/_async/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Any, Dict, Optional, Union
from __future__ import annotations

from typing import Any, Optional, Union

from ..helpers import encode_uri_component, parse_response, parse_session_or_user
from ..http_clients import AsyncClient
Expand All @@ -10,7 +12,7 @@ def __init__(
self,
*,
url: str,
headers: Dict[str, str],
headers: dict[str, str],
cookie_options: CookieOptions,
) -> None:
"""Initialise API class."""
Expand All @@ -19,7 +21,7 @@ def __init__(
self.cookie_options = cookie_options
self.http_client = AsyncClient()

async def __aenter__(self) -> "AsyncGoTrueApi":
async def __aenter__(self) -> AsyncGoTrueApi:
return self

async def __aexit__(self, exc_t, exc_v, exc_tb) -> None:
Expand All @@ -34,7 +36,7 @@ async def sign_up_with_email(
email: str,
password: str,
redirect_to: Optional[str] = None,
data: Optional[Dict[str, Any]] = None,
data: Optional[dict[str, Any]] = None,
) -> Union[Session, User]:
"""Creates a new user using their email address.
Expand All @@ -46,7 +48,7 @@ async def sign_up_with_email(
The password of the user.
redirect_to : Optional[str]
A URL or mobile address to send the user to after they are confirmed.
data : Optional[Dict[str, Any]]
data : Optional[dict[str, Any]]
Optional user metadata.
Returns
Expand Down Expand Up @@ -113,7 +115,7 @@ async def sign_up_with_phone(
*,
phone: str,
password: str,
data: Optional[Dict[str, Any]] = None,
data: Optional[dict[str, Any]] = None,
) -> Union[Session, User]:
"""Signs up a new user using their phone number and a password.
Expand All @@ -123,7 +125,7 @@ async def sign_up_with_phone(
The phone number of the user.
password : str
The password of the user.
data : Optional[Dict[str, Any]]
data : Optional[dict[str, Any]]
Optional user metadata.
Returns
Expand Down Expand Up @@ -271,7 +273,7 @@ async def invite_user_by_email(
*,
email: str,
redirect_to: Optional[str] = None,
data: Optional[Dict[str, Any]] = None,
data: Optional[dict[str, Any]] = None,
) -> User:
"""Sends an invite link to an email address.
Expand All @@ -281,7 +283,7 @@ async def invite_user_by_email(
The email address of the user.
redirect_to : Optional[str]
A URL or mobile address to send the user to after they are confirmed.
data : Optional[Dict[str, Any]]
data : Optional[dict[str, Any]]
Optional user metadata.
Returns
Expand Down Expand Up @@ -334,7 +336,7 @@ async def reset_password_for_email(
response = await self.http_client.post(url, json=data, headers=headers)
return parse_response(response, lambda _: None)

def _create_request_headers(self, *, jwt: str) -> Dict[str, str]:
def _create_request_headers(self, *, jwt: str) -> dict[str, str]:
"""Create temporary object.
Create a temporary object with all configured headers and adds the
Expand Down Expand Up @@ -518,7 +520,7 @@ async def generate_link(
email: str,
password: Optional[str] = None,
redirect_to: Optional[str] = None,
data: Optional[Dict[str, Any]] = None,
data: Optional[dict[str, Any]] = None,
) -> Union[Session, User]:
"""
Generates links to be sent via email or other.
Expand All @@ -533,7 +535,7 @@ async def generate_link(
User password. For signup only.
redirect_to : Optional[str]
The link type ("signup" or "magiclink" or "recovery" or "invite").
data : Optional[Dict[str, Any]]
data : Optional[dict[str, Any]]
Optional user metadata. For signup only.
Returns
Expand Down
16 changes: 9 additions & 7 deletions gotrue/_async/client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import annotations

from functools import partial
from json import dumps, loads
from threading import Timer
from time import time
from typing import Any, Callable, Dict, Optional, Union, cast
from typing import Any, Callable, Optional, Union, cast
from urllib.parse import parse_qs, urlparse
from uuid import uuid4

Expand All @@ -26,7 +28,7 @@ def __init__(
self,
*,
url: str = GOTRUE_URL,
headers: Dict[str, str] = DEFAULT_HEADERS,
headers: dict[str, str] = DEFAULT_HEADERS,
auto_refresh_token: bool = True,
persist_session: bool = True,
local_storage: AsyncSupportedStorage = AsyncMemoryStorage(),
Expand All @@ -36,7 +38,7 @@ def __init__(
url : str
The URL of the GoTrue server.
headers : Dict[str, str]
headers : dict[str, str]
Any additional headers to send to the GoTrue server.
auto_refresh_token : bool
Set to "true" if you want to automatically refresh the token before
Expand All @@ -54,7 +56,7 @@ def __init__(
"Warning:\n\nDO NOT USE HTTP IN PRODUCTION FOR GOTRUE EVER!\n"
"GoTrue REQUIRES HTTPS to work securely."
)
self.state_change_emitters: Dict[str, Subscription] = {}
self.state_change_emitters: dict[str, Subscription] = {}
self.refresh_token_timer: Optional[Timer] = None
self.current_user: Optional[User] = None
self.current_session: Optional[Session] = None
Expand All @@ -67,7 +69,7 @@ def __init__(
cookie_options=cookie_options,
)

async def __aenter__(self) -> "AsyncGoTrueClient":
async def __aenter__(self) -> AsyncGoTrueClient:
return self

async def __aexit__(self, exc_t, exc_v, exc_tb) -> None:
Expand All @@ -88,7 +90,7 @@ async def sign_up(
phone: Optional[str] = None,
password: Optional[str] = None,
redirect_to: Optional[str] = None,
data: Optional[Dict[str, Any]] = None,
data: Optional[dict[str, Any]] = None,
) -> Union[Session, User]:
"""Creates a new user.
Expand All @@ -102,7 +104,7 @@ async def sign_up(
The user's password.
redirect_to : Optional[str]
A URL or mobile address to send the user to after they are confirmed.
data : Optional[Dict[str, Any]]
data : Optional[dict[str, Any]]
Optional user metadata.
Returns
Expand Down
6 changes: 4 additions & 2 deletions gotrue/_async/storage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import Dict, Optional
from typing import Optional


class AsyncSupportedStorage(ABC):
Expand All @@ -18,7 +20,7 @@ async def remove_item(self, key: str) -> None:

class AsyncMemoryStorage(AsyncSupportedStorage):
def __init__(self):
self.storage: Dict[str, str] = {}
self.storage: dict[str, str] = {}

async def get_item(self, key: str) -> Optional[str]:
if key in self.storage:
Expand Down
26 changes: 14 additions & 12 deletions gotrue/_sync/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Any, Dict, Optional, Union
from __future__ import annotations

from typing import Any, Optional, Union

from ..helpers import encode_uri_component, parse_response, parse_session_or_user
from ..http_clients import SyncClient
Expand All @@ -10,7 +12,7 @@ def __init__(
self,
*,
url: str,
headers: Dict[str, str],
headers: dict[str, str],
cookie_options: CookieOptions,
) -> None:
"""Initialise API class."""
Expand All @@ -19,7 +21,7 @@ def __init__(
self.cookie_options = cookie_options
self.http_client = SyncClient()

def __enter__(self) -> "SyncGoTrueApi":
def __enter__(self) -> SyncGoTrueApi:
return self

def __exit__(self, exc_t, exc_v, exc_tb) -> None:
Expand All @@ -34,7 +36,7 @@ def sign_up_with_email(
email: str,
password: str,
redirect_to: Optional[str] = None,
data: Optional[Dict[str, Any]] = None,
data: Optional[dict[str, Any]] = None,
) -> Union[Session, User]:
"""Creates a new user using their email address.
Expand All @@ -46,7 +48,7 @@ def sign_up_with_email(
The password of the user.
redirect_to : Optional[str]
A URL or mobile address to send the user to after they are confirmed.
data : Optional[Dict[str, Any]]
data : Optional[dict[str, Any]]
Optional user metadata.
Returns
Expand Down Expand Up @@ -113,7 +115,7 @@ def sign_up_with_phone(
*,
phone: str,
password: str,
data: Optional[Dict[str, Any]] = None,
data: Optional[dict[str, Any]] = None,
) -> Union[Session, User]:
"""Signs up a new user using their phone number and a password.
Expand All @@ -123,7 +125,7 @@ def sign_up_with_phone(
The phone number of the user.
password : str
The password of the user.
data : Optional[Dict[str, Any]]
data : Optional[dict[str, Any]]
Optional user metadata.
Returns
Expand Down Expand Up @@ -271,7 +273,7 @@ def invite_user_by_email(
*,
email: str,
redirect_to: Optional[str] = None,
data: Optional[Dict[str, Any]] = None,
data: Optional[dict[str, Any]] = None,
) -> User:
"""Sends an invite link to an email address.
Expand All @@ -281,7 +283,7 @@ def invite_user_by_email(
The email address of the user.
redirect_to : Optional[str]
A URL or mobile address to send the user to after they are confirmed.
data : Optional[Dict[str, Any]]
data : Optional[dict[str, Any]]
Optional user metadata.
Returns
Expand Down Expand Up @@ -334,7 +336,7 @@ def reset_password_for_email(
response = self.http_client.post(url, json=data, headers=headers)
return parse_response(response, lambda _: None)

def _create_request_headers(self, *, jwt: str) -> Dict[str, str]:
def _create_request_headers(self, *, jwt: str) -> dict[str, str]:
"""Create temporary object.
Create a temporary object with all configured headers and adds the
Expand Down Expand Up @@ -518,7 +520,7 @@ def generate_link(
email: str,
password: Optional[str] = None,
redirect_to: Optional[str] = None,
data: Optional[Dict[str, Any]] = None,
data: Optional[dict[str, Any]] = None,
) -> Union[Session, User]:
"""
Generates links to be sent via email or other.
Expand All @@ -533,7 +535,7 @@ def generate_link(
User password. For signup only.
redirect_to : Optional[str]
The link type ("signup" or "magiclink" or "recovery" or "invite").
data : Optional[Dict[str, Any]]
data : Optional[dict[str, Any]]
Optional user metadata. For signup only.
Returns
Expand Down
16 changes: 9 additions & 7 deletions gotrue/_sync/client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import annotations

from functools import partial
from json import dumps, loads
from threading import Timer
from time import time
from typing import Any, Callable, Dict, Optional, Union, cast
from typing import Any, Callable, Optional, Union, cast
from urllib.parse import parse_qs, urlparse
from uuid import uuid4

Expand All @@ -26,7 +28,7 @@ def __init__(
self,
*,
url: str = GOTRUE_URL,
headers: Dict[str, str] = DEFAULT_HEADERS,
headers: dict[str, str] = DEFAULT_HEADERS,
auto_refresh_token: bool = True,
persist_session: bool = True,
local_storage: SyncSupportedStorage = SyncMemoryStorage(),
Expand All @@ -36,7 +38,7 @@ def __init__(
url : str
The URL of the GoTrue server.
headers : Dict[str, str]
headers : dict[str, str]
Any additional headers to send to the GoTrue server.
auto_refresh_token : bool
Set to "true" if you want to automatically refresh the token before
Expand All @@ -54,7 +56,7 @@ def __init__(
"Warning:\n\nDO NOT USE HTTP IN PRODUCTION FOR GOTRUE EVER!\n"
"GoTrue REQUIRES HTTPS to work securely."
)
self.state_change_emitters: Dict[str, Subscription] = {}
self.state_change_emitters: dict[str, Subscription] = {}
self.refresh_token_timer: Optional[Timer] = None
self.current_user: Optional[User] = None
self.current_session: Optional[Session] = None
Expand All @@ -67,7 +69,7 @@ def __init__(
cookie_options=cookie_options,
)

def __enter__(self) -> "SyncGoTrueClient":
def __enter__(self) -> SyncGoTrueClient:
return self

def __exit__(self, exc_t, exc_v, exc_tb) -> None:
Expand All @@ -88,7 +90,7 @@ def sign_up(
phone: Optional[str] = None,
password: Optional[str] = None,
redirect_to: Optional[str] = None,
data: Optional[Dict[str, Any]] = None,
data: Optional[dict[str, Any]] = None,
) -> Union[Session, User]:
"""Creates a new user.
Expand All @@ -102,7 +104,7 @@ def sign_up(
The user's password.
redirect_to : Optional[str]
A URL or mobile address to send the user to after they are confirmed.
data : Optional[Dict[str, Any]]
data : Optional[dict[str, Any]]
Optional user metadata.
Returns
Expand Down
6 changes: 4 additions & 2 deletions gotrue/_sync/storage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import Dict, Optional
from typing import Optional


class SyncSupportedStorage(ABC):
Expand All @@ -18,7 +20,7 @@ def remove_item(self, key: str) -> None:

class SyncMemoryStorage(SyncSupportedStorage):
def __init__(self):
self.storage: Dict[str, str] = {}
self.storage: dict[str, str] = {}

def get_item(self, key: str) -> Optional[str]:
if key in self.storage:
Expand Down

0 comments on commit b16356d

Please sign in to comment.