classification
Title: 2to3 and places argument in unitests assertAlmostEqual
Type: behavior Stage: resolved
Components: 2to3 (2.x to 3.x conversion tool) Versions: Python 3.1, Python 3.2
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, eric.araujo, maubp, michael.foord, r.david.murray
Priority: normal Keywords:

Created on 2010-11-08 11:03 by maubp, last changed 2010-11-08 15:12 by michael.foord. This issue is now closed.

Messages (4)
msg120728 - (view) Author: Peter (maubp) Date: 2010-11-08 11:03
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.
msg120745 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-11-08 14:51
The change of places to a keyword-only argument in 3.x was reverted last week in py3k trunk.
msg120751 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-11-08 15:10
Is 3.1 incompatible with 2.7 and 3.2?  If so, is it best to have 3.2 compatible with 3.1 or to have 3.1 buggy?
msg120752 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2010-11-08 15:12
It's fixed in release31-maint as well.
History
Date User Action Args
2010-11-08 15:12:55michael.foordsetmessages: + msg120752
2010-11-08 15:10:16eric.araujosetnosy: + eric.araujo
messages: + msg120751
2010-11-08 14:51:23r.david.murraysetstatus: open -> closed

versions: - Python 2.6, Python 2.7, Python 3.3
nosy: + r.david.murray, michael.foord

messages: + msg120745
resolution: out of date
stage: resolved
2010-11-08 14:48:00r.david.murraysetnosy: + benjamin.peterson
2010-11-08 11:03:41maubpcreate