jongracecox / anybadge

A Python project for generating badges for your projects, with a focus on simplicity and flexibility.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Integer printed as float

ahogen opened this issue · comments

While looking at fixed-widths branch for #24, I noticed that integer values seemed to get marked as float instead of int as intended. Since it's a simple fix, I didn't think it needed a full PR, but I can make one if you'd like.

diff --git a/anybadge.py b/anybadge.py
index 5cc9f04..5dd00b3 100755
--- a/anybadge.py
+++ b/anybadge.py
@@ -307,12 +307,11 @@ class Badge(object):

         Returns: type
         """
+        if self.value_is_int:
+            return int
         if self.value_is_float:
             return float
-        elif self.value_is_int:
-            return int
-        else:
-            return str
+        return str

     @property
     def label_width(self):

Test:

python ./anybadge.py -l "test" -v "1234" -f ./test.svg -o

Before:
before

After fix:
after

@ahogen Thanks for raising this issue, and the detail included. I added a unit test to ensure that the value_is_int and value_is_float functions are working, but actually the value_is_float function returns True for an integer string, which is technically wrong (or at least wrong for the purposes that we want to use the function for). I opted for updating the value_is_float function instead so that it returns False unless the value should be presented as a float on the badge value.

I only recently learned how many string-like things look like a float to Python. Among the values listed here, I found these rather interesting....

Command to parse                        Is it a float?  Comment
--------------------------------------  --------------- ------------
print(isfloat("NaN"))                   True            nan is also float
print(isfloat("6e777777"))              True            This is same as Inf
print(isfloat("-iNF"))                  True
print(isfloat("infinity"))              True
print(isfloat(True))                    True            Boolean is a float

I haven't tried, but I don't think any of these things will create any weird badge output currently. But it was something I thought about briefly when I found this issue.

Cheers 🙂