Opened 2 months ago

Last modified 2 months ago

#35816 assigned Bug

Django Template Language doesn't support all float literals

Reported by: Lily Foote Owned by: Aryaman
Component: Template system Version: 5.1
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (4)

comment:1 by Lily Foote, 2 months ago

Version: 5.05.1

comment:2 by Aryaman, 2 months ago

Owner: set to Aryaman
Status: newassigned

I am beginner in os , Giving this a try

comment:3 by Sarah Boyce, 2 months ago

Component: UncategorizedTemplate system
Triage Stage: UnreviewedAccepted

Thank you! We try to support scientific numbers so we should fix this.
I believe their is an issue with the regex, this might work

  • django/template/base.py

    diff --git a/django/template/base.py b/django/template/base.py
    index ee2e145c04..e0d032f688 100644
    a b filter_raw_string = r"""  
    640640         )?
    641641 )""" % {
    642642    "constant": constant_string,
    643     "num": r"[-+.]?\d[\d.e]*",
     643    "num": r"[-+.]?\d[\d.e-]*",
    644644    "var_chars": r"\w\.",
    645645    "filter_sep": re.escape(FILTER_SEPARATOR),

Will need a test

comment:4 by Sarah Boyce, 2 months ago

Type: UncategorizedBug
Note: See TracTickets for help on using tickets.
Back to Top