question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"assert_almost_equal" does not fail when comparing a number and an empty list

See original GitHub issue

The 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:open
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
mhvkcommented, May 13, 2018

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:

cond = (x.shape == () and y.size > 0 or y.shape == () and x.size > 0) or x.shape == y.shape
0reactions
ZedThreecommented, Dec 15, 2021

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:

>>> np.all([] == [12])
False

>>> np.all(np.array([]) == [12])
True

>>> np.array_equal(np.array([]), [12])
False

>>> np.allclose(np.array([]), [12])
True

I think I understand why these happen, but they don’t necessary feel correct. The documentation seems vague on this too.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found