From 88eb611eef7add22e4438d87bdf0a96a805256bc Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 5 Dec 2024 20:52:48 -0600 Subject: [PATCH] Fix deprecated call to mimetypes.guess_type in CachingStaticResource (#132299) --- homeassistant/components/http/static.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/http/static.py b/homeassistant/components/http/static.py index 29c5840a4bf291..9ca34af3741349 100644 --- a/homeassistant/components/http/static.py +++ b/homeassistant/components/http/static.py @@ -4,6 +4,7 @@ from collections.abc import Mapping from pathlib import Path +import sys from typing import Final from aiohttp.hdrs import CACHE_CONTROL, CONTENT_TYPE @@ -17,6 +18,15 @@ CACHE_HEADERS: Mapping[str, str] = {CACHE_CONTROL: CACHE_HEADER} RESPONSE_CACHE: LRU[tuple[str, Path], tuple[Path, str]] = LRU(512) +if sys.version_info >= (3, 13): + # guess_type is soft-deprecated in 3.13 + # for paths and should only be used for + # URLs. guess_file_type should be used + # for paths instead. + _GUESSER = CONTENT_TYPES.guess_file_type +else: + _GUESSER = CONTENT_TYPES.guess_type + class CachingStaticResource(StaticResource): """Static Resource handler that will add cache headers.""" @@ -37,9 +47,7 @@ async def _handle(self, request: Request) -> StreamResponse: # Must be directory index; ignore caching return response file_path = response._path # noqa: SLF001 - response.content_type = ( - CONTENT_TYPES.guess_type(file_path)[0] or FALLBACK_CONTENT_TYPE - ) + response.content_type = _GUESSER(file_path)[0] or FALLBACK_CONTENT_TYPE # Cache actual header after setter construction. content_type = response.headers[CONTENT_TYPE] RESPONSE_CACHE[key] = (file_path, content_type)