From d36499526dd93c812e3fbe2d766a1d2147895de9 Mon Sep 17 00:00:00 2001 From: Marcos Schroh Date: Tue, 2 Oct 2018 20:06:34 +0200 Subject: [PATCH] Missing annotations added on staticfiles module (#70) * fix(Missing annotation): missing annotations added on staticfiles module. * fix(staticfiles annotations): annotations fixed. * refactor(staticfiles): only instanciate a class when is necessary. --- starlette/staticfiles.py | 27 +++++++++++++++------------ tests/test_staticfiles.py | 17 +++++++++-------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/starlette/staticfiles.py b/starlette/staticfiles.py index 07cafe5a..38cc7b68 100644 --- a/starlette/staticfiles.py +++ b/starlette/staticfiles.py @@ -1,25 +1,28 @@ -from starlette.responses import PlainTextResponse, FileResponse -from aiofiles.os import stat as aio_stat import os import stat +from aiofiles.os import stat as aio_stat + +from starlette.responses import PlainTextResponse, FileResponse, Response +from starlette.types import Send, Receive, Scope, ASGIInstance + class StaticFile: - def __init__(self, *, path): + def __init__(self, *, path: str) -> None: self.path = path - def __call__(self, scope): + def __call__(self, scope: Scope) -> ASGIInstance: if scope["method"] not in ("GET", "HEAD"): return PlainTextResponse("Method Not Allowed", status_code=405) return _StaticFileResponder(scope, path=self.path) class StaticFiles: - def __init__(self, *, directory): + def __init__(self, *, directory: str) -> None: self.directory = directory self.config_checked = False - def __call__(self, scope): + def __call__(self, scope: Scope) -> ASGIInstance: if scope["method"] not in ("GET", "HEAD"): return PlainTextResponse("Method Not Allowed", status_code=405) path = os.path.normpath(os.path.join(*scope["path"].split("/"))) @@ -35,11 +38,11 @@ class StaticFiles: class _StaticFileResponder: - def __init__(self, scope, path): + def __init__(self, scope: Scope, path: str) -> None: self.scope = scope self.path = path - async def __call__(self, receive, send): + async def __call__(self, receive: Receive, send: Send) -> None: try: stat_result = await aio_stat(self.path) except FileNotFoundError: @@ -54,12 +57,12 @@ class _StaticFileResponder: class _StaticFilesResponder: - def __init__(self, scope, path, check_directory=None): + def __init__(self, scope: Scope, path: str, check_directory: str = None) -> None: self.scope = scope self.path = path self.check_directory = check_directory - async def check_directory_configured_correctly(self): + async def check_directory_configured_correctly(self) -> None: """ Perform a one-off configuration check that StaticFiles is actually pointed at a directory, so that we can raise loud errors rather than @@ -73,14 +76,14 @@ class _StaticFilesResponder: if not (stat.S_ISDIR(stat_result.st_mode) or stat.S_ISLNK(stat_result.st_mode)): raise RuntimeError("StaticFiles path '%s' is not a directory." % directory) - async def __call__(self, receive, send): + async def __call__(self, receive: Receive, send: Send) -> None: if self.check_directory is not None: await self.check_directory_configured_correctly() try: stat_result = await aio_stat(self.path) except FileNotFoundError: - response = PlainTextResponse("Not Found", status_code=404) + response = PlainTextResponse("Not Found", status_code=404) # type: Response else: mode = stat_result.st_mode if not stat.S_ISREG(mode): diff --git a/tests/test_staticfiles.py b/tests/test_staticfiles.py index c5a2a66f..74bdc57d 100644 --- a/tests/test_staticfiles.py +++ b/tests/test_staticfiles.py @@ -1,8 +1,9 @@ -from starlette.testclient import TestClient -from starlette.staticfiles import StaticFile, StaticFiles import os import pytest +from starlette.testclient import TestClient +from starlette.staticfiles import StaticFile, StaticFiles + def test_staticfile(tmpdir): path = os.path.join(tmpdir, "example.txt") @@ -47,7 +48,7 @@ def test_staticfile_with_directory_raises_error(tmpdir): app = StaticFile(path=tmpdir) client = TestClient(app) with pytest.raises(RuntimeError) as exc: - response = client.get("/") + client.get("/") assert "is not a file" in str(exc) @@ -56,7 +57,7 @@ def test_staticfile_with_missing_file_raises_error(tmpdir): app = StaticFile(path=path) client = TestClient(app) with pytest.raises(RuntimeError) as exc: - response = client.get("/") + client.get("/") assert "does not exist" in str(exc) @@ -113,7 +114,7 @@ def test_staticfiles_configured_with_missing_directory(tmpdir): app = StaticFiles(directory=path) client = TestClient(app) with pytest.raises(RuntimeError) as exc: - response = client.get("/example.txt") + client.get("/example.txt") assert "does not exist" in str(exc) @@ -125,7 +126,7 @@ def test_staticfiles_configured_with_file_instead_of_directory(tmpdir): app = StaticFiles(directory=path) client = TestClient(app) with pytest.raises(RuntimeError) as exc: - response = client.get("/example.txt") + client.get("/example.txt") assert "is not a directory" in str(exc) @@ -133,9 +134,9 @@ def test_staticfiles_config_check_occurs_only_once(tmpdir): app = StaticFiles(directory=tmpdir) client = TestClient(app) assert not app.config_checked - response = client.get("/") + client.get("/") assert app.config_checked - response = client.get("/") + client.get("/") assert app.config_checked