Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jinja filters converts none to string 'none' #2024

Closed
vladimir-vvalov opened this issue Sep 13, 2024 · 2 comments
Closed

jinja filters converts none to string 'none' #2024

vladimir-vvalov opened this issue Sep 13, 2024 · 2 comments

Comments

@vladimir-vvalov
Copy link

When argument value is None filters convert it to string 'None' then process it as string 'None'.
But if this behaviour was correct, the expression {{ if none == 'None' }} should return True. But returns False.
Also I didn't find any information about this behaviour in documentation:
https://jinja.palletsprojects.com/en/3.1.x/templates/

code:

--test
{%- set value = none %}
value: {{ value }}

--now value is none and isn't string
check value is none: {{ true if value is none else false }}
check value is string: {{ true if value is string else false }}

--example with trim and length - none converts to string 'None'
value_trim: {{ value|trim }}
value_trim_length: {{ (value|trim)|length }}
check value_trim is none: {{ true if value|trim is none else false }}
check value_trim is string: {{ true if value|trim is string else false }}

--example with replace - none converts to string 'None'
replace_value: {{ value|replace('n','x') }}
check value_replace is none: {{ true if value|replace('n','x') is none else false }}
check value_replace is string: {{ true if value|replace('n','x') is string else false }}

--check if none == 'None'
check if none == 'None': {{ true if none == 'None' else false }}

compiled code:

--test
value: None

--now value is none and isn't string
check value is none: True
check value is string: False

--example with trim and length - none converts to string 'None'
value_trim: None
value_trim_length: 4
check value_trim is none: False
check value_trim is string: True

--example with replace - none converts to string 'None'
replace_value: Noxe
check value_replace is none: False
check value_replace is string: True

--check if none == 'None'
check if none == 'None': False

expected behavior:
It was expected that if the argument is None, then the filter like trim() will also return None.

Thank you.

Environment:

  • Python version: 3.10.11
  • Jinja version: 3.1.3
@kevin-brown
Copy link
Member

I should note this appears to happen on filters that are specially geared towards strings. I'm not sure what you'd otherwise expect to happen when you ask to trim None but I'm confident multiple people would have differing opinions.

@davidism
Copy link
Member

Many filters operate on strings, and convert their value to string to do so. None remains its own value in other operations. You wouldn't expect None == "None" in Python either, although you might expect some Python function to convert its argument to string. I don't see anything wrong here.

@davidism davidism closed this as not planned Won't fix, can't repro, duplicate, stale Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants