Q:

Numpy's logic functions and the "if cond is False" idiom

When applying PEP8's "if cond is False" idiom proposed by PyCharm with numpy's logic functions such as np.isinf or np.isnan, we get surprising behaviors.
>>> import numpy as np
... assert not np.isnan(0), "first test failed"
... assert np.isnan(0) is False, "second test failed"
AssertionError: second test failed

A:

The problem is that numpy's logic functions do not return a bool, but an instance of numpy.bool_, which is a different type. Thus,
>>> np.bool_(False) is False
False

One could think of using the dtype argument of those functions to force a bool output, but this won't work.
>>> type(np.isnan(0, dtype=bool))
numpy.bool_

A solution is to cast explicitely to bool:
>>> bool(np.isnan(0)) is False
True

Even better: never use the if cond is False idiom which can break when cond is not a boolean. Simply use not cond:
>>> not np.isnan(0)
True

Actually, I didn't find a recommandation for the if cond is False idiom in PEP8; it even says

Yes:   if greeting:
No:    if greeting == True:
Worse: if greeting is True:

I don't know why this idiom is proposed by PyCharm. Don't use it, I have been bitten by it and there is no reason you should too.

