Stricter return type annotations for template.Library (#541)

* Stricter return type annotations for template.Library

* Add some unit tests for the template library decorators
This commit is contained in:
Tim Martin
2021-01-20 23:11:02 +03:00
committed by GitHub
parent a8d8561d0e
commit a1334a70b9
2 changed files with 118 additions and 15 deletions
+22 -15
View File
@@ -1,4 +1,4 @@
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from typing import Any, Callable, Dict, List, Optional, Tuple, TypeVar, Union, overload
from django.template.base import FilterExpression, Parser, Origin, Token
from django.template.context import Context
@@ -8,31 +8,38 @@ from .base import Node, Template
class InvalidTemplateLibrary(Exception): ...
_C = TypeVar("_C", bound=Callable[..., Any])
class Library:
filters: Dict[str, Callable] = ...
tags: Dict[str, Callable] = ...
def __init__(self) -> None: ...
def tag(
self, name: Optional[Union[Callable, str]] = ..., compile_function: Optional[Union[Callable, str]] = ...
) -> Callable: ...
def tag_function(self, func: Callable) -> Callable: ...
def filter(
self,
name: Optional[Union[Callable, str]] = ...,
filter_func: Optional[Union[Callable, str]] = ...,
**flags: Any
) -> Callable: ...
def filter_function(self, func: Callable, **flags: Any) -> Callable: ...
@overload
def tag(self, name: _C) -> _C: ...
@overload
def tag(self, name: str, compile_function: _C) -> _C: ...
@overload
def tag(self, name: Optional[str] = ..., compile_function: None = ...) -> Callable[[_C], _C]: ...
def tag_function(self, func: _C) -> _C: ...
@overload
def filter(self, name: _C, filter_func: None = ..., **flags: Any) -> _C: ...
@overload
def filter(self, name: Optional[str], filter_func: _C, **flags: Any) -> _C: ...
@overload
def filter(self, name: Optional[str] = ..., filter_func: None = ..., **flags: Any) -> Callable[[_C], _C]: ...
@overload
def simple_tag(self, func: _C) -> _C: ...
@overload
def simple_tag(
self, func: Optional[Union[Callable, str]] = ..., takes_context: Optional[bool] = ..., name: Optional[str] = ...
) -> Callable: ...
self, takes_context: Optional[bool] = ..., name: Optional[str] = ...
) -> Callable[[_C], _C]: ...
def inclusion_tag(
self,
filename: Union[Template, str],
func: None = ...,
takes_context: Optional[bool] = ...,
name: Optional[str] = ...,
) -> Callable: ...
) -> Callable[[_C], _C]: ...
class TagHelperNode(Node):
func: Any = ...