From 5e91f9926e171017f34e1e034b35234d588034b3 Mon Sep 17 00:00:00 2001 From: Graham Lord Date: Tue, 24 Nov 2026 09:32:59 -0600 Subject: [PATCH 1/2] Accept avatar image objects in profile attributes Spotify's profileAttributes query now returns avatar as {"sources": [{"url", "width", "height"}]} instead of a URL string, which made Profile.from_dict raise and broke every playlist mutation (create_playlist resolves the username through this query). Reduce the object form to its largest source URL so avatar stays a str. Co-Authored-By: Claude Opus 5.5 --- .../models/catalogue/profile_attributes.py | 33 ++++++++++++- tests/test_review_regressions.py | 47 +++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/spotapi/models/catalogue/profile_attributes.py b/spotapi/models/catalogue/profile_attributes.py index fcaacb7..7f761c5 100644 --- a/spotapi/models/catalogue/profile_attributes.py +++ b/spotapi/models/catalogue/profile_attributes.py @@ -4,7 +4,7 @@ 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 @@ -12,6 +12,28 @@ __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.""" @@ -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: diff --git a/tests/test_review_regressions.py b/tests/test_review_regressions.py index 044ccc6..059ba8c 100644 --- a/tests/test_review_regressions.py +++ b/tests/test_review_regressions.py @@ -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"): From 32567b6f2ad05b27f1a2f427f2a219b1956bac7c Mon Sep 17 00:00:00 2001 From: Graham Lord Date: Tue, 24 Nov 2026 09:39:22 -0600 Subject: [PATCH 2/2] Add changelog entry for object-shaped profile avatars Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fcfdfc6..1cbf8df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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