From a75588087c02253e7baa01d569ec3e8cf0d8b038 Mon Sep 17 00:00:00 2001 From: Noelle Leigh <5957867+noelleleigh@users.noreply.github.com> Date: Thu, 4 Jan 2024 12:30:15 -0500 Subject: [PATCH 1/6] Library.tag: Describe overloads Includes generic callables. --- django-stubs/template/library.pyi | 36 +++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/django-stubs/template/library.pyi b/django-stubs/template/library.pyi index 99f2986ec..5b5f1c2ef 100644 --- a/django-stubs/template/library.pyi +++ b/django-stubs/template/library.pyi @@ -1,5 +1,6 @@ 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 @@ -7,17 +8,44 @@ 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: Callable[..., Any] | str | None = ..., - compile_function: Callable[..., Any] | str | None = ..., - ) -> Callable[..., Any]: ... + 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: 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[..., Any]) -> Callable[..., Any]: ... def filter( self, From 2ca642dac9f46766f78d2e844fe0c1efe5485ae7 Mon Sep 17 00:00:00 2001 From: Noelle Leigh <5957867+noelleleigh@users.noreply.github.com> Date: Thu, 4 Jan 2024 12:32:09 -0500 Subject: [PATCH 2/6] Library.tag_function: Use generic Callable --- django-stubs/template/library.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django-stubs/template/library.pyi b/django-stubs/template/library.pyi index 5b5f1c2ef..7b136ee9f 100644 --- a/django-stubs/template/library.pyi +++ b/django-stubs/template/library.pyi @@ -46,7 +46,7 @@ class Library: name: str, compile_function: Callable[P, T], ) -> Callable[P, T]: ... - def tag_function(self, func: Callable[..., Any]) -> Callable[..., Any]: ... + def tag_function(self, func: Callable[P, T]) -> Callable[P, T]: ... def filter( self, name: Callable[..., Any] | str | None = ..., From b44f28e9ee9643e5e6ebb9b6fffd93540587d1b5 Mon Sep 17 00:00:00 2001 From: Noelle Leigh <5957867+noelleleigh@users.noreply.github.com> Date: Thu, 4 Jan 2024 12:34:53 -0500 Subject: [PATCH 3/6] Library.filter: Describe overloads --- django-stubs/template/library.pyi | 35 +++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/django-stubs/template/library.pyi b/django-stubs/template/library.pyi index 7b136ee9f..da268d5c6 100644 --- a/django-stubs/template/library.pyi +++ b/django-stubs/template/library.pyi @@ -47,12 +47,39 @@ class Library: 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: Callable[..., Any] | str | None = ..., - filter_func: Callable[..., Any] | str | None = ..., - **flags: Any - ) -> Callable[..., Any]: ... + name: None = ..., + filter_func: None = ..., + **flags: Any, + ) -> Callable[[Callable[P, T]], Callable[P, T]]: ... + # Only name as string + @overload + def filter( + self, + 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[..., Any], **flags: Any ) -> Callable[..., Any]: ... From 2617d7bc1acb5615118ddde0125d5809a321ecb5 Mon Sep 17 00:00:00 2001 From: Noelle Leigh <5957867+noelleleigh@users.noreply.github.com> Date: Thu, 4 Jan 2024 12:35:23 -0500 Subject: [PATCH 4/6] Library.filter_function: Use generic Callable --- django-stubs/template/library.pyi | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/django-stubs/template/library.pyi b/django-stubs/template/library.pyi index da268d5c6..254f4e0d5 100644 --- a/django-stubs/template/library.pyi +++ b/django-stubs/template/library.pyi @@ -80,9 +80,7 @@ class Library: filter_func: Callable[P, T], **flags: Any, ) -> Callable[P, T]: ... - def filter_function( - self, func: Callable[..., Any], **flags: Any - ) -> Callable[..., Any]: ... + def filter_function(self, func: Callable[P, T], **flags: Any) -> Callable[P, T]: ... def simple_tag( self, func: Callable[..., Any] | str | None = ..., From 6347ab2ff8969b87275dc7568f20df86825666e8 Mon Sep 17 00:00:00 2001 From: Noelle Leigh <5957867+noelleleigh@users.noreply.github.com> Date: Thu, 4 Jan 2024 12:35:56 -0500 Subject: [PATCH 5/6] Library.simple_tag: Describe overloads --- django-stubs/template/library.pyi | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/django-stubs/template/library.pyi b/django-stubs/template/library.pyi index 254f4e0d5..47ad0da07 100644 --- a/django-stubs/template/library.pyi +++ b/django-stubs/template/library.pyi @@ -81,12 +81,23 @@ class Library: **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: Callable[..., Any] | str | None = ..., + func: None = ..., takes_context: bool | None = ..., name: str | None = ..., - ) -> Callable[..., Any]: ... + ) -> Callable[[Callable[P, T]], Callable[P, T]]: ... + # func is callable + @overload + def simple_tag( + self, + func: Callable[P, T], + takes_context: bool | None = ..., + name: str | None = ..., + ) -> Callable[P, T]: ... def inclusion_tag( self, filename: Template | str, From 25bc4193fb550ca789529217b7ab76e9dec3c165 Mon Sep 17 00:00:00 2001 From: Noelle Leigh <5957867+noelleleigh@users.noreply.github.com> Date: Thu, 4 Jan 2024 12:36:20 -0500 Subject: [PATCH 6/6] Library.inclusion_tag: Use generic Callable --- django-stubs/template/library.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django-stubs/template/library.pyi b/django-stubs/template/library.pyi index 47ad0da07..2b013026c 100644 --- a/django-stubs/template/library.pyi +++ b/django-stubs/template/library.pyi @@ -104,7 +104,7 @@ class Library: func: None = ..., takes_context: bool | None = ..., name: str | None = ..., - ) -> Callable[..., Any]: ... + ) -> Callable[[Callable[P, T]], Callable[P, T]]: ... class TagHelperNode(Node): func: Any = ...