-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
bpo-30190: improved error msg for assertAlmostEqual(delta=...) #1331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…e difference between the 2 numbers in case of failure
@giampaolo, thanks for your PR! By analyzing the history of the files in this pull request, we identified @benjaminp, @voidspace and @serhiy-storchaka to be potential reviewers. |
Lib/unittest/case.py
Outdated
safe_repr(first), | ||
safe_repr(second), | ||
safe_repr(delta), | ||
diff) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing safe_repr() here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch, thanks
Lib/unittest/case.py
Outdated
@@ -857,22 +857,28 @@ def assertAlmostEqual(self, first, second, places=None, msg=None, | |||
raise TypeError("specify delta or places not both") | |||
|
|||
if delta is not None: | |||
if abs(first - second) <= delta: | |||
diff = abs(first - second) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is duplicated in both branches. It could be move above the if
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
standardMsg = '%s != %s within %r places' % (safe_repr(first), | ||
safe_repr(second), | ||
places) | ||
standardMsg = '%s != %s within %r places (%s difference)' % ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about assertNotAlmostEqual()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I'm understanding the code wrong but assertNotAlmostEqual
fails if the two numbers are almost equal and just "off" by a little. So in case of:
assertNotAlmostEqual(1.00000001, 1.0, places=7)
The failure will look like this:
AssertionError 1.00000001 == 1.0 within 7 places
...which is already enough. Makes sense?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How this differs from the case when delta is specified?
Actually I don't know what is the use case for assertNotAlmostEqual()
. It is not used in Python tests (except testing assertNotAlmostEqual()
itself). I suggest to not change it at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To my understanding delta
is entirely about acceptable difference (hence it makes sense to show the absolute diff) whereas places
is about decimal precision.
I'm also not sure what's the use case for assertNotAlmostEqual()
but whoever uses it will find this:
self.assertNotAlmostEqual(5, 10, delta=15)
...produces:
AssertionError: 5 == 10 within 15 delta (5 difference)
...more readable than the current message
So in summary, for assertNotAlmostEqual
, I'd be for showing the difference if delta
is specified but not if places
is specified.
OK to merge? |
Merging as per Raymond's approval. |
http://bugs.python.org/issue30190