From 68a539920355ef739f9fd40d6b1962163c206776 Mon Sep 17 00:00:00 2001 From: Jack Dyre <83248599+JackDyre@users.noreply.github.com> Date: Fri, 9 Aug 2024 17:28:49 -0500 Subject: [PATCH] Switched to typed dict for cache type --- pyproject.toml | 1 + requirements-dev.lock | 3 +++ spotipy/cache_handler.py | 47 ++++++++++++++++++++++++---------------- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index dc9951dd..e46d474c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,5 +31,6 @@ dev-dependencies = [ "flake8>=7.1.1", "isort>=5.13.2", "autopep8>=2.3.1", + "pyupgrade>=3.16.0", ] diff --git a/requirements-dev.lock b/requirements-dev.lock index 9e93dc80..5fdc22d7 100644 --- a/requirements-dev.lock +++ b/requirements-dev.lock @@ -42,10 +42,13 @@ pyflakes==3.2.0 pylint==3.2.6 pymemcache==4.0.0 # via spotipy +pyupgrade==3.16.0 redis==5.0.8 # via spotipy requests==2.32.3 # via spotipy +tokenize-rt==6.0.0 + # via pyupgrade tomli==2.0.1 # via autopep8 # via mypy diff --git a/spotipy/cache_handler.py b/spotipy/cache_handler.py index 5f268029..49b7647c 100644 --- a/spotipy/cache_handler.py +++ b/spotipy/cache_handler.py @@ -1,12 +1,4 @@ -__all__ = [ - "CacheHandler", - "CacheFileHandler", - "DjangoSessionCacheHandler", - "FlaskSessionCacheHandler", - "MemoryCacheHandler", - "RedisCacheHandler", - "MemcacheCacheHandler", -] +from __future__ import annotations import errno import json @@ -15,15 +7,32 @@ from abc import ABC, abstractmethod from json import JSONEncoder from pathlib import Path -from typing import Dict, Optional, Type, Union +from typing import TypedDict from redis import RedisError from .util import CLIENT_CREDS_ENV_VARS +__all__ = [ + "CacheHandler", + "CacheFileHandler", + "DjangoSessionCacheHandler", + "FlaskSessionCacheHandler", + "MemoryCacheHandler", + "RedisCacheHandler", + "MemcacheCacheHandler", +] + logger = logging.getLogger(__name__) -TokenInfoType = Dict[str, Union[str, int]] + +class TokenInfo(TypedDict): + access_token: str + token_type: str + scope: str + expires_in: int + refresh_token: str + expires_at: int class CacheHandler(ABC): @@ -37,11 +46,11 @@ class CacheHandler(ABC): """ @abstractmethod - def get_cached_token(self) -> Optional[TokenInfoType]: + def get_cached_token(self) -> TokenInfo | None: """Get and return a token_info dictionary object.""" @abstractmethod - def save_token_to_cache(self, token_info: TokenInfoType) -> None: + def save_token_to_cache(self, token_info: TokenInfo) -> None: """Save a token_info dictionary object to the cache and return None.""" @@ -50,9 +59,9 @@ class CacheFileHandler(CacheHandler): def __init__( self, - cache_path: Optional[str] = None, - username: Optional[str] = None, - encoder_cls: Optional[Type[JSONEncoder]] = None, + cache_path: str | None = None, + username: str | None = None, + encoder_cls: type[JSONEncoder] | None = None, ) -> None: """ Initialize CacheFileHandler instance. @@ -71,9 +80,9 @@ def __init__( cache_path += "-" + str(username) self.cache_path = cache_path - def get_cached_token(self) -> Optional[TokenInfoType]: + def get_cached_token(self) -> TokenInfo | None: """Get cached token from file.""" - token_info: Optional[TokenInfoType] = None + token_info: TokenInfo | None = None try: with Path(self.cache_path).open("r") as f: @@ -87,7 +96,7 @@ def get_cached_token(self) -> Optional[TokenInfoType]: return token_info - def save_token_to_cache(self, token_info: TokenInfoType) -> None: + def save_token_to_cache(self, token_info: TokenInfo) -> None: """Save token cache to file.""" try: with Path(self.cache_path).open("w") as f: