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

A numerical value of 0 is incorrectly treated as falsy #74

Closed
latimer1 opened this issue Sep 9, 2022 · 5 comments
Closed

A numerical value of 0 is incorrectly treated as falsy #74

latimer1 opened this issue Sep 9, 2022 · 5 comments
Labels
bug Something isn't working

Comments

@latimer1
Copy link

latimer1 commented Sep 9, 2022

Per https://shopify.github.io/liquid/basics/truthy-and-falsy/ "All values in Liquid are truthy except nil and false."

However, the integer 0 and float 0.0 are being treated as falsy.

Trying the following code input:

{% if 0.0 %} 0.0 float {% endif %}
{% if 0 %} 0 int {% endif %}
{% if '0' %} 0 string {% endif %}

Output:

0 string

If this is intended, I don't see this boolean behavior defined in known issues in https://jg-rp.github.io/liquid/known_issues

@latimer1
Copy link
Author

latimer1 commented Sep 9, 2022

The issue is https://github.com/jg-rp/liquid/blob/main/liquid/expression.py#L914

obj in (False, None) evaluates to true because 0.0 == False is True.

@jg-rp
Copy link
Owner

jg-rp commented Sep 9, 2022

Hi @latimer1,

I believe the case of {% if 0 %}... was fixed in version 1.4.2 with #65, but I hadn't considered {% if 0.0 %}..., so that's defiantly a bug that needs fixing.

Expected output from your example:

 0.0 float 
 0 int 
 0 string

Thanks for letting me know.

@jg-rp jg-rp added the bug Something isn't working label Sep 9, 2022
@latimer1
Copy link
Author

latimer1 commented Sep 9, 2022

I wonder if adding a type check would address any edge cases.

if type(obj) == type(False) and obj == False:
   return False
if type(obj) == type(None) and obj == None:
   return False

@jg-rp
Copy link
Owner

jg-rp commented Sep 9, 2022

There are a couple of cases that that does not cover. Undefined variables and drops that define __bool__ or __int__ methods can be falsy too.

This bug also extends to comparison operators and the default filter:

{% if 0.0 == false %}0.0 == false{% endif %}
{{ 0.0 | default: "hello" }}

Expected output


0.0

Actual output

0.0 == false 
hello

@jg-rp
Copy link
Owner

jg-rp commented Sep 10, 2022

Fixed in version 1.4.5, now released.

@jg-rp jg-rp closed this as completed Sep 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants