#35816: Django Template Language doesn't support all float literals
----------------------------+-----------------------------------------
Reporter: Lily Foote | Type: Uncategorized
Status: new | Component: Uncategorized
Version: 5.0 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+-----------------------------------------
When parsing arguments to filters, Django converts numeric literals to
`float` or `int`, but in the case of scientific notation for `float` this
is incomplete:
{{{
>>> from django.template.base import Template
>>> from django.template.engine import Engine
>>> 5.2e3
5200.0
>>> Template("{{ foo|default:5.2e3 }}", engine=e)
<Template template_string="{{ foo|default:5.2e3...">
>>> 5.2e-3
0.0052
>>> Template("{{ foo|default:5.2e-3 }}", engine=e)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/lily/.local/share/lilyenv/virtualenvs/django-rusty-
templates/3.12/lib/python3.12/site-packages/django/template/base.py", line
154, in __init__
self.nodelist = self.compile_nodelist()
^^^^^^^^^^^^^^^^^^^^^^^
File "/home/lily/.local/share/lilyenv/virtualenvs/django-rusty-
templates/3.12/lib/python3.12/site-packages/django/template/base.py", line
196, in compile_nodelist
nodelist = parser.parse()
^^^^^^^^^^^^^^
File "/home/lily/.local/share/lilyenv/virtualenvs/django-rusty-
templates/3.12/lib/python3.12/site-packages/django/template/base.py", line
489, in parse
raise self.error(token, e)
File "/home/lily/.local/share/lilyenv/virtualenvs/django-rusty-
templates/3.12/lib/python3.12/site-packages/django/template/base.py", line
487, in parse
filter_expression = self.compile_filter(token.contents)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/lily/.local/share/lilyenv/virtualenvs/django-rusty-
templates/3.12/lib/python3.12/site-packages/django/template/base.py", line
605, in compile_filter
return FilterExpression(token, self)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/lily/.local/share/lilyenv/virtualenvs/django-rusty-
templates/3.12/lib/python3.12/site-packages/django/template/base.py", line
706, in __init__
raise TemplateSyntaxError(
django.template.exceptions.TemplateSyntaxError: Could not parse the
remainder: '-3' from 'foo|default:5.2e-3'
}}}
I couldn't actually find the documentation for Django's handling of
numeric literals in templates, so this may be an undocumented
implementation detail. If so, perhaps deprecating scientific notation
would be appropriate. Otherwise, we should support this case.
--
Ticket URL: <
https://code.djangoproject.com/ticket/35816>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.