Pytest Fails with AssertionError False is False

Viewed 1428

Since one day I am faceing a rally strange error when running my pytests, they fail with either

E    assert False is False
AssertionError
E    assert True is True
AssertionError

But from my understanding and when checking it in python terminal that should give the correct output:

Python 3.6.9 (default, Oct  8 2020, 12:12:24) 
>>> False is False
True

The outputs from pytest before running it

platform linux -- Python 3.6.8, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
plugins: cov-2.11.1, instafail-0.4.2, timeout-1.4.2

Does anyone have an idea what causes this behavior, because for me it makes no sense at all?

Important info

My question is not about the test itself it is about how can it happen, that pytest raises an explicit error telling me False is False is not True but False == False works without a problem?

This actually reproduces the issue:

from typing import Tuple
import numpy as np
import pytest

class MyObject():
    def function_to_text(self, input: list) -> Tuple[bool, float]:
        val = np.array(0) > 10
        return val, 1.0


@pytest.fixture(scope='session')
def my_object():
    my_obj = MyObject()
    yield my_obj


def test_function_rejects_input(my_object):

    assert my_object.function_to_text([])[0] is False

And by finding a way to reproduce this one now, I know now also what is the problem, see the answer for the solution

1 Answers

Although Pytest says that False is False the first one is not acually False it is a numpy object which resolves to false (<class 'numpy.bool_'>).

If you compare a numpy.bool with a 'standard' bool this resolves to False, because they are different object - different memory locations

The == instead compares the value of both operants (https://www.geeksforgeeks.org/difference-operator-python/) with is False for both and hence resolves to True.

The confusing thing in that case is that the pytest output does not really give you an hint into that direction, because it does not display the type of both and only shows the value of both, which indeed is False

Related