[Feature request] Template language type annotations (#27703)

100 views
Skip to first unread message

Дмитрий Симонов

unread,
Jan 9, 2017, 10:45:11 AM1/9/17
to Django developers (Contributions to Django itself)
Hi guys.

Lets discuss about this ticket

I think we need create new tag in template language to annotate context variable types. Something like PEP 484 but on templates.
As in pep 484 main goal is provide easier static analysis, potential error checking and improve IDEs and editors support.
We can do this with new small tag. Something like:

@register.simpletag(name='var')
def type_hint(variable,variable_type_string: str) -> None:
   
pass

Then we can annotate variable in template:

{% var request 'django.http.HttpResponse' %} %}

or

{% var is_paginated 'bool' %}

or

{% var user_or_none 'typing.Union[None, django.contrib.auth.models.User]' %}

maybe

{% var user_or_none 'typing.Union[None, django.conf.settings.AUTH_USER_MODEL]' %}
 

Main idea is add this template to django default tags, not third part application. 
Then IDE, editor plugins, and analysis tools can build support, based on this.

Similar links:

English is not my mother tongue; please excuse any errors on my part.

Adam Johnson

unread,
Jan 9, 2017, 11:03:45 AM1/9/17
to django-d...@googlegroups.com
I think a third party implementation could be done first for the separate tag. I also think you'd learn more and iterate faster by building some tooling on top of the tags whilst a third party app before merging with core was considered. One idea that springs to mind is allowing multiple type declarations in the same tag rather than requiring "{% var " written multiple times.

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscribe@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/53df8783-7234-405e-a325-a8bed448cd35%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Adam

Дмитрий Симонов

unread,
Jan 9, 2017, 2:43:00 PM1/9/17
to django-d...@googlegroups.com

Martin Owens

unread,
Jan 10, 2017, 8:47:15 AM1/10/17
to Django developers (Contributions to Django itself)


On Monday, January 9, 2017 at 2:43:00 PM UTC-5, Дмитрий Симонов wrote:
Ok. Here is my example with multiline annotations.


What do you think?

It should probably avoid parsing of the main body, I don't believe that to be canonical with the rest of django's tags.

It might be better to parse each pair of variables from the {% var %} tag itself. Just allow that tag to take more than one pair and you should have all the support for single and multiple declarations.

The other issue I have is that the tag doesn't effect the output the template produces, so it's placement in templates will complicate the formatting for non-coding contributors. This might be worth it, but I can see the benefit of keeping these check lists outside of the template file itself too. Having a test suite pick up these issues is my preferred pattern, but anyway you develop it you'll have a very small amount of use from most projects.

Best Regards, Martin Owens
Inkscape Website Admin
Reply all
Reply to author
Forward
0 new messages