Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

django-stubs/template/library.pyi Improvements #222

Merged
merged 6 commits into from
Jan 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 79 additions & 15 deletions django-stubs/template/library.pyi
Original file line number Diff line number Diff line change
@@ -1,46 +1,110 @@
from collections.abc import Callable
from typing import Any
from typing import Any, TypeVar, overload
from typing_extensions import ParamSpec

from django.template.base import FilterExpression, Origin, Parser, Token
from django.template.context import Context
from django.utils.safestring import SafeText

from .base import Node, Template

T = TypeVar("T")
P = ParamSpec("P")

class InvalidTemplateLibrary(Exception): ...

class Library:
filters: dict[str, Callable[..., Any]] = ...
tags: dict[str, Callable[..., Any]] = ...
def __init__(self) -> None: ...

# Both arguments None
@overload
def tag(
self,
name: None = ...,
compile_function: None = ...,
) -> Callable[[Callable[P, T]], Callable[P, T]]: ...
# Only name as function
@overload
def tag(
self,
name: Callable[P, T],
compile_function: None = ...,
) -> Callable[P, T]: ...
# Only name as string
@overload
def tag(
self,
name: Callable[..., Any] | str | None = ...,
compile_function: Callable[..., Any] | str | None = ...,
) -> Callable[..., Any]: ...
def tag_function(self, func: Callable[..., Any]) -> Callable[..., Any]: ...
name: str,
compile_function: None = ...,
) -> Callable[[Callable[P, T]], Callable[P, T]]: ...
# Both arguments specified
@overload
def tag(
self,
name: str,
compile_function: Callable[P, T],
) -> Callable[P, T]: ...
def tag_function(self, func: Callable[P, T]) -> Callable[P, T]: ...

# Both arguments None
@overload
def filter(
self,
name: None = ...,
filter_func: None = ...,
**flags: Any,
) -> Callable[[Callable[P, T]], Callable[P, T]]: ...
# Only name as string
@overload
def filter(
self,
name: Callable[..., Any] | str | None = ...,
filter_func: Callable[..., Any] | str | None = ...,
**flags: Any
) -> Callable[..., Any]: ...
def filter_function(
self, func: Callable[..., Any], **flags: Any
) -> Callable[..., Any]: ...
name: str = ...,
filter_func: None = ...,
**flags: Any,
) -> Callable[[Callable[P, T]], Callable[P, T]]: ...
# Only name as callable
@overload
def filter(
self,
name: Callable[P, T],
filter_func: None = ...,
**flags: Any,
) -> Callable[P, T]: ...
# Both arguments
@overload
def filter(
self,
name: str,
filter_func: Callable[P, T],
**flags: Any,
) -> Callable[P, T]: ...
def filter_function(self, func: Callable[P, T], **flags: Any) -> Callable[P, T]: ...

# func is None
@overload
def simple_tag(
self,
func: None = ...,
takes_context: bool | None = ...,
name: str | None = ...,
) -> Callable[[Callable[P, T]], Callable[P, T]]: ...
# func is callable
@overload
def simple_tag(
self,
func: Callable[..., Any] | str | None = ...,
func: Callable[P, T],
takes_context: bool | None = ...,
name: str | None = ...,
) -> Callable[..., Any]: ...
) -> Callable[P, T]: ...
def inclusion_tag(
self,
filename: Template | str,
func: None = ...,
takes_context: bool | None = ...,
name: str | None = ...,
) -> Callable[..., Any]: ...
) -> Callable[[Callable[P, T]], Callable[P, T]]: ...

class TagHelperNode(Node):
func: Any = ...
Expand Down