sbdchd / django-types

:doughnut: Type stubs for Django
MIT License
186 stars 62 forks source link

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

Closed noelleleigh closed 8 months ago

noelleleigh commented 8 months ago

Makes use of typing.ParamSpec in Library decorators to allow decorated functions to preserve their type information.

Example:

register = template.Library()

@register.filter
def plain_text_quote(value: str):
    """Prefix each line in value with "> " to "quote" it in plain-text email bodies."""
    return indent(value, "> ", lambda _: True)

result = plain_text_quote("foo")
result # <- inferred type is `str`

The docorators have complex relationships between their arguments and return types, so they are overloaded.

Implementation reference