[issue10353] 2to3 and places argument in unitests assertAlmostEqual

Peter report at bugs.python.org
Mon Nov 8 12:03:43 CET 2010


New submission from Peter <p.j.a.cock at googlemail.com>:

Consider the following example unit test using assertAlmostEqual which uses the places argument as a positional argument, call this places.py:

import unittest
class Tests(unittest.TestCase):
    def test_equal_to_five_decimal_places(self):
        """Check assertAlmostEqual using decimal places"""
        self.assertAlmostEqual(0.123456789, 0.123456, 5)
if __name__ == "__main__":
    runner = unittest.TextTestRunner(verbosity = 2)
    unittest.main(testRunner=runner)



This works fine with Python 2.6.6 (Linux),

$ python2.6 places.py
Check assertAlmostEqual using decimal places ... ok

----------------------------------------------------------------------
Ran 1 test in 0.000s

OK




Trying 2to3 on it reports no changes needed:

$ 2to3 places.py
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: No changes to places.py
RefactoringTool: Files that need to be modified:
RefactoringTool: places.py


Trying the test as is under Python 3.1.2 (Linux) fails:

$ python3.1 places.py
test_equal_to_five_decimal_places (__main__.Tests)
Check assertAlmostEqual using decimal places ... ERROR

======================================================================
ERROR: test_equal_to_five_decimal_places (__main__.Tests)
Check assertAlmostEqual using decimal places
----------------------------------------------------------------------
Traceback (most recent call last):
  File "places.py", line 5, in test_equal_to_five_decimal_places
    self.assertAlmostEqual(0.123456789, 0.123456, 5)
TypeError: assertAlmostEqual() takes exactly 3 positional arguments (4 given)

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)


The test can be fixed to run on Python 3.1 (any Python 2.6) by supplying the places argument by name:


import unittest
class Tests(unittest.TestCase):
    def test_equal_to_five_decimal_places(self):
        """Check assertAlmostEqual using decimal places"""
        self.assertAlmostEqual(0.123456789, 0.123456, places=5)
if __name__ == "__main__":
    runner = unittest.TextTestRunner(verbosity = 2)
    unittest.main(testRunner=runner)


Note http://docs.python.org/library/unittest.html does not explicitly discuss passing places by name vs position. On the other hand, http://docs.python.org/py3k/library/unittest.html explicitly shows the by name form only.

I think the 2to3 script needs to be extended to use the places argument by name.

----------
components: 2to3 (2.x to 3.0 conversion tool)
messages: 120728
nosy: maubp
priority: normal
severity: normal
status: open
title: 2to3 and places argument in unitests assertAlmostEqual
type: behavior
versions: Python 2.6, Python 2.7, Python 3.1, Python 3.2, Python 3.3

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10353>
_______________________________________


More information about the Python-bugs-list mailing list