From 63d71457aacb56c90ff89852fb201f74562ebec2 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Tue, 16 Aug 2022 16:31:09 +0200 Subject: [PATCH] Type BrowseMedia children as a covariant (#76869) --- homeassistant/components/apple_tv/media_player.py | 2 +- homeassistant/components/jellyfin/media_source.py | 10 +++++----- .../components/media_player/browse_media.py | 3 ++- homeassistant/components/media_source/models.py | 2 -- .../components/unifiprotect/media_source.py | 4 ++-- homeassistant/components/xbox/browse_media.py | 13 ++++++------- 6 files changed, 16 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/apple_tv/media_player.py b/homeassistant/components/apple_tv/media_player.py index 362a09fb5fc..3a495e053eb 100644 --- a/homeassistant/components/apple_tv/media_player.py +++ b/homeassistant/components/apple_tv/media_player.py @@ -422,7 +422,7 @@ class AppleTvMediaPlayer(AppleTVEntity, MediaPlayerEntity): return cur_item # Add app item if we have one - if self._app_list and cur_item.children: + if self._app_list and cur_item.children and isinstance(cur_item.children, list): cur_item.children.insert(0, build_app_list(self._app_list)) return cur_item diff --git a/homeassistant/components/jellyfin/media_source.py b/homeassistant/components/jellyfin/media_source.py index 8a09fd8d552..662c0f0040a 100644 --- a/homeassistant/components/jellyfin/media_source.py +++ b/homeassistant/components/jellyfin/media_source.py @@ -173,10 +173,10 @@ class JellyfinSource(MediaSource): if include_children: result.children_media_class = MEDIA_CLASS_ARTIST - result.children = await self._build_artists(library_id) # type: ignore[assignment] + result.children = await self._build_artists(library_id) if not result.children: result.children_media_class = MEDIA_CLASS_ALBUM - result.children = await self._build_albums(library_id) # type: ignore[assignment] + result.children = await self._build_albums(library_id) return result @@ -207,7 +207,7 @@ class JellyfinSource(MediaSource): if include_children: result.children_media_class = MEDIA_CLASS_ALBUM - result.children = await self._build_albums(artist_id) # type: ignore[assignment] + result.children = await self._build_albums(artist_id) return result @@ -238,7 +238,7 @@ class JellyfinSource(MediaSource): if include_children: result.children_media_class = MEDIA_CLASS_TRACK - result.children = await self._build_tracks(album_id) # type: ignore[assignment] + result.children = await self._build_tracks(album_id) return result @@ -293,7 +293,7 @@ class JellyfinSource(MediaSource): if include_children: result.children_media_class = MEDIA_CLASS_MOVIE - result.children = await self._build_movies(library_id) # type: ignore[assignment] + result.children = await self._build_movies(library_id) return result diff --git a/homeassistant/components/media_player/browse_media.py b/homeassistant/components/media_player/browse_media.py index 9327bf68f9f..e3474eeb58e 100644 --- a/homeassistant/components/media_player/browse_media.py +++ b/homeassistant/components/media_player/browse_media.py @@ -1,6 +1,7 @@ """Browse media features for media player.""" from __future__ import annotations +from collections.abc import Sequence from datetime import timedelta import logging from typing import Any @@ -97,7 +98,7 @@ class BrowseMedia: title: str, can_play: bool, can_expand: bool, - children: list[BrowseMedia] | None = None, + children: Sequence[BrowseMedia] | None = None, children_media_class: str | None = None, thumbnail: str | None = None, not_shown: int = 0, diff --git a/homeassistant/components/media_source/models.py b/homeassistant/components/media_source/models.py index 0aee6ad1330..f6772bc6ad9 100644 --- a/homeassistant/components/media_source/models.py +++ b/homeassistant/components/media_source/models.py @@ -27,8 +27,6 @@ class PlayMedia: class BrowseMediaSource(BrowseMedia): """Represent a browsable media file.""" - children: list[BrowseMediaSource | BrowseMedia] | None - def __init__( self, *, domain: str | None, identifier: str | None, **kwargs: Any ) -> None: diff --git a/homeassistant/components/unifiprotect/media_source.py b/homeassistant/components/unifiprotect/media_source.py index 7e66f783ee0..104323eeaa2 100644 --- a/homeassistant/components/unifiprotect/media_source.py +++ b/homeassistant/components/unifiprotect/media_source.py @@ -528,7 +528,7 @@ class ProtectMediaSource(MediaSource): args["camera_id"] = camera_id events = await self._build_events(**args) # type: ignore[arg-type] - source.children = events # type: ignore[assignment] + source.children = events source.title = self._breadcrumb( data, title, @@ -653,7 +653,7 @@ class ProtectMediaSource(MediaSource): title = f"{start.strftime('%B %Y')} > {title}" events = await self._build_events(**args) # type: ignore[arg-type] - source.children = events # type: ignore[assignment] + source.children = events source.title = self._breadcrumb( data, title, diff --git a/homeassistant/components/xbox/browse_media.py b/homeassistant/components/xbox/browse_media.py index ee1eabf1e00..e4c268bd6b8 100644 --- a/homeassistant/components/xbox/browse_media.py +++ b/homeassistant/components/xbox/browse_media.py @@ -1,7 +1,7 @@ """Support for media browsing.""" from __future__ import annotations -from typing import TYPE_CHECKING, NamedTuple +from typing import NamedTuple from xbox.webapi.api.client import XboxLiveClient from xbox.webapi.api.provider.catalog.const import HOME_APP_IDS, SYSTEM_PFN_ID_MAP @@ -56,6 +56,7 @@ async def build_item_response( apps: InstalledPackagesList = await client.smartglass.get_installed_apps(device_id) if media_content_type in (None, "library"): + children: list[BrowseMedia] = [] library_info = BrowseMedia( media_class=MEDIA_CLASS_DIRECTORY, media_content_id="library", @@ -63,10 +64,8 @@ async def build_item_response( title="Installed Applications", can_play=False, can_expand=True, - children=[], + children=children, ) - if TYPE_CHECKING: - assert library_info.children is not None # Add Home id_type = AlternateIdType.LEGACY_XBOX_PRODUCT_ID @@ -78,7 +77,7 @@ async def build_item_response( home_thumb = _find_media_image( home_catalog.products[0].localized_properties[0].images ) - library_info.children.append( + children.append( BrowseMedia( media_class=MEDIA_CLASS_APP, media_content_id="Home", @@ -101,7 +100,7 @@ async def build_item_response( tv_thumb = _find_media_image( tv_catalog.products[0].localized_properties[0].images ) - library_info.children.append( + children.append( BrowseMedia( media_class=MEDIA_CLASS_APP, media_content_id="TV", @@ -117,7 +116,7 @@ async def build_item_response( {app.content_type for app in apps.result if app.content_type in TYPE_MAP} ) for c_type in content_types: - library_info.children.append( + children.append( BrowseMedia( media_class=MEDIA_CLASS_DIRECTORY, media_content_id=c_type,