Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

- Accept the object-shaped profile `avatar` (`{"sources": [...]}`) that Spotify now
returns, reducing it to the largest image URL. Previously it raised and broke every
playlist mutation that resolves the username (for example `create_playlist`).

## 2.1.3 - 2026-11-22

- Decode object-shaped album pre-release timestamps as typed dates without breaking
Expand Down
33 changes: 31 additions & 2 deletions spotapi/models/catalogue/profile_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,36 @@

from collections.abc import Mapping
from dataclasses import dataclass
from typing import Any
from typing import Any, cast

from spotapi._decoding import from_dict
from spotapi.types import BaseClientError

__all__ = ("Profile",)


def _avatar_url(value: object) -> object:
"""Return the largest image URL from an avatar object; pass other values through."""
if not isinstance(value, Mapping):
return value
sources: object = cast(Mapping[str, object], value).get("sources")
if not isinstance(sources, list):
return None
best: str | None = None
best_area = -1
for source in cast(list[object], sources):
if not isinstance(source, Mapping):
continue
entry = cast(Mapping[str, object], source)
url, width, height = entry.get("url"), entry.get("width"), entry.get("height")
if not isinstance(url, str) or not url:
continue
area = width * height if isinstance(width, int) and isinstance(height, int) else 0
if area > best_area:
best, best_area = url, area
return best


@dataclass(slots=True)
class Profile:
"""Authenticated profile attributes with Python field names."""
Expand All @@ -26,7 +48,14 @@ class Profile:

@classmethod
def from_dict(cls, data: Mapping[str, Any]) -> Profile:
"""Validate required profile identity and optional native response fields."""
"""Validate required profile identity and optional native response fields.

Spotify sends ``avatar`` either as a URL string or as an image object
(``{"sources": [{"url", "width", "height"}, ...]}``). The object form is
reduced to the URL of its largest source so ``avatar`` stays a string.
"""
if "avatar" in data:
data = {**data, "avatar": _avatar_url(data["avatar"])}
try:
return from_dict(cls, data)
except (TypeError, ValueError) as error:
Expand Down
47 changes: 47 additions & 0 deletions tests/test_review_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,53 @@ def test_profile_attributes_accept_native_keys_and_missing_optional_fields() ->
assert profile.avatar is None and profile.social_handle is None


def test_profile_attributes_accept_avatar_image_object() -> None:
profile = Profile.from_dict(
{
"accountId": "account",
"name": "Listener",
"username": "listener",
"uri": "spotify:user:listener",
"avatar": {
"sources": [
{"url": "https://i.scdn.co/small", "width": 64, "height": 64},
{"url": "https://i.scdn.co/large", "width": 300, "height": 300},
]
},
"avatarBackgroundColor": 123,
}
)
assert profile.avatar == "https://i.scdn.co/large"
assert profile.avatar_background_color == 123


@pytest.mark.parametrize("avatar", [{}, {"sources": []}, {"sources": [{"url": ""}]}])
def test_profile_attributes_treat_empty_avatar_object_as_missing(avatar: object) -> None:
profile = Profile.from_dict(
{
"accountId": "account",
"name": "Listener",
"username": "listener",
"uri": "spotify:user:listener",
"avatar": avatar,
}
)
assert profile.avatar is None


def test_profile_attributes_keep_string_avatar() -> None:
profile = Profile.from_dict(
{
"accountId": "account",
"name": "Listener",
"username": "listener",
"uri": "spotify:user:listener",
"avatar": "https://i.scdn.co/plain",
}
)
assert profile.avatar == "https://i.scdn.co/plain"


@pytest.mark.parametrize("value", [None, 123, False])
def test_profile_attributes_reject_invalid_required_names(value: object) -> None:
with pytest.raises(BaseClientError, match="profile attributes"):
Expand Down