Skip to content

Commit 23a4b31

Browse files
[PR #10102/7557b03d backport][3.11] Fix deprecated calls to guess_type for paths with Python 3.13 (#10103)
Co-authored-by: J. Nick Koston <nick@koston.org>
1 parent c41ffc7 commit 23a4b31

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

CHANGES/10102.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replaced deprecated call to :func:`mimetypes.guess_type` with :func:`mimetypes.guess_file_type` when using Python 3.13+ -- by :user:`bdraco`.

aiohttp/payload.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import json
55
import mimetypes
66
import os
7+
import sys
78
import warnings
89
from abc import ABC, abstractmethod
910
from itertools import chain
@@ -169,7 +170,11 @@ def __init__(
169170
if content_type is not sentinel and content_type is not None:
170171
self._headers[hdrs.CONTENT_TYPE] = content_type
171172
elif self._filename is not None:
172-
content_type = mimetypes.guess_type(self._filename)[0]
173+
if sys.version_info >= (3, 13):
174+
guesser = mimetypes.guess_file_type
175+
else:
176+
guesser = mimetypes.guess_type
177+
content_type = guesser(self._filename)[0]
173178
if content_type is None:
174179
content_type = self._default_content_type
175180
self._headers[hdrs.CONTENT_TYPE] = content_type

aiohttp/web_fileresponse.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
import os
33
import pathlib
4+
import sys
45
from contextlib import suppress
56
from mimetypes import MimeTypes
67
from stat import S_ISREG
@@ -317,9 +318,11 @@ async def prepare(self, request: "BaseRequest") -> Optional[AbstractStreamWriter
317318
# extension of the request path. The encoding returned by guess_type
318319
# can be ignored since the map was cleared above.
319320
if hdrs.CONTENT_TYPE not in self.headers:
320-
self.content_type = (
321-
CONTENT_TYPES.guess_type(self._path)[0] or FALLBACK_CONTENT_TYPE
322-
)
321+
if sys.version_info >= (3, 13):
322+
guesser = CONTENT_TYPES.guess_file_type
323+
else:
324+
guesser = CONTENT_TYPES.guess_type
325+
self.content_type = guesser(self._path)[0] or FALLBACK_CONTENT_TYPE
323326

324327
if file_encoding:
325328
self.headers[hdrs.CONTENT_ENCODING] = file_encoding

0 commit comments

Comments
 (0)