Skip to content

Commit ecfb756

Browse files
[5.2] Fix django.template.library (#2633)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 977c0e5 commit ecfb756

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed

django-stubs/template/library.pyi

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ from django.template.base import FilterExpression, Origin, Parser, Token
55
from django.template.context import Context
66
from django.utils.safestring import SafeString
77

8-
from .base import Node, Template
8+
from .base import Node, NodeList, Template
99

1010
class InvalidTemplateLibrary(Exception): ...
1111

@@ -28,17 +28,36 @@ class Library:
2828
def filter(self, name: str | None, filter_func: _C, **flags: Any) -> _C: ...
2929
@overload
3030
def filter(self, name: str | None = None, filter_func: None = None, **flags: Any) -> Callable[[_C], _C]: ...
31+
def filter_function(self, func: _C, **flags: Any) -> _C: ...
3132
@overload
3233
def simple_tag(self, func: _C, takes_context: bool | None = None, name: str | None = None) -> _C: ...
3334
@overload
34-
def simple_tag(self, takes_context: bool | None = None, name: str | None = None) -> Callable[[_C], _C]: ...
35+
def simple_tag(
36+
self, func: None = None, takes_context: bool | None = None, name: str | None = None
37+
) -> Callable[[_C], _C]: ...
3538
def inclusion_tag(
3639
self,
3740
filename: Template | str,
3841
func: Callable | None = None,
3942
takes_context: bool | None = None,
4043
name: str | None = None,
4144
) -> Callable[[_C], _C]: ...
45+
@overload
46+
def simple_block_tag(
47+
self,
48+
func: _C,
49+
takes_context: bool | None = None,
50+
name: str | None = None,
51+
end_name: str | None = None,
52+
) -> _C: ...
53+
@overload
54+
def simple_block_tag(
55+
self,
56+
func: None = None,
57+
takes_context: bool | None = None,
58+
name: str | None = None,
59+
end_name: str | None = None,
60+
) -> Callable[[_C], _C]: ...
4261

4362
class TagHelperNode(Node):
4463
func: Any
@@ -71,6 +90,19 @@ class SimpleNode(TagHelperNode):
7190
target_var: str | None,
7291
) -> None: ...
7392

93+
class SimpleBlockNode(SimpleNode):
94+
nodelist: NodeList
95+
96+
def __init__(
97+
self,
98+
nodelist: NodeList,
99+
func: Callable,
100+
takes_context: bool | None,
101+
args: list[FilterExpression],
102+
kwargs: dict[str, FilterExpression],
103+
target_var: str | None,
104+
) -> None: ...
105+
74106
class InclusionNode(TagHelperNode):
75107
args: list[FilterExpression]
76108
func: Callable

scripts/stubtest/allowlist_todo.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,8 +1132,6 @@ django.http.HttpRequest.__init__
11321132
django.http.StreamingHttpResponse.content
11331133
django.http.request.HttpRequest.__init__
11341134
django.http.response.StreamingHttpResponse.content
1135-
django.template.Library.filter_function
1136-
django.template.Library.simple_tag
11371135
django.template.Node.__iter__
11381136
django.template.VariableDoesNotExist.__init__
11391137
django.template.base.Context
@@ -1146,8 +1144,6 @@ django.template.defaulttags.do_ifequal
11461144
django.template.defaulttags.ifequal
11471145
django.template.defaulttags.ifnotequal
11481146
django.template.library.InclusionNode.token
1149-
django.template.library.Library.filter_function
1150-
django.template.library.Library.simple_tag
11511147
django.template.library.SimpleNode.token
11521148
django.template.loader_tags.BlockNode.token
11531149
django.template.loader_tags.ExtendsNode.token

scripts/stubtest/allowlist_todo_django52.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,5 @@ django.forms.models.ModelChoiceField.validate_no_null_characters
168168
django.forms.renderers.BaseRenderer.bound_field_class
169169
django.http.QueryDict.pop
170170
django.http.request.QueryDict.pop
171-
django.template.Library.simple_block_tag
172171
django.template.autoreload
173-
django.template.library.Library.simple_block_tag
174-
django.template.library.SimpleBlockNode
175172
django.test.runner.ParallelTestSuite.handle_event

tests/typecheck/template/test_library.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,29 @@
6767
6868
reveal_type(register.simple_tag(f, name='double')) # N: Revealed type is "def (s: builtins.str) -> builtins.str"
6969
70+
- case: register_simple_block_tag
71+
main: |
72+
import datetime
73+
from django import template
74+
register = template.Library()
75+
76+
def f(s: str) -> str:
77+
return s * 2
78+
79+
reveal_type(register.simple_block_tag(f, name='double')) # N: Revealed type is "def (s: builtins.str) -> builtins.str"
80+
81+
@register.simple_block_tag
82+
def current_time(format_string: str) -> str:
83+
return datetime.datetime.now().strftime(format_string)
84+
85+
reveal_type(current_time) # N: Revealed type is "def (format_string: builtins.str) -> builtins.str"
86+
87+
@register.simple_block_tag(name='minustwo')
88+
def some_function(value: int) -> int:
89+
return value - 2
90+
91+
reveal_type(some_function) # N: Revealed type is "def (value: builtins.int) -> builtins.int"
92+
7093
- case: register_tag_no_args
7194
main: |
7295
from django import template

0 commit comments

Comments
 (0)