<div dir="ltr">Hey,<div><br></div><div>The way I&#39;d solve this problem is by replacing `is False` with `is numpy.False_`, I think this has a few nice advantages:</div><div><br></div><div>* Makes pep8/related tooling happy (Obey your tools!)</div>
<div>* Is readable for the next person, even self documenting you might say</div><div>* Doesn&#39;t require a ton of thought :-)</div><div><br></div><div>Cheers,</div><div>Alex</div></div><div class="gmail_extra"><br><br>
<div class="gmail_quote">On Thu, Jun 19, 2014 at 10:08 PM, Brianna Laugher <span dir="ltr">&lt;<a href="mailto:brianna.laugher@gmail.com" target="_blank">brianna.laugher@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">...That last example should of course be<div class=""><br><br>@py.test.mark.parametrize((&#39;vals&#39;, &#39;expected&#39;), [<br>    ([3, 4, 5], False),<br>    ([5, 6, 7], True),<br>    ])<br>def test_foo4(vals, expected):<br>

    a = foo(vals)<br></div>    assert a is expected<br><br><br></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On 20 June 2014 15:06, Brianna Laugher <span dir="ltr">&lt;<a href="mailto:brianna.laugher@gmail.com" target="_blank">brianna.laugher@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Hello,<br><br></div>Has anyone encountered the problem that a numpy bool False &#39;is&#39; not a &#39;bool&#39; False?<br>

<div><br>import py<br>import numpy as np<br><br>def foo(vals):<br>    mean = np.mean(vals)<br>
    return mean &gt; 5<br><br>def test_foo():<br>    a = foo([3, 4, 5])<br>    assert a is False<br><br>def test_foo2():<br>    a = foo([3, 4, 5])<br>    assert not a<br><br>def test_foo3():<br>    a = foo([3, 4, 5])<br>

    assert a == False<br>
<br><br></div><div>test_foo fails, test_foo2 and test_foo3 pass. And I get the lovely mystifying traceback <br></div><div><br>Traceback (most recent call last):<br>  File &quot;/workspace/test_foo.py&quot;, line 12, in test_foo<br>


    assert a is False<br>AssertionError: assert False is False<br><br><div>However, pep8 doesn&#39;t like the style of foo3:<br><br> E712 comparison to False should be &#39;if cond is False:&#39; or &#39;if not cond:&#39;<br>


<br></div><div>The style of foo2 is usually fine to use, but sometimes I have tests where I want to be more explicit, e.g.<br><br></div><div>@py.test.mark.parametrize((&#39;vals&#39;, &#39;expected&#39;), [<br>    ([3, 4, 5], False),<br>


    ([5, 6, 7], True),<br>    ])<br>def test_foo4(vals, expected):<br>    a = foo(vals)<br>    assert a is False<br><br><br></div><div>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 &#39;if x is True/False&#39; formulations but it could cause quite subtle bugs. <br>


</div><div>Just wondering if anyone has advice here.<br></div><div><br></div><div>thanks,<br>Brianna<span><font color="#888888"><br></font></span></div><span><font color="#888888"><div><br>-- <br>
They&#39;ve just been waiting in a mountain for the right moment: <a href="http://modernthings.org/" target="_blank">http://modernthings.org/</a>
</div></font></span></div></div>
</blockquote></div><br><br clear="all"><br>-- <br>They&#39;ve just been waiting in a mountain for the right moment: <a href="http://modernthings.org/" target="_blank">http://modernthings.org/</a>
</div>
</div></div><br>_______________________________________________<br>
testing-in-python mailing list<br>
<a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a><br>
<a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div dir="ltr">&quot;I disapprove of what you say, but I will defend to the death your right to say it.&quot; -- Evelyn Beatrice Hall (summarizing Voltaire)<br>
&quot;The people&#39;s good is the highest law.&quot; -- Cicero<br><div>GPG Key fingerprint: 125F 5C67 DFE9 4084</div></div>
</div>