"assert_almost_equal" does not fail when comparing a number and an empty list
See original GitHub issueThe behavior of numpy.testing.assert_almost_equal
seems incorrect when one of the arguments is an empty list.
The following cases behave as I expect:
# Compare an empty list to a list containing the number 2. These are not almost equal, so an AssertionError is correctly raised.
>>> from numpy.testing import assert_almost_equal
>>> assert_almost_equal([], [2])
AssertionError:
Arrays are not almost equal to 7 decimals
(shapes (0,), (1,) mismatch)
x: array([], dtype=float64)
y: array([2])
# Compare the integer 2 to a list containing only 2. Since the arguments are treated as 'array-like', this should not raise an exception (and doesn't).
>>> from numpy.testing import assert_almost_equal
>>> assert_almost_equal(2, [2])
>>>
When one of the arguments is an empty list and the other is a number, they are presumably not almost equal. Nonetheless:
>>> from numpy.testing import assert_almost_equal
>>> assert_almost_equal([], 2) # no AssertionError?
>>>
The same behavior can be observed in numpy.testing.assert_allclose
.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Error with unit testing using selfassertAlmostEqual
assertAlmostEqual subtracts the two numbers it is comparing and asserts that ... There's no built-in assertion to compare your two lists and ...
Read more >unittest — Unit testing framework — Python 3.11.1 ...
The unittest unit testing framework was originally inspired by JUnit and has a ... check that s.split fails when the separator is not...
Read more >Python unittest - assertAlmostEqual() function - GeeksforGeeks
This function check that first and second are approximately equal by computing the difference, rounding to the given number of decimal places ( ......
Read more >The Complete List Of Python Assert Statements
assert type(5) is int # Success Example assert type(5) is not int # Fail ... list python list is True because at least...
Read more >tfds.testing.SubTestCase | TensorFlow Datasets
AssertionError, if any of the elements do not fall into expected_set . ... given number of decimal places (default 7) and comparing to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think the behaviour is quite obviously not intended and would argue for ensuring that comparing arrays with size 0 and size non-zero always leads to failure. Since this problem arises because we’re already special-casing scalars (which is useful), one might as well do something like:
I’ve just bumped into this today, or at least I think this is the same issue. Here are some more examples of surprising behaviour with empty lists/zero-sized arrays:
I think I understand why these happen, but they don’t necessary feel correct. The documentation seems vague on this too.