Skip to content

Commit db5e6bb

Browse files
authored
[PR #10122/703ce61 backport][3.11] Typing improvements for file responses (#10123)
1 parent bcae561 commit db5e6bb

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

aiohttp/web_fileresponse.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,7 @@ def _get_file_path_stat_encoding(
250250

251251
# Fallback to the uncompressed file
252252
st = file_path.stat()
253-
if not S_ISREG(st.st_mode):
254-
return None, st, None
255-
return file_path, st, None
253+
return file_path if S_ISREG(st.st_mode) else None, st, None
256254

257255
async def prepare(self, request: "BaseRequest") -> Optional[AbstractStreamWriter]:
258256
loop = asyncio.get_running_loop()
@@ -307,12 +305,12 @@ async def _prepare_open_file(
307305
file_encoding: Optional[str],
308306
) -> Optional[AbstractStreamWriter]:
309307
status = self._status
310-
file_size = st.st_size
311-
count = file_size
312-
start = None
308+
file_size: int = st.st_size
309+
file_mtime: float = st.st_mtime
310+
count: int = file_size
311+
start: Optional[int] = None
313312

314-
ifrange = request.if_range
315-
if ifrange is None or st.st_mtime <= ifrange.timestamp():
313+
if (ifrange := request.if_range) is None or file_mtime <= ifrange.timestamp():
316314
# If-Range header check:
317315
# condition = cached date >= last modification date
318316
# return 206 if True else 200.
@@ -323,7 +321,7 @@ async def _prepare_open_file(
323321
try:
324322
rng = request.http_range
325323
start = rng.start
326-
end = rng.stop
324+
end: Optional[int] = rng.stop
327325
except ValueError:
328326
# https://tools.ietf.org/html/rfc7233:
329327
# A server generating a 416 (Range Not Satisfiable) response to
@@ -340,7 +338,7 @@ async def _prepare_open_file(
340338

341339
# If a range request has been made, convert start, end slice
342340
# notation into file pointer offset and count
343-
if start is not None or end is not None:
341+
if start is not None:
344342
if start < 0 and end is None: # return tail of file
345343
start += file_size
346344
if start < 0:
@@ -398,25 +396,23 @@ async def _prepare_open_file(
398396
self._compression = False
399397

400398
self.etag = f"{st.st_mtime_ns:x}-{st.st_size:x}" # type: ignore[assignment]
401-
self.last_modified = st.st_mtime # type: ignore[assignment]
399+
self.last_modified = file_mtime # type: ignore[assignment]
402400
self.content_length = count
403401

404402
self._headers[hdrs.ACCEPT_RANGES] = "bytes"
405403

406-
real_start = cast(int, start)
407-
408404
if status == HTTPPartialContent.status_code:
405+
real_start = start
406+
assert real_start is not None
409407
self._headers[hdrs.CONTENT_RANGE] = "bytes {}-{}/{}".format(
410408
real_start, real_start + count - 1, file_size
411409
)
412410

413411
# If we are sending 0 bytes calling sendfile() will throw a ValueError
414-
if count == 0 or must_be_empty_body(request.method, self.status):
412+
if count == 0 or must_be_empty_body(request.method, status):
415413
return await super().prepare(request)
416414

417-
if start: # be aware that start could be None or int=0 here.
418-
offset = start
419-
else:
420-
offset = 0
415+
# be aware that start could be None or int=0 here.
416+
offset = start or 0
421417

422418
return await self._sendfile(request, fobj, offset, count)

0 commit comments

Comments
 (0)