diff --git a/CHANGES/10102.bugfix.rst b/CHANGES/10102.bugfix.rst new file mode 100644 index 00000000000..86dda8684dd --- /dev/null +++ b/CHANGES/10102.bugfix.rst @@ -0,0 +1 @@ +Replaced deprecated call to :func:`mimetypes.guess_type` with :func:`mimetypes.guess_file_type` when using Python 3.13+ -- by :user:`bdraco`. diff --git a/aiohttp/payload.py b/aiohttp/payload.py index 1bc7f27de8b..55a7a677f49 100644 --- a/aiohttp/payload.py +++ b/aiohttp/payload.py @@ -4,6 +4,7 @@ import json import mimetypes import os +import sys import warnings from abc import ABC, abstractmethod from itertools import chain @@ -169,7 +170,11 @@ def __init__( assert isinstance(content_type, str) self._headers[hdrs.CONTENT_TYPE] = content_type elif self._filename is not None: - content_type = mimetypes.guess_type(self._filename)[0] + if sys.version_info >= (3, 13): + guesser = mimetypes.guess_file_type + else: + guesser = mimetypes.guess_type + content_type = guesser(self._filename)[0] if content_type is None: content_type = self._default_content_type self._headers[hdrs.CONTENT_TYPE] = content_type diff --git a/aiohttp/web_fileresponse.py b/aiohttp/web_fileresponse.py index 7a29c767ba5..eafc3b051cd 100644 --- a/aiohttp/web_fileresponse.py +++ b/aiohttp/web_fileresponse.py @@ -2,6 +2,7 @@ import io import os import pathlib +import sys from contextlib import suppress from mimetypes import MimeTypes from stat import S_ISREG @@ -360,9 +361,11 @@ async def _prepare_open_file( # extension of the request path. The encoding returned by guess_type # can be ignored since the map was cleared above. if hdrs.CONTENT_TYPE not in self.headers: - self.content_type = ( - CONTENT_TYPES.guess_type(self._path)[0] or FALLBACK_CONTENT_TYPE - ) + if sys.version_info >= (3, 13): + guesser = CONTENT_TYPES.guess_file_type + else: + guesser = CONTENT_TYPES.guess_type + self.content_type = guesser(self._path)[0] or FALLBACK_CONTENT_TYPE if file_encoding: self.headers[hdrs.CONTENT_ENCODING] = file_encoding