diff options
| author | Jacob Walls <jacobtylerwalls@gmail.com> | 2025-11-29 18:45:39 -0500 |
|---|---|---|
| committer | Jacob Walls <jacobtylerwalls@gmail.com> | 2025-12-01 20:49:53 -0500 |
| commit | ec7327453d266d31a00060aeb5b9f19e5adfb0a4 (patch) | |
| tree | 991b15a8439d4d768ae6256e378b8e2036c2f51e /tests/template_tests | |
| parent | 2e3953f0a2de7bb702d393279d9dbbe2581a21a5 (diff) | |
[6.0.x] Fixed #36712 -- Evaluated type annotations lazily in template tag registration.
Ideally, this will be reverted when an upstream solution is available for
https://github.com/python/cpython/issues/141560.
Thanks Patrick Rauscher for the report and Augusto Pontes for the
first iteration and test.
Backport of 34186e731ca20a2344b1f88fd543a854d6b13a00 from main.
Diffstat (limited to 'tests/template_tests')
| -rw-r--r-- | tests/template_tests/test_library.py | 26 | ||||
| -rw-r--r-- | tests/template_tests/test_parser.py | 17 |
2 files changed, 43 insertions, 0 deletions
diff --git a/tests/template_tests/test_library.py b/tests/template_tests/test_library.py index 5827d33223..609b919923 100644 --- a/tests/template_tests/test_library.py +++ b/tests/template_tests/test_library.py @@ -1,8 +1,10 @@ import functools +import unittest from django.template import Library from django.template.base import Node from django.test import SimpleTestCase +from django.utils.version import PY314 class FilterRegistrationTests(SimpleTestCase): @@ -78,6 +80,14 @@ class InclusionTagRegistrationTests(SimpleTestCase): self.assertIs(func_wrapped, func) self.assertTrue(hasattr(func_wrapped, "cache_info")) + @unittest.skipUnless(PY314, "Deferred annotations are Python 3.14+ only") + def test_inclusion_tag_deferred_annotation(self): + @self.library.inclusion_tag("template.html") + def func(arg: SomeType): # NOQA: F821 + return "" + + self.assertIn("func", self.library.tags) + class SimpleTagRegistrationTests(SimpleTestCase): def setUp(self): @@ -104,6 +114,14 @@ class SimpleTagRegistrationTests(SimpleTestCase): self.assertIn("name", self.library.tags) + @unittest.skipUnless(PY314, "Deferred annotations are Python 3.14+ only") + def test_tag_deferred_annotation(self): + @self.library.simple_tag + def func(parser, token: SomeType): # NOQA: F821 + return Node() + + self.assertIn("func", self.library.tags) + def test_simple_tag_invalid(self): msg = "Invalid arguments provided to simple_tag" with self.assertRaisesMessage(ValueError, msg): @@ -145,6 +163,14 @@ class SimpleBlockTagRegistrationTests(SimpleTestCase): self.assertIn("name", self.library.tags) + @unittest.skipUnless(PY314, "Deferred annotations are Python 3.14+ only") + def test_simple_block_tag_deferred_annotation(self): + @self.library.simple_block_tag + def func(content: SomeType): # NOQA: F821 + return content + + self.assertIn("func", self.library.tags) + def test_simple_block_tag_invalid(self): msg = "Invalid arguments provided to simple_block_tag" with self.assertRaisesMessage(ValueError, msg): diff --git a/tests/template_tests/test_parser.py b/tests/template_tests/test_parser.py index 317fb88238..9d68955b54 100644 --- a/tests/template_tests/test_parser.py +++ b/tests/template_tests/test_parser.py @@ -3,6 +3,8 @@ Testing some internals of the template processing. These are *not* examples to be copied in user code. """ +import unittest + from django.template import Library, TemplateSyntaxError from django.template.base import ( FilterExpression, @@ -15,6 +17,7 @@ from django.template.base import ( ) from django.template.defaultfilters import register as filter_library from django.test import SimpleTestCase +from django.utils.version import PY314 class ParserTests(SimpleTestCase): @@ -240,3 +243,17 @@ class ParserTests(SimpleTestCase): FilterExpression(num, p).resolve({}) with self.assertRaises(TemplateSyntaxError): FilterExpression(f"0|default:{num}", p).resolve({}) + + @unittest.skipUnless(PY314, "Deferred annotations are Python 3.14+ only") + def test_filter_deferred_annotation(self): + register = Library() + + @register.filter("example") + def example_filter(value: str, arg: SomeType): # NOQA: F821 + return f"{value}_{arg}" + + result = FilterExpression.args_check( + "example", example_filter, ["extra_example"] + ) + + self.assertIs(result, True) |
