[TIP] assertions with numpy bool and pep8 advice

Alex Gaynor alex.gaynor at gmail.com
Thu Jun 19 22:13:44 PDT 2014


Hey,

The way I'd solve this problem is by replacing `is False` with `is
numpy.False_`, I think this has a few nice advantages:

* Makes pep8/related tooling happy (Obey your tools!)
* Is readable for the next person, even self documenting you might say
* Doesn't require a ton of thought :-)

Cheers,
Alex


On Thu, Jun 19, 2014 at 10:08 PM, Brianna Laugher <brianna.laugher at gmail.com
> wrote:

> ...That last example should of course be
>
>
> @py.test.mark.parametrize(('vals', 'expected'), [
>     ([3, 4, 5], False),
>     ([5, 6, 7], True),
>     ])
> def test_foo4(vals, expected):
>     a = foo(vals)
>     assert a is expected
>
>
>
>
> On 20 June 2014 15:06, Brianna Laugher <brianna.laugher at gmail.com> wrote:
>
>> Hello,
>>
>> Has anyone encountered the problem that a numpy bool False 'is' not a
>> 'bool' False?
>>
>> import py
>> import numpy as np
>>
>> def foo(vals):
>>     mean = np.mean(vals)
>>     return mean > 5
>>
>> def test_foo():
>>     a = foo([3, 4, 5])
>>     assert a is False
>>
>> def test_foo2():
>>     a = foo([3, 4, 5])
>>     assert not a
>>
>> def test_foo3():
>>     a = foo([3, 4, 5])
>>     assert a == False
>>
>>
>> test_foo fails, test_foo2 and test_foo3 pass. And I get the lovely
>> mystifying traceback
>>
>> Traceback (most recent call last):
>>   File "/workspace/test_foo.py", line 12, in test_foo
>>     assert a is False
>> AssertionError: assert False is False
>>
>> However, pep8 doesn't like the style of foo3:
>>
>>  E712 comparison to False should be 'if cond is False:' or 'if not cond:'
>>
>> The style of foo2 is usually fine to use, but sometimes I have tests
>> where I want to be more explicit, e.g.
>>
>> @py.test.mark.parametrize(('vals', 'expected'), [
>>     ([3, 4, 5], False),
>>     ([5, 6, 7], True),
>>     ])
>> def test_foo4(vals, expected):
>>     a = foo(vals)
>>     assert a is False
>>
>>
>> I was totally surprised to realise that I was getting back some numpy
>> bool object anyway and not just a regular False. It seems like a bit of a
>> gotcha, luckily our code base does not contain many 'if x is True/False'
>> formulations but it could cause quite subtle bugs.
>> Just wondering if anyone has advice here.
>>
>> thanks,
>> Brianna
>>
>> --
>> They've just been waiting in a mountain for the right moment:
>> http://modernthings.org/
>>
>
>
>
> --
> They've just been waiting in a mountain for the right moment:
> http://modernthings.org/
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>
>


-- 
"I disapprove of what you say, but I will defend to the death your right to
say it." -- Evelyn Beatrice Hall (summarizing Voltaire)
"The people's good is the highest law." -- Cicero
GPG Key fingerprint: 125F 5C67 DFE9 4084
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20140619/ac6ab96e/attachment-0001.htm>


More information about the testing-in-python mailing list