Skip to content

Commit

Permalink
Fixed mimetypes.guess_type not supporting PathLike on py3.7 and below
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinastone committed Jul 20, 2020
1 parent 6746e61 commit a9c423c
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion starlette/responses.py
Expand Up @@ -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
Expand All @@ -30,6 +31,15 @@
ujson = None # type: ignore


# Compatibility wrapper for `mimetypes.guess_type` to support `os.PathLike` on <py3.8
def guess_type(
url: typing.Union[str, "os.PathLike[str]"], strict: bool = True
) -> 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"
Expand Down

0 comments on commit a9c423c

Please sign in to comment.