Merge pull request #4735 from pallets/json_encoder-warning
Before adoption: this commit came in with the history the project adopted, so no landing records it.
show deprecation warning when using json_encoder/decoder
authored by David Lord · committed by GitHub · parents 9a1b25f, 714ccef · Browse files · 5 files·+167 added−42 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+167 added−42 removed
- other:+167 added−42 removed
Provenance
- Commit
723a3a6- Tree
339877b- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
9a1b25f- Parent 2
714ccef
5 files changed+167 added−42 removed
modified:src/flask/json/provider.py+6 added-6 removed
174 unmodified lines175 176 :param obj: The data to serialize.177 :param kwargs: Passed to :func:`json.dumps`.178 """179- cls = self._app.json_encoder179+ cls = self._app._json_encoder180 bp = self._app.blueprints.get(request.blueprint) if request else None181 182- if bp is not None and bp.json_encoder is not None:183- cls = bp.json_encoder182+ if bp is not None and bp._json_encoder is not None:183+ cls = bp._json_encoder184 185 if cls is not None:186 import warnings187 46 unmodified lines234 235 :param s: Text or UTF-8 bytes.236 :param kwargs: Passed to :func:`json.loads`.237 """238- cls = self._app.json_decoder238+ cls = self._app._json_decoder239 bp = self._app.blueprints.get(request.blueprint) if request else None240 241- if bp is not None and bp.json_decoder is not None:242- cls = bp.json_decoder241+ if bp is not None and bp._json_decoder is not None:242+ cls = bp._json_decoder243 244 if cls is not None:245 import warnings246 64 unmodified linesmodified:src/flask/app.py+80 added-18 removed
1 import functools2 import inspect3+import json4 import logging5 import os6 import sys7 import typing as t371 unmodified lines379 stacklevel=2,380 )381 self.config["USE_X_SENDFILE"] = value382 382- #: The JSON encoder class to use. Defaults to383- #: :class:`~flask.json.JSONEncoder`.384- #:385- #: .. deprecated:: 2.2386- #: Will be removed in Flask 2.3. Customize387- #: :attr:`json_provider_class` instead.388- #:389- #: .. versionadded:: 0.10390- json_encoder: None = None383+ _json_encoder: t.Union[t.Type[json.JSONEncoder], None] = None384+ _json_decoder: t.Union[t.Type[json.JSONDecoder], None] = None385 392- #: The JSON decoder class to use. Defaults to393- #: :class:`~flask.json.JSONDecoder`.394- #:395- #: .. deprecated:: 2.2396- #: Will be removed in Flask 2.3. Customize397- #: :attr:`json_provider_class` instead.398- #:399- #: .. versionadded:: 0.10400- json_decoder: None = None386+ @property # type: ignore[override]387+ def json_encoder(self) -> t.Type[json.JSONEncoder]: # type: ignore[override]388+ """The JSON encoder class to use. Defaults to389+ :class:`~flask.json.JSONEncoder`.390 391+ .. deprecated:: 2.2392+ Will be removed in Flask 2.3. Customize393+ :attr:`json_provider_class` instead.394+395+ .. versionadded:: 0.10396+ """397+ import warnings398+399+ warnings.warn(400+ "'app.json_encoder' is deprecated and will be removed in Flask 2.3."401+ " Customize 'app.json_provider_class' or 'app.json' instead.",402+ DeprecationWarning,403+ stacklevel=2,404+ )405+406+ if self._json_encoder is None:407+ from . import json408+409+ return json.JSONEncoder410+411+ return self._json_encoder412+413+ @json_encoder.setter414+ def json_encoder(self, value: t.Type[json.JSONEncoder]) -> None:415+ import warnings416+417+ warnings.warn(418+ "'app.json_encoder' is deprecated and will be removed in Flask 2.3."419+ " Customize 'app.json_provider_class' or 'app.json' instead.",420+ DeprecationWarning,421+ stacklevel=2,422+ )423+ self._json_encoder = value424+425+ @property # type: ignore[override]426+ def json_decoder(self) -> t.Type[json.JSONDecoder]: # type: ignore[override]427+ """The JSON decoder class to use. Defaults to428+ :class:`~flask.json.JSONDecoder`.429+430+ .. deprecated:: 2.2431+ Will be removed in Flask 2.3. Customize432+ :attr:`json_provider_class` instead.433+434+ .. versionadded:: 0.10435+ """436+ import warnings437+438+ warnings.warn(439+ "'app.json_decoder' is deprecated and will be removed in Flask 2.3."440+ " Customize 'app.json_provider_class' or 'app.json' instead.",441+ DeprecationWarning,442+ stacklevel=2,443+ )444+445+ if self._json_decoder is None:446+ from . import json447+448+ return json.JSONDecoder449+450+ return self._json_decoder451+452+ @json_decoder.setter453+ def json_decoder(self, value: t.Type[json.JSONDecoder]) -> None:454+ import warnings455+456+ warnings.warn(457+ "'app.json_decoder' is deprecated and will be removed in Flask 2.3."458+ " Customize 'app.json_provider_class' or 'app.json' instead.",459+ DeprecationWarning,460+ stacklevel=2,461+ )462+ self._json_decoder = value463+464 json_provider_class: t.Type[JSONProvider] = DefaultJSONProvider465 """A subclass of :class:`~flask.json.provider.JSONProvider`. An466 instance is created and assigned to :attr:`app.json` when creating467 the app.2081 unmodified linesmodified:src/flask/blueprints.py+71 added-12 removed
1+import json2 import os3 import typing as t4 from collections import defaultdict5 from functools import update_wrapper166 unmodified lines172 """173 174 _got_registered_once = False175 175- #: Blueprint local JSON encoder class to use. Set to ``None`` to use176- #: the app's :class:`~flask.Flask.json_encoder`.177- #:178- #: .. deprecated:: 2.2179- #: Will be removed in Flask 2.3.180- json_encoder: None = None181- #: Blueprint local JSON decoder class to use. Set to ``None`` to use182- #: the app's :class:`~flask.Flask.json_decoder`.183- #:184- #: .. deprecated:: 2.2185- #: Will be removed in Flask 2.3.186- json_decoder: None = None176+ _json_encoder: t.Union[t.Type[json.JSONEncoder], None] = None177+ _json_decoder: t.Union[t.Type[json.JSONDecoder], None] = None178 179+ @property # type: ignore[override]180+ def json_encoder( # type: ignore[override]181+ self,182+ ) -> t.Union[t.Type[json.JSONEncoder], None]:183+ """Blueprint-local JSON encoder class to use. Set to ``None`` to use the app's.184+185+ .. deprecated:: 2.2186+ Will be removed in Flask 2.3. Customize187+ :attr:`json_provider_class` instead.188+189+ .. versionadded:: 0.10190+ """191+ import warnings192+193+ warnings.warn(194+ "'bp.json_encoder' is deprecated and will be removed in Flask 2.3."195+ " Customize 'app.json_provider_class' or 'app.json' instead.",196+ DeprecationWarning,197+ stacklevel=2,198+ )199+ return self._json_encoder200+201+ @json_encoder.setter202+ def json_encoder(self, value: t.Union[t.Type[json.JSONEncoder], None]) -> None:203+ import warnings204+205+ warnings.warn(206+ "'bp.json_encoder' is deprecated and will be removed in Flask 2.3."207+ " Customize 'app.json_provider_class' or 'app.json' instead.",208+ DeprecationWarning,209+ stacklevel=2,210+ )211+ self._json_encoder = value212+213+ @property # type: ignore[override]214+ def json_decoder( # type: ignore[override]215+ self,216+ ) -> t.Union[t.Type[json.JSONDecoder], None]:217+ """Blueprint-local JSON decoder class to use. Set to ``None`` to use the app's.218+219+ .. deprecated:: 2.2220+ Will be removed in Flask 2.3. Customize221+ :attr:`json_provider_class` instead.222+223+ .. versionadded:: 0.10224+ """225+ import warnings226+227+ warnings.warn(228+ "'bp.json_decoder' is deprecated and will be removed in Flask 2.3."229+ " Customize 'app.json_provider_class' or 'app.json' instead.",230+ DeprecationWarning,231+ stacklevel=2,232+ )233+ return self._json_decoder234+235+ @json_decoder.setter236+ def json_decoder(self, value: t.Union[t.Type[json.JSONDecoder], None]) -> None:237+ import warnings238+239+ warnings.warn(240+ "'bp.json_decoder' is deprecated and will be removed in Flask 2.3."241+ " Customize 'app.json_provider_class' or 'app.json' instead.",242+ DeprecationWarning,243+ stacklevel=2,244+ )245+ self._json_decoder = value246+247 def __init__(248 self,249 name: str,250 import_name: str,456 unmodified linesmodified:src/flask/scaffold.py+3 added-2 removed
1 import importlib.util2+import json3 import os4 import pathlib5 import pkgutil6 import sys71 unmodified lines78 #: blueprint sets this, it will be used instead of the app's value.79 #:80 #: .. deprecated:: 2.281 #: Will be removed in Flask 2.3.81- json_encoder: None = None82+ json_encoder: t.Union[t.Type[json.JSONEncoder], None] = None83 84 #: JSON decoder class used by :func:`flask.json.loads`. If a85 #: blueprint sets this, it will be used instead of the app's value.86 #:87 #: .. deprecated:: 2.288 #: Will be removed in Flask 2.3.88- json_decoder: None = None89+ json_decoder: t.Union[t.Type[json.JSONDecoder], None] = None90 91 def __init__(92 self,93 import_name: str,805 unmodified linesmodified:CHANGES.rst+7 added-4 removed
1 unmodified line2 -------------3 4 Unreleased5 6+- Setting or accessing ``json_encoder`` or ``json_decoder`` raises a7+ deprecation warning. :issue:`4732`8 9+10 Version 2.2.011 -------------12 13 Released 2022-08-0138 unmodified lines52 - ``flask.json`` is an instance of ``JSONProvider``. A different53 provider can be set to use a different JSON library.54 ``flask.jsonify`` will call ``app.json.response``, other55 functions in ``flask.json`` will call corresponding functions in53- ``app.json``. :pr:`4688`56+ ``app.json``. :pr:`4692`57 58 - JSON configuration is moved to attributes on the default59 ``app.json`` provider. ``JSON_AS_ASCII``, ``JSON_SORT_KEYS``,60 ``JSONIFY_MIMETYPE``, and ``JSONIFY_PRETTYPRINT_REGULAR`` are58- deprecated. :pr:`4688`61+ deprecated. :pr:`4692`62 - Setting custom ``json_encoder`` and ``json_decoder`` classes on the63 app or a blueprint, and the corresponding ``json.JSONEncoder`` and64 ``JSONDecoder`` classes, are deprecated. JSON behavior can now be62- overridden using the ``app.json`` provider interface. :pr:`4688`65+ overridden using the ``app.json`` provider interface. :pr:`4692`66 - ``json.htmlsafe_dumps`` and ``json.htmlsafe_dump`` are deprecated,64- the function is built-in to Jinja now. :pr:`4688`67+ the function is built-in to Jinja now. :pr:`4692`68 - Refactor ``register_error_handler`` to consolidate error checking.69 Rewrite some error messages to be more consistent. :issue:`4559`70 - Use Blueprint decorators and functions intended for setup after71 registering the blueprint will show a warning. In the next version,1324 unmodified lines