Skip to content

Commit

Permalink
Without pydantic
Browse files Browse the repository at this point in the history
  • Loading branch information
steinitzu committed Feb 5, 2024
1 parent 94644b5 commit 345add8
Show file tree
Hide file tree
Showing 32 changed files with 575 additions and 348 deletions.
21 changes: 10 additions & 11 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,12 @@
# Specify the target platform details in config, so your developers are
# free to run mypy on Windows, Linux, or macOS and get consistent
# results.
python_version=3.6
python_version=3.8
platform=linux

# flake8-mypy expects the two following for sensible formatting
show_column_numbers=True
show_error_context=False

# Follow imports but not show errors in other files
follow_imports=silent

# # since we're ignoring imports, writing .mypy_cache doesn't make any sense
# cache_dir=/dev/null

# suppress errors about unsatisfied imports
ignore_missing_imports=True

# allow untyped calls as a consequence of the options above
disallow_untyped_calls=False
Expand All @@ -25,7 +16,9 @@ disallow_untyped_calls=False
warn_return_any=False

# treat Optional per PEP 484
strict_optional=True
# Implicit optional allows for more flexible code, but can be a source of bugs.
# If you want to be strict, you can set this to False.
no_implicit_optional=False

# ensure all execution paths are returning
warn_no_return=True
Expand All @@ -39,3 +32,9 @@ warn_unused_ignores=False
# Flip them on if you feel adventurous.
disallow_untyped_defs=False
check_untyped_defs=True

[mypy-urlobject.*]
ignore_missing_imports = True

[mypy-snapshottest.*]
ignore_missing_imports = True
399 changes: 319 additions & 80 deletions poetry.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ classifiers = [
[tool.poetry.dependencies]
python = "^3.8"
urlobject = "^2"
pydantic = ">=0.23"
typing-extensions = "^4.9.0"

[tool.poetry.dev-dependencies]
pytest = "^7.0"
Expand All @@ -38,15 +38,16 @@ pytest-cov = "^4.0"
sphinx = "^1.8"
sphinx-autodoc-typehints = "^1.6"
sphinx_rtd_theme = "^0.4.3"
aiohttp = "^3.5"
aiohttp = "^3.9"
requests = "^2.21"
astor = "^0.7.1"
pytest-asyncio = "^0.21"
pytest-asyncio = "^0.23.4"
sphinxcontrib-asyncio = "^0.2.0"

[tool.poetry.group.dev.dependencies]
snapshottest = "^0.6.0"
pluggy = "^1.4.0"
types-requests = "^2.31.0.20240125"

[tool.black]
line-length = 79
Expand Down
9 changes: 7 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
[tool:pytest]
addopts=--cov spoffy --no-cov-on-fail
asyncio_mode = auto

[flake8]
max-line-length = 79
max-line-length = 120
builtins =
_
exclude = ./tests/mock/responses/
# exlucd test/mock/responses and tests/modules/snapshots
exclude = ./tests/mock/responses/, ./tests/modules/snapshots



2 changes: 1 addition & 1 deletion spoffy/client/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def access_token(self) -> Union[str, None]:
if self._access_token:
return self._access_token
elif self.token:
return self.token.access_token
return self.token["access_token"]
return None

@access_token.setter
Expand Down
5 changes: 4 additions & 1 deletion spoffy/io/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ async def request(self, request: Request) -> Response:
headers=request.headers,
) as resp:
response = Response(
request, resp.status, resp.headers, await resp.read()
request,
resp.status,
resp.headers, # type: ignore
await resp.read(),
)
response.raise_for_status()
return response
Expand Down
33 changes: 3 additions & 30 deletions spoffy/models/base.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,5 @@
from pydantic import BaseModel, BaseConfig, Extra
from typing_extensions import TypedDict


class Cfg(BaseConfig):
extra = Extra.allow


class CustomMeta(BaseModel.__class__): # type:ignore
def __new__(mcs, name, bases, namespace):
ret = super().__new__(mcs, name, bases, namespace)
for field in ret.__fields__.values():
field.required = False
field.prepare()
return ret


class SpotifyObject(BaseModel, metaclass=CustomMeta):
Config = Cfg

def __init__(self, **data) -> None:
super().__init__(**data)
for field in self.__fields_set__:
if field not in self.__fields__:
value = getattr(self, field)
if isinstance(value, dict):
setattr(self, field, SpotifyObject(**value))
elif (
value
and isinstance(value, list)
and isinstance(value[0], dict)
):
setattr(self, field, [SpotifyObject(**v) for v in value])
class SpotifyObject(TypedDict):
pass
28 changes: 12 additions & 16 deletions spoffy/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Restrictions(SpotifyObject):


class ExternalIds(SpotifyObject):
upc: Opt[str] = None
upc: Opt[str]


class TrackExternalIds(SpotifyObject):
Expand Down Expand Up @@ -69,9 +69,9 @@ class TrackSimple(SpotifyObject):
type: str
is_local: bool
uri: str
available_markets: Opt[List[str]] = None
linked_from: Opt[TrackLink] = None
is_playable: Opt[bool] = None
available_markets: Opt[List[str]]
linked_from: Opt[TrackLink]
is_playable: Opt[bool]


class AlbumBase(SpotifyObject):
Expand All @@ -90,22 +90,18 @@ class AlbumBase(SpotifyObject):


class AlbumSimple(AlbumBase):
restrictions: Opt[Restrictions] = None
available_markets: Opt[List[str]] = None
restrictions: Opt[Restrictions]
available_markets: Opt[List[str]]


class Track(TrackSimple):
explicit: bool
album: AlbumSimple
popularity: int
external_ids: TrackExternalIds
available_markets: Opt[List[str]] = None
linked_from: Opt[TrackLink] = None
is_playable: Opt[bool] = None


class AlbumTracksPaging(OffsetPaging):
items: List[TrackSimple]
class AlbumTracksPaging(OffsetPaging[TrackSimple]):
pass


class Album(AlbumBase):
Expand All @@ -115,12 +111,12 @@ class Album(AlbumBase):
label: str
popularity: int
tracks: AlbumTracksPaging
restrictions: Opt[Restrictions] = None
available_markets: Opt[List[str]] = None
restrictions: Opt[Restrictions]
available_markets: Opt[List[str]]


class AlbumSimplePaging(OffsetPaging):
items: List[AlbumSimple]
class AlbumSimplePaging(OffsetPaging[AlbumSimple]):
pass


class NewAlbumReleases(SpotifyObject):
Expand Down
4 changes: 2 additions & 2 deletions spoffy/models/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

class Image(SpotifyObject):
url: str
width: Opt[int] = None
height: Opt[int] = None
width: Opt[int]
height: Opt[int]
10 changes: 4 additions & 6 deletions spoffy/models/library.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import List

from spoffy.models.core import Track, Album
from spoffy.models.paging import OffsetPaging
from spoffy.models.base import SpotifyObject
Expand All @@ -10,14 +8,14 @@ class SavedTrack(SpotifyObject):
track: Track


class SavedTracksPaging(OffsetPaging):
items: List[SavedTrack]
class SavedTracksPaging(OffsetPaging[SavedTrack]):
pass


class SavedAlbum(SpotifyObject):
added_at: str
album: Album


class SavedAlbumsPaging(OffsetPaging):
items: List[SavedAlbum]
class SavedAlbumsPaging(OffsetPaging[SavedAlbum]):
pass
21 changes: 7 additions & 14 deletions spoffy/models/paging.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
from typing import Optional as Opt, List, Any
from typing import Optional as Opt, List, Generic, TypeVar

from spoffy.models.base import SpotifyObject

TPageItem = TypeVar("TPageItem")

class Paging(SpotifyObject):

class Paging(SpotifyObject, Generic[TPageItem]):
href: str
limit: int
next: Opt[str]
previous: Opt[str]
items: List[Any]
items: List[TPageItem]


class OffsetPaging(Paging):
class OffsetPaging(Paging[TPageItem]):
"""
Offset based paging object
Expand All @@ -23,21 +25,16 @@ class OffsetPaging(Paging):
:param previous: URL to previous page (if available)
"""

# Jedi doesn't support generics, do this for now
href: str
limit: int
offset: int
total: int
next: Opt[str]
previous: Opt[str]


class Cursor(SpotifyObject):
after: str
before: str


class CursorPaging(Paging):
class CursorPaging(Paging[TPageItem]):
"""
Cursor based paging object
Expand All @@ -48,8 +45,4 @@ class CursorPaging(Paging):
:param previous: URL to previous page (if available)
"""

href: str
limit: int
cursors: Cursor
next: Opt[str] = None
previous: Opt[str] = None
11 changes: 4 additions & 7 deletions spoffy/models/personalization.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
from typing import List


from spoffy.models.core import Track, Artist
from spoffy.models.paging import OffsetPaging


class TopArtistsPaging(OffsetPaging):
items: List[Artist]
class TopArtistsPaging(OffsetPaging[Artist]):
pass


class TopTracksPaging(OffsetPaging):
items: List[Track]
class TopTracksPaging(OffsetPaging[Track]):
pass
4 changes: 2 additions & 2 deletions spoffy/models/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ class PlayHistoryItem(SpotifyObject):
context: Opt[Context]


class PlayHistoryPaging(CursorPaging):
items: List[PlayHistoryItem]
class PlayHistoryPaging(CursorPaging[PlayHistoryItem]):
pass
17 changes: 7 additions & 10 deletions spoffy/models/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from spoffy.models.image import Image
from spoffy.models.users import UserBase
from spoffy.models.core import Track, TrackLink, ExternalUrls, Followers
from spoffy.models.core import Track, ExternalUrls, Followers
from spoffy.models.paging import OffsetPaging
from spoffy.models.base import SpotifyObject

Expand All @@ -24,9 +24,7 @@ class VideoThumbnail(SpotifyObject):


class PlaylistTrackTrack(Track):
available_markets: Opt[List[str]] = None
linked_from: Opt[TrackLink] = None
is_playable: Opt[bool] = None
pass


class PlaylistTrack(SpotifyObject):
Expand All @@ -36,8 +34,8 @@ class PlaylistTrack(SpotifyObject):
track: PlaylistTrackTrack


class PlaylistTracksPaging(OffsetPaging):
items: List[PlaylistTrack]
class PlaylistTracksPaging(OffsetPaging[PlaylistTrack]):
pass


class PlaylistBase(SpotifyObject):
Expand Down Expand Up @@ -80,8 +78,7 @@ class PlaylistBase(SpotifyObject):


class TracksHref(SpotifyObject):
"""
"""
""" """

href: str
total: int
Expand Down Expand Up @@ -118,8 +115,8 @@ class Playlist(PlaylistBase):
tracks: PlaylistTracksPaging


class PlaylistSimplePaging(OffsetPaging):
items: List[PlaylistSimple]
class PlaylistSimplePaging(OffsetPaging[PlaylistSimple]):
pass


Playlist.__doc__ += PlaylistBase.__doc__ # type: ignore
Loading

0 comments on commit 345add8

Please sign in to comment.