Opened 4 years ago

Last modified 4 years ago

#8075 defect new

assertEqual blows up if arguments cannot be pprinted

Reported by: Jonathan Lange Owned by:
Priority: normal Milestone:
Component: trial Keywords:
Cc: Jonathan Lange Branch:
Author:

Description

Trial blows up if it's given objects that cannot be pformatted. This masks the actual test failure. Instead, it should fall back to the repr of the given objects.

e.g.

import unittest
from twisted.trial.unittest import SynchronousTestCase

class DemoFailure(SynchronousTestCase):
    def test_blows_up(self):
        x = 2 + 3j
        y = 3 + 5j
        z = 5 + 7j
        self.assertEqual({x, y}, {y, z})

unittest.main()

Will fail with,

Traceback (most recent call last):
  File "/Users/jml/.virtualenvs/flocker/lib/python2.7/site-packages/twisted/trial/_synctest.py", line 1308, in _run
    runWithWarningsSuppressed(suppress, method)
  File "/Users/jml/.virtualenvs/flocker/lib/python2.7/site-packages/twisted/python/util.py", line 1021, in runWithWarningsSuppressed
    return f(*args, **kwargs)
  File "demo-trial-bug.py", line 11, in test_blows_up
    self.assertEqual({x, y}, {y, z})
  File "/Users/jml/.virtualenvs/flocker/lib/python2.7/site-packages/twisted/trial/_synctest.py", line 447, in assertEqual
    % (msg, pformat(first), pformat(second)))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pprint.py", line 63, in pformat
    return PrettyPrinter(indent=indent, width=width, depth=depth).pformat(object)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pprint.py", line 122, in pformat
    self._format(object, sio, 0, 0, {}, 0)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pprint.py", line 199, in _format
    object = _sorted(object)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pprint.py", line 82, in _sorted
    return sorted(iterable)
TypeError: no ordering relation is defined for complex numbers

Note that repr({x, y}) works just fine.

There is a parallel issue on pyrsistent for making their objects pformatable: https://github.com/tobgu/pyrsistent/issues/69

Change History (1)

comment:1 Changed 4 years ago by DefaultCC Plugin

Cc: Jonathan Lange added
Note: See TracTickets for help on using tickets.