Merge pull request #4722 from pallets/deprecate-config-attrs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
deprecate config attributes
authored by David Lord · committed by GitHub · parents 98ca00d, bfdd371 · Browse files · 8 files·+154 added−57 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+154 added−57 removed
- tests:+0 added−11 removed
- other:+154 added−46 removed
Provenance
- Commit
cfd5783- Tree
abb687f- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
98ca00d- Parent 2
bfdd371
8 files changed+154 added−57 removed
modified:docs/api.rst+3 added-4 removed
123 unmodified lines124 :members:125 126 .. admonition:: Notice127 128- The ``PERMANENT_SESSION_LIFETIME`` config key can also be an integer129- starting with Flask 0.8. Either catch this down yourself or use130- the :attr:`~flask.Flask.permanent_session_lifetime` attribute on the131- app which converts the result to an integer automatically.128+ The :data:`PERMANENT_SESSION_LIFETIME` config can be an integer or ``timedelta``.129+ The :attr:`~flask.Flask.permanent_session_lifetime` attribute is always a130+ ``timedelta``.131 132 133 Test Client134 -----------616 unmodified linesmodified:src/flask/app.py+137 added-34 removed
97 unmodified lines98 99 return inspect.iscoroutinefunction(func)100 101 102-def _make_timedelta(value: t.Optional[timedelta]) -> t.Optional[timedelta]:102+def _make_timedelta(value: t.Union[timedelta, int, None]) -> t.Optional[timedelta]:103 if value is None or isinstance(value, timedelta):104 return value105 106 return timedelta(seconds=value)165 unmodified lines272 #: This attribute can also be configured from the config with the273 #: :data:`SECRET_KEY` configuration key. Defaults to ``None``.274 secret_key = ConfigAttribute("SECRET_KEY")275 276- #: The secure cookie uses this for the name of the session cookie.277- #:278- #: This attribute can also be configured from the config with the279- #: ``SESSION_COOKIE_NAME`` configuration key. Defaults to ``'session'``280- session_cookie_name = ConfigAttribute("SESSION_COOKIE_NAME")276+ @property277+ def session_cookie_name(self) -> str:278+ """The name of the cookie set by the session interface.279 280+ .. deprecated:: 2.2281+ Will be removed in Flask 2.3. Use ``app.config["SESSION_COOKIE_NAME"]``282+ instead.283+ """284+ import warnings285+286+ warnings.warn(287+ "'session_cookie_name' is deprecated and will be removed in Flask 2.3. Use"288+ " 'SESSION_COOKIE_NAME' in 'app.config' instead.",289+ DeprecationWarning,290+ stacklevel=2,291+ )292+ return self.config["SESSION_COOKIE_NAME"]293+294+ @session_cookie_name.setter295+ def session_cookie_name(self, value: str) -> None:296+ import warnings297+298+ warnings.warn(299+ "'session_cookie_name' is deprecated and will be removed in Flask 2.3. Use"300+ " 'SESSION_COOKIE_NAME' in 'app.config' instead.",301+ DeprecationWarning,302+ stacklevel=2,303+ )304+ self.config["SESSION_COOKIE_NAME"] = value305+306 #: A :class:`~datetime.timedelta` which is used to set the expiration307 #: date of a permanent session. The default is 31 days which makes a308 #: permanent session survive for roughly one month.309 #:3 unmodified lines313 permanent_session_lifetime = ConfigAttribute(314 "PERMANENT_SESSION_LIFETIME", get_converter=_make_timedelta315 )316 293- #: A :class:`~datetime.timedelta` or number of seconds which is used294- #: as the default ``max_age`` for :func:`send_file`. The default is295- #: ``None``, which tells the browser to use conditional requests296- #: instead of a timed cache.297- #:298- #: Configured with the :data:`SEND_FILE_MAX_AGE_DEFAULT`299- #: configuration key.300- #:301- #: .. versionchanged:: 2.0302- #: Defaults to ``None`` instead of 12 hours.303- send_file_max_age_default = ConfigAttribute(304- "SEND_FILE_MAX_AGE_DEFAULT", get_converter=_make_timedelta305- )317+ @property318+ def send_file_max_age_default(self) -> t.Optional[timedelta]:319+ """The default value for ``max_age`` for :func:`~flask.send_file`. The default320+ is ``None``, which tells the browser to use conditional requests instead of a321+ timed cache.322 307- #: Enable this if you want to use the X-Sendfile feature. Keep in308- #: mind that the server has to support this. This only affects files309- #: sent with the :func:`send_file` method.310- #:311- #: .. versionadded:: 0.2312- #:313- #: This attribute can also be configured from the config with the314- #: ``USE_X_SENDFILE`` configuration key. Defaults to ``False``.315- use_x_sendfile = ConfigAttribute("USE_X_SENDFILE")323+ .. deprecated:: 2.2324+ Will be removed in Flask 2.3. Use325+ ``app.config["SEND_FILE_MAX_AGE_DEFAULT"]`` instead.326 327+ .. versionchanged:: 2.0328+ Defaults to ``None`` instead of 12 hours.329+ """330+ import warnings331+332+ warnings.warn(333+ "'send_file_max_age_default' is deprecated and will be removed in Flask"334+ " 2.3. Use 'SEND_FILE_MAX_AGE_DEFAULT' in 'app.config' instead.",335+ DeprecationWarning,336+ stacklevel=2,337+ )338+ return _make_timedelta(self.config["SEND_FILE_MAX_AGE_DEFAULT"])339+340+ @send_file_max_age_default.setter341+ def send_file_max_age_default(self, value: t.Union[int, timedelta, None]) -> None:342+ import warnings343+344+ warnings.warn(345+ "'send_file_max_age_default' is deprecated and will be removed in Flask"346+ " 2.3. Use 'SEND_FILE_MAX_AGE_DEFAULT' in 'app.config' instead.",347+ DeprecationWarning,348+ stacklevel=2,349+ )350+ self.config["SEND_FILE_MAX_AGE_DEFAULT"] = _make_timedelta(value)351+352+ @property353+ def use_x_sendfile(self) -> bool:354+ """Enable this to use the ``X-Sendfile`` feature, assuming the server supports355+ it, from :func:`~flask.send_file`.356+357+ .. deprecated:: 2.2358+ Will be removed in Flask 2.3. Use ``app.config["USE_X_SENDFILE"]`` instead.359+ """360+ import warnings361+362+ warnings.warn(363+ "'use_x_sendfile' is deprecated and will be removed in Flask 2.3. Use"364+ " 'USE_X_SENDFILE' in 'app.config' instead.",365+ DeprecationWarning,366+ stacklevel=2,367+ )368+ return self.config["USE_X_SENDFILE"]369+370+ @use_x_sendfile.setter371+ def use_x_sendfile(self, value: bool) -> None:372+ import warnings373+374+ warnings.warn(375+ "'use_x_sendfile' is deprecated and will be removed in Flask 2.3. Use"376+ " 'USE_X_SENDFILE' in 'app.config' instead.",377+ DeprecationWarning,378+ stacklevel=2,379+ )380+ self.config["USE_X_SENDFILE"] = value381+382 #: The JSON encoder class to use. Defaults to383 #: :class:`~flask.json.JSONEncoder`.384 #:385 #: .. deprecated:: 2.2302 unmodified lines688 def propagate_exceptions(self) -> bool:689 """Returns the value of the ``PROPAGATE_EXCEPTIONS`` configuration690 value in case it's set, otherwise a sensible default is returned.691 692+ .. deprecated:: 2.2693+ Will be removed in Flask 2.3.694+695 .. versionadded:: 0.7696 """697+ import warnings698+699+ warnings.warn(700+ "'propagate_exceptions' is deprecated and will be removed in Flask 2.3.",701+ DeprecationWarning,702+ stacklevel=2,703+ )704 rv = self.config["PROPAGATE_EXCEPTIONS"]705 if rv is not None:706 return rv707 return self.testing or self.debug100 unmodified lines808 809 @property810 def templates_auto_reload(self) -> bool:811 """Reload templates when they are changed. Used by737- :meth:`create_jinja_environment`.812+ :meth:`create_jinja_environment`. It is enabled by default in debug mode.813 739- This attribute can be configured with :data:`TEMPLATES_AUTO_RELOAD`. If740- not set, it will be enabled in debug mode.814+ .. deprecated:: 2.2815+ Will be removed in Flask 2.3. Use ``app.config["TEMPLATES_AUTO_RELOAD"]``816+ instead.817 818 .. versionadded:: 1.0819 This property was added but the underlying config and behavior820 already existed.821 """822+ import warnings823+824+ warnings.warn(825+ "'templates_auto_reload' is deprecated and will be removed in Flask 2.3."826+ " Use 'TEMPLATES_AUTO_RELOAD' in 'app.config' instead.",827+ DeprecationWarning,828+ stacklevel=2,829+ )830 rv = self.config["TEMPLATES_AUTO_RELOAD"]831 return rv if rv is not None else self.debug832 833 @templates_auto_reload.setter834 def templates_auto_reload(self, value: bool) -> None:835+ import warnings836+837+ warnings.warn(838+ "'templates_auto_reload' is deprecated and will be removed in Flask 2.3."839+ " Use 'TEMPLATES_AUTO_RELOAD' in 'app.config' instead.",840+ DeprecationWarning,841+ stacklevel=2,842+ )843 self.config["TEMPLATES_AUTO_RELOAD"] = value844 845 def create_jinja_environment(self) -> Environment:846 """Create the Jinja environment based on :attr:`jinja_options`12 unmodified lines859 if "autoescape" not in options:860 options["autoescape"] = self.select_jinja_autoescape861 862 if "auto_reload" not in options:771- options["auto_reload"] = self.templates_auto_reload863+ auto_reload = self.config["TEMPLATES_AUTO_RELOAD"]864 865+ if auto_reload is None:866+ auto_reload = self.debug867+868+ options["auto_reload"] = auto_reload869+870 rv = self.jinja_environment(self, **options)871 rv.globals.update(872 url_for=self.url_for,873 get_flashed_messages=get_flashed_messages,120 unmodified lines994 995 @debug.setter996 def debug(self, value: bool) -> None:997 self.config["DEBUG"] = value901- self.jinja_env.auto_reload = self.templates_auto_reload998 999+ if self.config["TEMPLATES_AUTO_RELOAD"] is None:1000+ self.jinja_env.auto_reload = value1001+1002 def run(1003 self,1004 host: t.Optional[str] = None,1005 port: t.Optional[int] = None,633 unmodified lines1639 .. versionadded:: 0.31640 """1641 exc_info = sys.exc_info()1642 got_request_exception.send(self, exception=e)1643+ propagate = self.config["PROPAGATE_EXCEPTIONS"]1644 1545- if self.propagate_exceptions:1645+ if propagate is None:1646+ propagate = self.testing or self.debug1647+1648+ if propagate:1649 # Re-raise if called with an active exception, otherwise1650 # raise the passed in exception.1651 if exc_info[1] is e:1652 raise834 unmodified linesmodified:src/flask/helpers.py+1 added-1 removed
412 unmodified lines413 kwargs["max_age"] = current_app.get_send_file_max_age414 415 kwargs.update(416 environ=request.environ,417- use_x_sendfile=current_app.use_x_sendfile,417+ use_x_sendfile=current_app.config["USE_X_SENDFILE"],418 response_class=current_app.response_class,419 _root_path=current_app.root_path, # type: ignore420 )421 return kwargs284 unmodified linesmodified:src/flask/scaffold.py+6 added-2 removed
3 unmodified lines4 import pkgutil5 import sys6 import typing as t7 from collections import defaultdict8+from datetime import timedelta9 from functools import update_wrapper10 11 from jinja2 import FileSystemLoader12 from werkzeug.exceptions import default_exceptions289 unmodified lines302 The default configuration is ``None`` instead of 12 hours.303 304 .. versionadded:: 0.9305 """305- value = current_app.send_file_max_age_default306+ value = current_app.config["SEND_FILE_MAX_AGE_DEFAULT"]307 308 if value is None:309 return None310 311+ if isinstance(value, timedelta):310- return int(value.total_seconds())312+ return int(value.total_seconds())313 314+ return value315+316 def send_static_file(self, filename: str) -> "Response":317 """The view function used to serve files from318 :attr:`static_folder`. A route is automatically registered for319 this view at :attr:`static_url_path` if :attr:`static_folder` is578 unmodified linesmodified:src/flask/sessions.py+2 added-5 removed
175 unmodified lines176 """177 return isinstance(obj, self.null_session_class)178 179 def get_cookie_name(self, app: "Flask") -> str:180- """Returns the name of the session cookie.180+ """The name of the session cookie. Uses``app.config["SESSION_COOKIE_NAME"]``."""181+ return app.config["SESSION_COOKIE_NAME"]182 182- Uses ``app.session_cookie_name`` which is set to ``SESSION_COOKIE_NAME``183- """184- return app.session_cookie_name185-183 def get_cookie_domain(self, app: "Flask") -> t.Optional[str]:184 """Returns the domain that should be set for the session cookie.185 186 Uses ``SESSION_COOKIE_DOMAIN`` if it is configured, otherwise233 unmodified linesmodified:tests/test_config.py-9 removed
1 import json2 import os3 import textwrap4-from datetime import timedelta4 5 import pytest6 7 import flask197 unmodified lines205 app.config["PERMANENT_SESSION_LIFETIME"] = 42206 assert app.permanent_session_lifetime.seconds == 42207 208 210-def test_send_file_max_age():211- app = flask.Flask(__name__)212- app.config["SEND_FILE_MAX_AGE_DEFAULT"] = 3600213- assert app.send_file_max_age_default.seconds == 3600214- app.config["SEND_FILE_MAX_AGE_DEFAULT"] = timedelta(hours=2)215- assert app.send_file_max_age_default.seconds == 7200216-217-209 def test_get_namespace():210 app = flask.Flask(__name__)211 app.config["FOO_OPTION_1"] = "foo option 1"212 app.config["FOO_OPTION_2"] = "foo option 2"37 unmodified linesmodified:tests/test_templating.py-2 removed
395 unmodified lines396 397 monkeypatch.setattr(werkzeug.serving, "run_simple", run_simple_mock)398 399 app.run()400- assert not app.templates_auto_reload400 assert not app.jinja_env.auto_reload401 402 app.run(debug=True)404- assert app.templates_auto_reload403 assert app.jinja_env.auto_reload404 405 406 def test_template_loader_debugging(test_apps, monkeypatch):44 unmodified linesmodified:CHANGES.rst+5 added
25 unmodified lines26 - The ``FLASK_ENV`` environment variable and ``app.env`` attribute are27 deprecated, removing the distinction between development and debug28 mode. Debug mode should be controlled directly using the ``--debug``29 option or ``app.run(debug=True)``. :issue:`4714`30+- Some attributes that proxied config keys on ``app`` are deprecated:31+ ``session_cookie_name``, ``send_file_max_age_default``,32+ ``use_x_sendfile``, ``propagate_exceptions``, and33+ ``templates_auto_reload``. Use the relevant config keys instead.34+ :issue:`4716`35 - Add new customization points to the ``Flask`` app object for many36 previously global behaviors.37 38 - ``flask.url_for`` will call ``app.url_for``. :issue:`4568`1348 unmodified lines