From a9c423cfc5c8ff8b1e098d878449f776353f5f76 Mon Sep 17 00:00:00 2001 From: Kevin Stone Date: Mon, 20 Jul 2020 11:41:10 -0500 Subject: [PATCH] Fixed `mimetypes.guess_type` not supporting PathLike on py3.7 and below --- starlette/responses.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/starlette/responses.py b/starlette/responses.py index 527db9c5f9..24dfb2c84c 100644 --- a/starlette/responses.py +++ b/starlette/responses.py @@ -4,9 +4,10 @@ import json import os import stat +import sys import typing from email.utils import formatdate -from mimetypes import guess_type +from mimetypes import guess_type as mimetypes_guess_type from urllib.parse import quote, quote_plus from starlette.background import BackgroundTask @@ -30,6 +31,15 @@ ujson = None # type: ignore +# Compatibility wrapper for `mimetypes.guess_type` to support `os.PathLike` on typing.Tuple[typing.Optional[str], typing.Optional[str]]: + if sys.version_info < (3, 8): # pragma: no cover + url = os.fspath(url) + return mimetypes_guess_type(url, strict) + + class Response: media_type = None charset = "utf-8"