I am using Python 2.7. (Switching to Python 3 for this particular code is not an option, please don't suggest it.) I am writing unit tests for some code.
Here is the relevant piece of code:
class SingleLineGrooveTable:
VEFMT = '.3f'
@classmethod
def formatve(cls, value, error=None):
er = 0
if error is not None:
er = error
v = value
elif len(value) > 1:
v, er = value
else:
v = value
return format(v, cls.VEFMT), format(er, cls.VEFMT)
and my test is:
import unittest
class TestSingleLineGrooveTable(unittest.TestCase):
def test_formatve_no_error(self):
e_v = '3.142'
e_er = '0.000'
r_v, r_er = SingleLineGrooveTable.formatve([3.1423])
self.assertEqual(e_v, r_v)
self.assertEqual(e_er, r_er)
(Yes, I know it's funny I'm getting an error on the test with "no_error" in the name...)
When I run the test, it throws ValueError: Unknown format code 'f' for object of type 'str'
on the return statement for the function. But I can't figure out where it's getting a str from. Possibly relevant, this code and the code I have that uses it were copied pretty much wholesale from someone else's code (who I can no longer contact), so maybe I'm calling it in the wrong way, but still, that's a list, not a string!
What is going on here? How do I fix this?
def formatve
that takes two arguments (and a third optional one) but then your code seems to call it with a single argument, so this should be throwingTypeError: formatve() missing 1 required positional argument: 'value'
. Also, there are no f-strings in your code, so are you sure about the f-string tag on your post?v = value
should bev = value[0]
. The error stems from trying to format a list as afloat
.[3.1423].__format__('.3f')
is less important than making the intended call of3.1423.__format__('.3f')
.