use template_filter without parens
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by kadai0308 · committed by David Lord · parent 85c5d93 · Browse files · 6 files·+272 added−23 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+272 added−23 removed
- tests:+162 added−0 removed
- other:+110 added−23 removed
Provenance
- Commit
daf1510- Tree
df00172- Author
kadai0308 <kadai0308@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
85c5d93
6 files changed+272 added−23 removed
modified:docs/templating.rst+5 added-1 removed
143 unmodified lines144 that. You can either put them by hand into the145 :attr:`~flask.Flask.jinja_env` of the application or use the146 :meth:`~flask.Flask.template_filter` decorator.147 148-The two following examples work the same and both reverse an object::148+The following examples work the same and all reverse an object::149 150+ @app.template_filter # use the function name as filter name151+ def reverse_filter(s):152+ return s[::-1]153+154 @app.template_filter('reverse')155 def reverse_filter(s):156 return s[::-1]157 76 unmodified linesmodified:src/flask/sansio/app.py+68 added-16 removed
660 unmodified lines661 self.view_functions[endpoint] = view_func662 663 @setupmethod664 def template_filter(665- self, name: str | None = None666- ) -> t.Callable[[T_template_filter], T_template_filter]:665+ self, name: t.Callable[..., t.Any] | str | None = None666+ ) -> t.Callable[[T_template_filter], T_template_filter] | T_template_filter:667 """A decorator that is used to register custom template filter.668 You can specify a name for the filter, otherwise the function669 name will be used. Example::670 671- @app.template_filter()672- def reverse(s):673- return s[::-1]671+ @app.template_filter()672+ def reverse(s):673+ return s[::-1]674 675+ The decorator also can be used without parentheses::676+677+ @app.template_filter678+ def reverse(s):679+ return s[::-1]680+681 :param name: the optional name of the filter, otherwise the682 function name will be used.683 """684 685+ if callable(name):686+ # If name is callable, it is the function to register.687+ # This is a shortcut for the common case of688+ # @app.template_filter689+ # def func():690+691+ func = name692+ self.add_template_filter(func)693+ return func694+695 def decorator(f: T_template_filter) -> T_template_filter:696 self.add_template_filter(f, name=name)697 return f698 12 unmodified lines711 self.jinja_env.filters[name or f.__name__] = f712 713 @setupmethod714 def template_test(699- self, name: str | None = None700- ) -> t.Callable[[T_template_test], T_template_test]:715+ self, name: t.Callable[..., t.Any] | str | None = None716+ ) -> t.Callable[[T_template_test], T_template_test] | T_template_filter:717 """A decorator that is used to register custom template test.718 You can specify a name for the test, otherwise the function719 name will be used. Example::720 705- @app.template_test()706- def is_prime(n):707- if n == 2:708- return True709- for i in range(2, int(math.ceil(math.sqrt(n))) + 1):710- if n % i == 0:711- return False721+ @app.template_test()722+ def is_prime(n):723+ if n == 2:724+ return True725+ for i in range(2, int(math.ceil(math.sqrt(n))) + 1):726+ if n % i == 0:727+ return False728 return True729 730+ The decorator also can be used without parentheses::731+732+ @app.template_test733+ def is_prime(n):734+ if n == 2:735+ return True736+ for i in range(2, int(math.ceil(math.sqrt(n))) + 1):737+ if n % i == 0:738+ return False739+740 .. versionadded:: 0.10741 742 :param name: the optional name of the test, otherwise the743 function name will be used.744 """745 746+ if callable(name):747+ # If name is callable, it is the function to register.748+ # This is a shortcut for the common case of749+ # @app.template_test750+ # def func():751+752+ func = name753+ self.add_template_test(func)754+ return func755+756 def decorator(f: T_template_test) -> T_template_test:757 self.add_template_test(f, name=name)758 return f759 14 unmodified lines774 self.jinja_env.tests[name or f.__name__] = f775 776 @setupmethod777 def template_global(742- self, name: str | None = None743- ) -> t.Callable[[T_template_global], T_template_global]:778+ self, name: t.Callable[..., t.Any] | str | None = None779+ ) -> t.Callable[[T_template_global], T_template_global] | T_template_filter:780 """A decorator that is used to register a custom template global function.781 You can specify a name for the global function, otherwise the function782 name will be used. Example::783 784 @app.template_global()785 def double(n):786 return 2 * n787 788+ The decorator also can be used without parentheses::789+790+ @app.template_global791+ def double(n):792+ return 2 * n793+794 .. versionadded:: 0.10795 796 :param name: the optional name of the global function, otherwise the797 function name will be used.798 """799 800+ if callable(name):801+ # If name is callable, it is the function to register.802+ # This is a shortcut for the common case of803+ # @app.template_global804+ # def func():805+806+ func = name807+ self.add_template_global(func)808+ return func809+810 def decorator(f: T_template_global) -> T_template_global:811 self.add_template_global(f, name=name)812 return f813 203 unmodified linesmodified:src/flask/sansio/blueprints.py+35 added-6 removed
440 unmodified lines441 )442 443 @setupmethod444 def app_template_filter(445- self, name: str | None = None446- ) -> t.Callable[[T_template_filter], T_template_filter]:445+ self, name: t.Callable[..., t.Any] | str | None = None446+ ) -> t.Callable[[T_template_filter], T_template_filter] | T_template_filter:447 """Register a template filter, available in any template rendered by the448 application. Equivalent to :meth:`.Flask.template_filter`.449 450 :param name: the optional name of the filter, otherwise the451 function name will be used.452 """453 454+ if callable(name):455+ # If name is callable, it is the function to register.456+ # This is a shortcut for the common case of457+ # @bp.add_template_filter458+ # def func():459+460+ func = name461+ self.add_app_template_filter(func)462+ return func463+464 def decorator(f: T_template_filter) -> T_template_filter:465 self.add_app_template_filter(f, name=name)466 return f467 17 unmodified lines485 self.record_once(register_template)486 487 @setupmethod488 def app_template_test(479- self, name: str | None = None480- ) -> t.Callable[[T_template_test], T_template_test]:489+ self, name: t.Callable[..., t.Any] | str | None = None490+ ) -> t.Callable[[T_template_test], T_template_test] | T_template_filter:491 """Register a template test, available in any template rendered by the492 application. Equivalent to :meth:`.Flask.template_test`.493 494 .. versionadded:: 0.101 unmodified line496 :param name: the optional name of the test, otherwise the497 function name will be used.498 """499 500+ if callable(name):501+ # If name is callable, it is the function to register.502+ # This is a shortcut for the common case of503+ # @bp.add_template_test504+ # def func():505+506+ func = name507+ self.add_app_template_test(func)508+ return func509+510 def decorator(f: T_template_test) -> T_template_test:511 self.add_app_template_test(f, name=name)512 return f513 19 unmodified lines533 self.record_once(register_template)534 535 @setupmethod536 def app_template_global(517- self, name: str | None = None518- ) -> t.Callable[[T_template_global], T_template_global]:537+ self, name: t.Callable[..., t.Any] | str | None = None538+ ) -> t.Callable[[T_template_global], T_template_global] | T_template_filter:539 """Register a template global, available in any template rendered by the540 application. Equivalent to :meth:`.Flask.template_global`.541 542 .. versionadded:: 0.10543 544 :param name: the optional name of the global, otherwise the545 function name will be used.546 """547+ if callable(name):548+ # If name is callable, it is the function to register.549+ # This is a shortcut for the common case of550+ # @bp.add_template_global551+ # def func():552 553+ func = name554+ self.add_app_template_global(func)555+ return func556+557 def decorator(f: T_template_global) -> T_template_global:558 self.add_app_template_global(f, name=name)559 return f560 101 unmodified linesmodified:tests/test_blueprints.py+81 added
364 unmodified lines365 @bp.app_template_filter()366 def my_reverse(s):367 return s[::-1]368 369+ @bp.app_template_filter370+ def my_reverse_2(s):371+ return s[::-1]372+373+ @bp.app_template_filter("my_reverse_custom_name_3")374+ def my_reverse_3(s):375+ return s[::-1]376+377+ @bp.app_template_filter(name="my_reverse_custom_name_4")378+ def my_reverse_4(s):379+ return s[::-1]380+381 app.register_blueprint(bp, url_prefix="/py")382 assert "my_reverse" in app.jinja_env.filters.keys()383 assert app.jinja_env.filters["my_reverse"] == my_reverse384 assert app.jinja_env.filters["my_reverse"]("abcd") == "dcba"385 386+ assert "my_reverse_2" in app.jinja_env.filters.keys()387+ assert app.jinja_env.filters["my_reverse_2"] == my_reverse_2388+ assert app.jinja_env.filters["my_reverse_2"]("abcd") == "dcba"389 390+ assert "my_reverse_custom_name_3" in app.jinja_env.filters.keys()391+ assert app.jinja_env.filters["my_reverse_custom_name_3"] == my_reverse_3392+ assert app.jinja_env.filters["my_reverse_custom_name_3"]("abcd") == "dcba"393+394+ assert "my_reverse_custom_name_4" in app.jinja_env.filters.keys()395+ assert app.jinja_env.filters["my_reverse_custom_name_4"] == my_reverse_4396+ assert app.jinja_env.filters["my_reverse_custom_name_4"]("abcd") == "dcba"397+398+399 def test_add_template_filter(app):400 bp = flask.Blueprint("bp", __name__)401 402 def my_reverse(s):122 unmodified lines525 @bp.app_template_test()526 def is_boolean(value):527 return isinstance(value, bool)528 529+ @bp.app_template_test530+ def boolean_2(value):531+ return isinstance(value, bool)532+533+ @bp.app_template_test("my_boolean_custom_name")534+ def boolean_3(value):535+ return isinstance(value, bool)536+537+ @bp.app_template_test(name="my_boolean_custom_name_2")538+ def boolean_4(value):539+ return isinstance(value, bool)540+541 app.register_blueprint(bp, url_prefix="/py")542 assert "is_boolean" in app.jinja_env.tests.keys()543 assert app.jinja_env.tests["is_boolean"] == is_boolean544 assert app.jinja_env.tests["is_boolean"](False)545 546+ assert "boolean_2" in app.jinja_env.tests.keys()547+ assert app.jinja_env.tests["boolean_2"] == boolean_2548+ assert app.jinja_env.tests["boolean_2"](False)549 550+ assert "my_boolean_custom_name" in app.jinja_env.tests.keys()551+ assert app.jinja_env.tests["my_boolean_custom_name"] == boolean_3552+ assert app.jinja_env.tests["my_boolean_custom_name"](False)553+554+ assert "my_boolean_custom_name_2" in app.jinja_env.tests.keys()555+ assert app.jinja_env.tests["my_boolean_custom_name_2"] == boolean_4556+ assert app.jinja_env.tests["my_boolean_custom_name_2"](False)557+558+559 def test_add_template_test(app):560 bp = flask.Blueprint("bp", __name__)561 562 def is_boolean(value):163 unmodified lines726 @bp.app_template_global()727 def get_answer():728 return 42729 730+ @bp.app_template_global731+ def get_stuff_1():732+ return "get_stuff_1"733+734+ @bp.app_template_global("my_get_stuff_custom_name_2")735+ def get_stuff_2():736+ return "get_stuff_2"737+738+ @bp.app_template_global(name="my_get_stuff_custom_name_3")739+ def get_stuff_3():740+ return "get_stuff_3"741+742 # Make sure the function is not in the jinja_env already743 assert "get_answer" not in app.jinja_env.globals.keys()744 app.register_blueprint(bp)745 1 unmodified line747 assert "get_answer" in app.jinja_env.globals.keys()748 assert app.jinja_env.globals["get_answer"] is get_answer749 assert app.jinja_env.globals["get_answer"]() == 42750 751+ assert "get_stuff_1" in app.jinja_env.globals.keys()752+ assert app.jinja_env.globals["get_stuff_1"] == get_stuff_1753+ assert app.jinja_env.globals["get_stuff_1"](), "get_stuff_1"754+755+ assert "my_get_stuff_custom_name_2" in app.jinja_env.globals.keys()756+ assert app.jinja_env.globals["my_get_stuff_custom_name_2"] == get_stuff_2757+ assert app.jinja_env.globals["my_get_stuff_custom_name_2"](), "get_stuff_2"758+759+ assert "my_get_stuff_custom_name_3" in app.jinja_env.globals.keys()760+ assert app.jinja_env.globals["my_get_stuff_custom_name_3"] == get_stuff_3761+ assert app.jinja_env.globals["my_get_stuff_custom_name_3"](), "get_stuff_3"762+763 with app.app_context():764 rv = flask.render_template_string("{{ get_answer() }}")765 assert rv == "42"766 767+ rv = flask.render_template_string("{{ get_stuff_1() }}")768+ assert rv == "get_stuff_1"769 770+ rv = flask.render_template_string("{{ my_get_stuff_custom_name_2() }}")771+ assert rv == "get_stuff_2"772+773+ rv = flask.render_template_string("{{ my_get_stuff_custom_name_3() }}")774+ assert rv == "get_stuff_3"775+776+777 def test_request_processing(app, client):778 bp = flask.Blueprint("bp", __name__)779 evts = []780 347 unmodified linesmodified:tests/test_templating.py+81 added
127 unmodified lines128 assert "my_reverse" in app.jinja_env.filters.keys()129 assert app.jinja_env.filters["my_reverse"] == my_reverse130 assert app.jinja_env.filters["my_reverse"]("abcd") == "dcba"131 132+ @app.template_filter133+ def my_reverse_2(s):134+ return s[::-1]135 136+ assert "my_reverse_2" in app.jinja_env.filters.keys()137+ assert app.jinja_env.filters["my_reverse_2"] == my_reverse_2138+ assert app.jinja_env.filters["my_reverse_2"]("abcd") == "dcba"139+140+ @app.template_filter("my_reverse_custom_name_3")141+ def my_reverse_3(s):142+ return s[::-1]143+144+ assert "my_reverse_custom_name_3" in app.jinja_env.filters.keys()145+ assert app.jinja_env.filters["my_reverse_custom_name_3"] == my_reverse_3146+ assert app.jinja_env.filters["my_reverse_custom_name_3"]("abcd") == "dcba"147+148+ @app.template_filter(name="my_reverse_custom_name_4")149+ def my_reverse_4(s):150+ return s[::-1]151+152+ assert "my_reverse_custom_name_4" in app.jinja_env.filters.keys()153+ assert app.jinja_env.filters["my_reverse_custom_name_4"] == my_reverse_4154+ assert app.jinja_env.filters["my_reverse_custom_name_4"]("abcd") == "dcba"155+156+157 def test_add_template_filter(app):158 def my_reverse(s):159 return s[::-1]160 85 unmodified lines246 assert "boolean" in app.jinja_env.tests.keys()247 assert app.jinja_env.tests["boolean"] == boolean248 assert app.jinja_env.tests["boolean"](False)249 250+ @app.template_test251+ def boolean_2(value):252+ return isinstance(value, bool)253 254+ assert "boolean_2" in app.jinja_env.tests.keys()255+ assert app.jinja_env.tests["boolean_2"] == boolean_2256+ assert app.jinja_env.tests["boolean_2"](False)257+258+ @app.template_test("my_boolean_custom_name")259+ def boolean_3(value):260+ return isinstance(value, bool)261+262+ assert "my_boolean_custom_name" in app.jinja_env.tests.keys()263+ assert app.jinja_env.tests["my_boolean_custom_name"] == boolean_3264+ assert app.jinja_env.tests["my_boolean_custom_name"](False)265+266+ @app.template_test(name="my_boolean_custom_name_2")267+ def boolean_4(value):268+ return isinstance(value, bool)269+270+ assert "my_boolean_custom_name_2" in app.jinja_env.tests.keys()271+ assert app.jinja_env.tests["my_boolean_custom_name_2"] == boolean_4272+ assert app.jinja_env.tests["my_boolean_custom_name_2"](False)273+274+275 def test_add_template_test(app):276 def boolean(value):277 return isinstance(value, bool)278 88 unmodified lines367 368 rv = flask.render_template_string("{{ get_stuff() }}")369 assert rv == "42"370 371+ @app.template_global372+ def get_stuff_1():373+ return "get_stuff_1"374 375+ assert "get_stuff_1" in app.jinja_env.globals.keys()376+ assert app.jinja_env.globals["get_stuff_1"] == get_stuff_1377+ assert app.jinja_env.globals["get_stuff_1"](), "get_stuff_1"378+379+ rv = flask.render_template_string("{{ get_stuff_1() }}")380+ assert rv == "get_stuff_1"381+382+ @app.template_global("my_get_stuff_custom_name_2")383+ def get_stuff_2():384+ return "get_stuff_2"385+386+ assert "my_get_stuff_custom_name_2" in app.jinja_env.globals.keys()387+ assert app.jinja_env.globals["my_get_stuff_custom_name_2"] == get_stuff_2388+ assert app.jinja_env.globals["my_get_stuff_custom_name_2"](), "get_stuff_2"389+390+ rv = flask.render_template_string("{{ my_get_stuff_custom_name_2() }}")391+ assert rv == "get_stuff_2"392+393+ @app.template_global(name="my_get_stuff_custom_name_3")394+ def get_stuff_3():395+ return "get_stuff_3"396+397+ assert "my_get_stuff_custom_name_3" in app.jinja_env.globals.keys()398+ assert app.jinja_env.globals["my_get_stuff_custom_name_3"] == get_stuff_3399+ assert app.jinja_env.globals["my_get_stuff_custom_name_3"](), "get_stuff_3"400+401+ rv = flask.render_template_string("{{ my_get_stuff_custom_name_3() }}")402+ assert rv == "get_stuff_3"403+404+405 def test_custom_template_loader(client):406 class MyFlask(flask.Flask):407 def create_global_jinja_loader(self):408 from jinja2 import DictLoader124 unmodified linesmodified:CHANGES.rst+2 added
3 unmodified lines4 Unreleased5 6 - Drop support for Python 3.9. :pr:`5730`7 - Remove previously deprecated code: ``__version__``. :pr:`5648`8+- ``template_filter``, ``template_test``, and ``template_global`` decorators9+ can be used without parentheses. :issue:`5729`10 11 12 Version 3.1.113 -------------1611 unmodified lines