Skip to content
Myr

Merge pull request #4026 from greyli/improve-type-hints

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Improve type hints for send_file and send_from_directory

authored by David Lord · committed by GitHub · parents 6414df9, d5aadba · Browse files · 3 files+32 added−22 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+32 added−22 removed

  • other:+32 added−22 removed

3 files+32 added−22 removed

modified:src/flask/helpers.py+29 added-21 removed
2 unmodified lines
3import socket4import sys5import typing as t6import warnings7from datetime import datetime8from datetime import timedelta9from functools import update_wrapper10from threading import RLock11
424 unmodified lines
436 return flashes437438439def _prepare_send_file_kwargs(439 download_name=None,440 attachment_filename=None,441 etag=None,442 add_etags=None,443 max_age=None,444 cache_timeout=None,445 **kwargs,446):440 download_name: t.Optional[str] = None,441 attachment_filename: t.Optional[str] = None,442 etag: t.Optional[t.Union[bool, str]] = None,443 add_etags: t.Optional[t.Union[bool]] = None,444 max_age: t.Optional[445 t.Union[int, t.Callable[[t.Optional[str]], t.Optional[int]]]446 ] = None,447 cache_timeout: t.Optional[int] = None,448 **kwargs: t.Any,449) -> t.Dict[str, t.Any]:450 if attachment_filename is not None:451 warnings.warn(452 "The 'attachment_filename' parameter has been renamed to"453 " 'download_name'. The old name will be removed in Flask"
30 unmodified lines
484 etag=etag,485 max_age=max_age,486 use_x_sendfile=current_app.use_x_sendfile,487 response_class=current_app.response_class,485 _root_path=current_app.root_path,488 _root_path=current_app.root_path, # type: ignore489 )490 return kwargs491492493def send_file(491 path_or_file,492 mimetype=None,493 as_attachment=False,494 download_name=None,495 attachment_filename=None,496 conditional=True,497 etag=True,498 add_etags=None,499 last_modified=None,500 max_age=None,501 cache_timeout=None,494 path_or_file: t.Union[os.PathLike, str, t.BinaryIO],495 mimetype: t.Optional[str] = None,496 as_attachment: bool = False,497 download_name: t.Optional[str] = None,498 attachment_filename: t.Optional[str] = None,499 conditional: bool = True,500 etag: t.Union[bool, str] = True,501 add_etags: t.Optional[bool] = None,502 last_modified: t.Optional[t.Union[datetime, int, float]] = None,503 max_age: t.Optional[504 t.Union[int, t.Callable[[t.Optional[str]], t.Optional[int]]]505 ] = None,506 cache_timeout: t.Optional[int] = None,507):508 """Send the contents of a file to the client.509510 The first argument can be a file path or a file-like object. Paths
136 unmodified lines
647 return path648649650def send_from_directory(646 directory: str, path: str, filename: t.Optional[str] = None, **kwargs: t.Any651 directory: t.Union[os.PathLike, str],652 path: t.Union[os.PathLike, str],653 filename: t.Optional[str] = None,654 **kwargs: t.Any,655) -> "Response":656 """Send a file from within a directory using :func:`send_file`.657658 .. code-block:: python
165 unmodified lines
modified:src/flask/scaffold.py+1 added-1 removed
286 unmodified lines
287 value = value.rstrip("/")288289 self._static_url_path = value290291 def get_send_file_max_age(self, filename: str) -> t.Optional[int]:291 def get_send_file_max_age(self, filename: t.Optional[str]) -> t.Optional[int]:292 """Used by :func:`send_file` to determine the ``max_age`` cache293 value for a given file path if it wasn't passed.294295 By default, this returns :data:`SEND_FILE_MAX_AGE_DEFAULT` from
567 unmodified lines
modified:CHANGES.rst+2 added
11 unmodified lines
12 imports in user projects. :issue:`4024`13- Fix type annotation for ``g`` and inform mypy that it is a namespace14 object that has arbitrary attributes. :issue:`4020`15- Fix some types that weren't available in Python 3.6.0. :issue:`4040`16- Improve typing for ``send_file``, ``send_from_directory``, and17 ``get_send_file_max_age``. :issue:`4044`, :pr:`4026`181920Version 2.0.021-------------
1096 unmodified lines