Skip to content

Commit 5b39c61

Browse files
committed
Type ytmusic search filter as a Literal
ytmusicapi 1.12.1 narrows the `filter` argument of `search()` from `str | None` to a `Literal[...]`, so passing our `str | None` value fails mypy. Add a `YTMSearchFilter` Literal alias for the filter values we use and apply it to the search helper and its caller. The alias is a subset of ytmusicapi's accepted values and remains assignable to the current 1.11.5 `str | None` signature, so the change is backward-compatible and can land independently of the bump.
1 parent 824fd36 commit 5b39c61

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

music_assistant/providers/ytmusic/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
from music_assistant.models.music_provider import MusicProvider
6464

6565
from .helpers import (
66+
YTMSearchFilter,
6667
add_remove_playlist_tracks,
6768
convert_to_netscape,
6869
determine_recommendation_icon,
@@ -241,7 +242,7 @@ async def search(
241242
:param limit: Number of items to return in the search (per type).
242243
"""
243244
parsed_results = SearchResults()
244-
ytm_filter = None
245+
ytm_filter: YTMSearchFilter | None = None
245246
if len(media_types) == 1:
246247
# YTM does not support multiple searchtypes, falls back to all if no type given
247248
if media_types[0] == MediaType.ARTIST:

music_assistant/providers/ytmusic/helpers.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
import asyncio
1010
from http.cookies import SimpleCookie
1111
from time import time
12-
from typing import Any
12+
from typing import Any, Literal
1313

1414
import ytmusicapi
1515
from ytmusicapi import LikeStatus
1616
from ytmusicapi.exceptions import YTMusicError
1717

1818
from music_assistant.providers.ytmusic.constants import YTMRecommendationIcons
1919

20+
# subset of ytmusicapi's accepted search filters that we use
21+
YTMSearchFilter = Literal["artists", "albums", "songs", "playlists"]
22+
2023

2124
async def get_artist(
2225
prov_artist_id: str, headers: dict[str, str], language: str = "en"
@@ -319,7 +322,7 @@ def _get_song_radio_tracks() -> dict[str, Any]:
319322

320323

321324
async def search(
322-
query: str, ytm_filter: str | None = None, limit: int = 20, language: str = "en"
325+
query: str, ytm_filter: YTMSearchFilter | None = None, limit: int = 20, language: str = "en"
323326
) -> list[dict[str, Any]]:
324327
"""Async wrapper around the ytmusicapi search function."""
325328

0 commit comments

Comments
 (0)