Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type annotations for Spotify JSON objects #695

Draft
wants to merge 12 commits into
base: v3
Choose a base branch
from
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
author_email="[email protected]",
url='https://spotipy.readthedocs.org/',
install_requires=[
'typing_extensions>=3.10.0',
'requests>=2.25.0',
'six>=1.15.0',
'urllib3>=1.26.0'
Expand Down
23 changes: 13 additions & 10 deletions spotipy/cache_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import json
import logging
import os
from spotipy.util import CLIENT_CREDS_ENV_VARS
from abc import ABC, abstractmethod
from typing import Optional

from spotipy.json_types import TokenInfo
from spotipy.util import CLIENT_CREDS_ENV_VARS

logger = logging.getLogger(__name__)

Expand All @@ -23,13 +26,13 @@ class CacheHandler(ABC):
"""

@abstractmethod
def get_cached_token(self):
def get_cached_token(self) -> Optional[TokenInfo]:
"""
Get and return a token_info dictionary object.
"""

@abstractmethod
def save_token_to_cache(self, token_info):
def save_token_to_cache(self, token_info: TokenInfo):
"""
Save a token_info dictionary object to the cache and return None.
"""
Expand All @@ -42,8 +45,8 @@ class CacheFileHandler(CacheHandler):
"""

def __init__(self,
cache_path=None,
username=None):
cache_path: Optional[str] = None,
username: Optional[str] = None):
"""
Parameters:
* cache_path: May be supplied, will otherwise be generated
Expand All @@ -61,7 +64,7 @@ def __init__(self,
cache_path += "-" + str(username)
self.cache_path = cache_path

def get_cached_token(self):
def get_cached_token(self) -> Optional[TokenInfo]:
token_info = None

try:
Expand All @@ -78,7 +81,7 @@ def get_cached_token(self):

return token_info

def save_token_to_cache(self, token_info):
def save_token_to_cache(self, token_info: TokenInfo):
try:
f = open(self.cache_path, "w")
f.write(json.dumps(token_info))
Expand All @@ -95,15 +98,15 @@ class MemoryCacheHandler(CacheHandler):
instance is freed.
"""

def __init__(self, token_info=None):
def __init__(self, token_info: Optional[TokenInfo] = None):
"""
Parameters:
* token_info: The token info to store in memory. Can be None.
"""
self.token_info = token_info

def get_cached_token(self):
def get_cached_token(self) -> Optional[TokenInfo]:
return self.token_info

def save_token_to_cache(self, token_info):
def save_token_to_cache(self, token_info: TokenInfo):
self.token_info = token_info
Loading