Opened 6 years ago

Closed 6 years ago

#25422 closed Bug (fixed)

Type annotations in template tag functions crash

Reported by: Thomas Orozco Owned by: nobody
Component: Template system Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Using function annotations in a function decorated with e.g. simple_tag throws an error:

======================================================================
ERROR: test_annotated_function (template_tests.test_custom.TemplateTagLoadingTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/thomas/dev/django/tests/template_tests/test_custom.py", line 446, in test_annotated_function
    Template(ttext)
  File "/Users/thomas/dev/django/django/template/base.py", line 190, in __init__
    self.nodelist = engine.compile_string(template_string, origin)
  File "/Users/thomas/dev/django/django/template/engine.py", line 261, in compile_string
    return parser.parse()
  File "/Users/thomas/dev/django/django/template/base.py", line 341, in parse
    compiled_result = compile_func(self, token)
  File "/Users/thomas/dev/django/django/template/defaulttags.py", line 1159, in load
    lib = get_library(taglib)
  File "/Users/thomas/dev/django/django/template/base.py", line 1392, in get_library
    lib = import_library(taglib_module)
  File "/Users/thomas/dev/django/django/template/base.py", line 1331, in import_library
    mod = import_module(taglib_module)
  File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 662, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/Users/thomas/dev/django/tests/template_tests/templatetags/annotated_tag_function.py", line 7, in <module>
    def annotated_tag_function(val: int):
  File "/Users/thomas/dev/django/django/template/base.py", line 1189, in dec
    params, varargs, varkw, defaults = getargspec(func)
  File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/inspect.py", line 1044, in getargspec
    raise ValueError("Function has keyword-only arguments or annotations"
ValueError: Function has keyword-only arguments or annotations, use getfullargspec() API which can support them

I have prepared a failing test case (and will file it in Github after creating this issue). It looks like this is fixed in 1.9 due to https://code.djangoproject.com/ticket/24979

Backporting those changes would probably fix the issue, but if that's not your preferred solution, I'm happy to work on an alternate patch.

Cheers,

Change History (7)

comment:1 Changed 6 years ago by Tim Graham

Resolution: wontfix
Status: newclosed
Summary: Annotations in tag functions crashType annotations in template tag functions crash
Type: UncategorizedBug

Sorry, but Django 1.8 won't be getting Python 3.5 support.

comment:2 Changed 6 years ago by Thomas Orozco

As mentioned in the GitHub issue, this is not actually a Python 3.5 issue (please feel free to let me know what place is more convenient to discuss — here or Github).

Cheers,

comment:3 Changed 6 years ago by Collin Anderson

Hi Thomas, did this work without crashing on django 1.7?

comment:4 Changed 6 years ago by Tim Graham

Resolution: wontfix
Status: closednew

As Collin mentioned, if it's not a regression, then a fix probably doesn't qualify for a backport per our supported versions policy.

Anyway, we can merge the test to ensure it keeps working from 1.9 and on. Could you send the pull request against master?

comment:5 Changed 6 years ago by Thomas Orozco

Oops, I didn't realize this kind of fixes wouldn't qualify for older releases. My bad, apologies!

It does indeed *not* work on Django 1.7 either.

I sent over another PR targeting master (https://github.com/django/django/pull/5307), though by the looks of it (you transitioned to signature), I doubt there will be a regression!

Thanks for the quick response,

Cheers,

comment:6 Changed 6 years ago by Tim Graham <timograham@…>

In d49667e:

Refs #25422 -- Added a test for a template tag with type annotations.

This doesn't work in Django 1.8 but was fixed in Django 1.9 as a
side effect of another change.

comment:7 Changed 6 years ago by Tim Graham

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top