Skip to content

Commit

Permalink
Revert "Use os.PathLike in StaticFiles for directory (encode#1007)"
Browse files Browse the repository at this point in the history
This reverts commit 9387832.
  • Loading branch information
em92 committed Sep 11, 2020
1 parent 9693a3f commit c61f2ca
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 35 deletions.
4 changes: 1 addition & 3 deletions docs/staticfiles.md
Expand Up @@ -5,7 +5,7 @@ Starlette also includes a `StaticFiles` class for serving files in a given direc

Signature: `StaticFiles(directory=None, packages=None, check_dir=True)`

* `directory` - A string or [os.Pathlike][pathlike] denoting a directory path.
* `directory` - A string denoting a directory path.
* `packages` - A list of strings of python packages.
* `html` - Run in HTML mode. Automatically loads `index.html` for directories if such file exist.
* `check_dir` - Ensure that the directory exists upon instantiation. Defaults to `True`.
Expand Down Expand Up @@ -51,5 +51,3 @@ app = Starlette(routes=routes)
You may prefer to include static files directly inside the "static" directory
rather than using Python packaging to include static files, but it can be useful
for bundling up reusable components.

[pathlike]: https://docs.python.org/3/library/os.html#os.PathLike
14 changes: 2 additions & 12 deletions starlette/responses.py
Expand Up @@ -3,10 +3,9 @@
import json
import os
import stat
import sys
import typing
from email.utils import formatdate
from mimetypes import guess_type as mimetypes_guess_type
from mimetypes import guess_type
from urllib.parse import quote, quote_plus

from async_generator import isasyncgen
Expand All @@ -32,15 +31,6 @@
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 Expand Up @@ -250,7 +240,7 @@ class FileResponse(Response):

def __init__(
self,
path: typing.Union[str, "os.PathLike[str]"],
path: str,
status_code: int = 200,
headers: dict = None,
media_type: str = None,
Expand Down
10 changes: 4 additions & 6 deletions starlette/staticfiles.py
Expand Up @@ -15,8 +15,6 @@
)
from starlette.types import Receive, Scope, Send

PathLike = typing.Union[str, "os.PathLike[str]"]


class NotModifiedResponse(Response):
NOT_MODIFIED_HEADERS = (
Expand All @@ -43,7 +41,7 @@ class StaticFiles:
def __init__(
self,
*,
directory: PathLike = None,
directory: str = None,
packages: typing.List[str] = None,
html: bool = False,
check_dir: bool = True
Expand All @@ -57,8 +55,8 @@ def __init__(
raise RuntimeError("Directory '{}' does not exist".format(directory))

def get_directories(
self, directory: PathLike = None, packages: typing.List[str] = None
) -> typing.List[PathLike]:
self, directory: str = None, packages: typing.List[str] = None
) -> typing.List[str]:
"""
Given `directory` and `packages` arguments, return a list of all the
directories that should be used for serving static files from.
Expand Down Expand Up @@ -164,7 +162,7 @@ async def lookup_path(

def file_response(
self,
full_path: PathLike,
full_path: str,
stat_result: os.stat_result,
scope: Scope,
status_code: int = 200,
Expand Down
14 changes: 0 additions & 14 deletions tests/test_staticfiles.py
@@ -1,6 +1,5 @@
import asyncio
import os
import pathlib
import time

import pytest
Expand All @@ -24,19 +23,6 @@ def test_staticfiles(tmpdir):
assert response.text == "<file content>"


def test_staticfiles_with_pathlib(tmpdir):
base_dir = pathlib.Path(tmpdir)
path = base_dir / "example.txt"
with open(path, "w") as file:
file.write("<file content>")

app = StaticFiles(directory=base_dir)
client = TestClient(app)
response = client.get("/example.txt")
assert response.status_code == 200
assert response.text == "<file content>"


def test_staticfiles_head_with_middleware(tmpdir):
"""
see https://github.com/encode/starlette/pull/935
Expand Down

0 comments on commit c61f2ca

Please sign in to comment.