[Python-Dev] Re: unittest of sequence equality

2020-12-22 Thread David Mertz
On Tue, Dec 22, 2020 at 11:44 PM Alan G. Isaac  wrote:

> My question is not about work arounds.
> It is about whether the current definition of a sequence
> (in collections.abc) should govern `assertSequenceEqual`.
>

Why do you think a NumPy array is a sequence?

E.g.:

>>> a
array([[1, 2],
   [3, 4]])
>>> b
[1, 2]
>>> len(a) = len(b)

 Ok, I suppose if you then loop through indices, you indeed get different
things.  But N-dimensional arrays aren't "indexed by an integer" except in
the sense of an edge case.  I've always read the Glossary as more narrowly
"element access using ONLY integer indices."

Moreover, if you propose to ignore `==` comparison and always loop, will
you do so recursively?  What about:

assertSequenceEqual(a, [[1,2], [3,4]])

-- 
The dead increasingly dominate and strangle both the living and the
not-yet born.  Vampiric capital and undead corporate persons abuse
the lives and control the thoughts of homo faber. Ideas, once born,
become abortifacients against new conceptions.
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/VMKUC6LDGDPURJ5PN7T7FKOASN2M4NZR/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: unittest of sequence equality

2020-12-22 Thread Alan G. Isaac

This comment completely misses the point.
This "weird type" qualifies as a Sequence.
(See collections.abc.)
Alan Isaac


On 12/22/2020 3:09 PM, Ivan Pozdeev via Python-Dev wrote:

Or just bail out ("resist the temptation to guess") and tell the user to 
compare their weird types themselves.

___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/72HMVTAHUHVRXKVZOYHWK5BW2KYJ2AZT/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: unittest of sequence equality

2020-12-22 Thread Jeff Allen

On 22/12/2020 19:08, Brett Cannon wrote:


... The fact that numpy chooses to implement __eq__ in such a way that 
its result would be surprising if used in an `if` guard I think is 
more a design choice/issue of numpy than a suggestion that you can't 
trust `==` in testing because it _can_ be something other than True/False.


+1  In addition to NumPy's regularly surprising interpretation of 
operators, it is evident from Ivan Pozdeev's investigation (other 
branch) that part of the problem lies with bool(np.array) being an 
error. I can see why that might be sensible. You can have one or the 
other, but not both.


I wondered if Python had become stricter here after NumPy made its 
choices, but a little mining turns up:


   "New in version 2.1. These are the so-called ``rich comparison''
   methods, and are called for comparison operators in preference to
   __cmp__() below. The correspondence between operator symbols and
   method names is as follows: |xy| call
   |x.__ne__(y)|, |x>y| calls |x.__gt__(y)|, and |x>=y| calls
   |x.__ge__(y)|. These methods can return any value, but if the
   comparison operator is used in a Boolean context, the return value
   should be interpretable as a Boolean value, else a TypeError will be
   raised. By convention, |0| is used for false and |1| for true. "

   https://docs.python.org/release/2.1/ref/customization.html

The combination of choices makes the result of a comparison, about which 
there is some freedom, not interpretable as a boolean value. We are 
warned that this should not be expected to work. Later docs (from v2.6) 
refer explicitly to calling bool() as a definition of "interpretable". 
bool() is there from v2.3.


Jeff Allen

___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/WZWT6NZXM4EILAVVJRJQ2A2LK7BBNMFV/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: unittest of sequence equality

2020-12-22 Thread Gregory P. Smith
Numpy chose to violate the principal of equality by having __eq__ not
return a bool. So a numpy type can't be used reliably outside of the numpy
DSL.

-gps

On Tue, Dec 22, 2020, 11:51 AM Alan G. Isaac  wrote:

> Here, `seq1 == seq2` produces a boolean array (i.e., an array of boolean
> values).
> hth, Alan Isaac
>
>
> On 12/22/2020 2:28 PM, Ivan Pozdeev via Python-Dev wrote:
> >
> > You sure about that? For me, bool(np.array) raises an exception:
> >
> > In [12]: np.__version__ Out[12]: '1.19.4'
> >
> > In [11]: if [False, False]==np.array([False, False]): print("foo") <...>
> ValueError: The truth value of an array with more than one element is
> ambiguous.
> > Use a.any() or a.all()
> >
> >
>
>
>
> > On 22.12.2020 21:52, Alan G. Isaac wrote:
> >> The following test fails because because `seq1 == seq2` returns a
> (boolean) NumPy array whenever either seq is a NumPy array.
> >>
> >> import unittest import numpy as np
> unittest.TestCase().assertSequenceEqual([1.,2.,3.], np.array([1.,2.,3.]))
> >>
> >> I expected `unittest` to rely only on features of a
> `collections.abc.Sequence`, which based on
> https://docs.python.org/3/glossary.html#term-sequence, I
> >> believe are satisfied by a NumPy array. Specifically, I see no
> requirement that a sequence implement __eq__ at all much less in any
> particular way.
> >>
> >> In short: a test named `assertSequenceEqual` should, I would think,
> work for any sequence and therefore (based on the available documentation)
> should not
> >> depend on the class-specific implementation of __eq__.
> >>
> >> Is that wrong?
> >>
> >> Thank you, Alan Isaac ___
> Python-Dev mailing list -- python-dev@python.org To unsubscribe send an
> email to
> >> python-dev-le...@python.org
> https://mail.python.org/mailman3/lists/python-dev.python.org/ Message
> archived at
> >>
> https://mail.python.org/archives/list/python-dev@python.org/message/6Z43SU2RPIHTRABYAXBOGRKWGTLIFJYK/
> Code of Conduct:
> >> http://python.org/psf/codeofconduct/
> >
> ___
> Python-Dev mailing list -- python-dev@python.org
> To unsubscribe send an email to python-dev-le...@python.org
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-dev@python.org/message/74CUML37OCQXXKMVUFZORMSCOYP2W5GH/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/FNMNQVMKCN3F3XHLESZLV2NT6XGV2DIM/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: unittest of sequence equality

2020-12-22 Thread Ivan Pozdeev via Python-Dev


On 22.12.2020 22:59, Ivan Pozdeev via Python-Dev wrote:


In the light of this and https://github.com/python/cpython/blob/6afb730e2a8bf0b472b4c3157bcf5b44aa7e6d56/Lib/unittest/case.py#L970 (linked 
to from https://mail.python.org/archives/list/python-dev@python.org/message/AQRLRVY7EJT4LTOCV7CLOFK3FJJTVYYM/ )


I reckon that

*like the other code before it, `seq1 == seq2` should check for (TypeError, 
NotImplementedError)





and fall back to by-element comparison in such a case.*



Or just bail out ("resist the temptation to guess") and tell the user to 
compare their weird types themselves.



On 22.12.2020 22:50, Ivan Pozdeev via Python-Dev wrote:


Okay, I see that by "fails", you probably meant "raises this exception" rather 
than fails the usual way (i.e. raises anAssertionError).

On 22.12.2020 22:38, Alan G. Isaac wrote:

Here, `seq1 == seq2` produces a boolean array (i.e., an array of boolean 
values).
hth, Alan Isaac


On 12/22/2020 2:28 PM, Ivan Pozdeev via Python-Dev wrote:


You sure about that? For me, bool(np.array) raises an exception:

In [12]: np.__version__ Out[12]: '1.19.4'

In [11]: if [False, False]==np.array([False, False]): print("foo") <...> ValueError: The truth value of an array with more than one 
element is ambiguous.

Use a.any() or a.all()







On 22.12.2020 21:52, Alan G. Isaac wrote:

The following test fails because because `seq1 == seq2` returns a (boolean) 
NumPy array whenever either seq is a NumPy array.

import unittest import numpy as np 
unittest.TestCase().assertSequenceEqual([1.,2.,3.], np.array([1.,2.,3.]))

I expected `unittest` to rely only on features of a `collections.abc.Sequence`, which based on 
https://docs.python.org/3/glossary.html#term-sequence, I believe are satisfied by a NumPy array. Specifically, I see no requirement 
that a sequence implement __eq__ at all much less in any particular way.


In short: a test named `assertSequenceEqual` should, I would think, work for any sequence and therefore (based on the available 
documentation) should not depend on the class-specific implementation of __eq__.


Is that wrong?

Thank you, Alan Isaac ___ Python-Dev mailing list -- python-dev@python.org To unsubscribe 
send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/6Z43SU2RPIHTRABYAXBOGRKWGTLIFJYK/ Code of Conduct: 
http://python.org/psf/codeofconduct/



___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/74CUML37OCQXXKMVUFZORMSCOYP2W5GH/
Code of Conduct: http://python.org/psf/codeofconduct/

--
Regards,
Ivan

___
Python-Dev mailing list --python-dev@python.org
To unsubscribe send an email topython-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived 
athttps://mail.python.org/archives/list/python-dev@python.org/message/MKNN64A4JFKSBG37KZJ4HFYQ5TIE35AX/
Code of Conduct:http://python.org/psf/codeofconduct/

--
Regards,
Ivan

___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/WYHFEGPV5WBQZLZZFCQW4STVRGDXNGP3/
Code of Conduct: http://python.org/psf/codeofconduct/


--
Regards,
Ivan

___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/PTLTPR4YWS5L32D3SZHKD3QNIIIYKWOH/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: unittest of sequence equality

2020-12-22 Thread Ivan Pozdeev via Python-Dev
In the light of this and https://github.com/python/cpython/blob/6afb730e2a8bf0b472b4c3157bcf5b44aa7e6d56/Lib/unittest/case.py#L970 (linked 
to from https://mail.python.org/archives/list/python-dev@python.org/message/AQRLRVY7EJT4LTOCV7CLOFK3FJJTVYYM/ )


I reckon that

*like the other code before it, `seq1 == seq2` should check for (TypeError, NotImplementedError) and fall back to by-element comparison in 
such a case.*


On 22.12.2020 22:50, Ivan Pozdeev via Python-Dev wrote:


Okay, I see that by "fails", you probably meant "raises this exception" rather 
than fails the usual way (i.e. raises anAssertionError).

On 22.12.2020 22:38, Alan G. Isaac wrote:

Here, `seq1 == seq2` produces a boolean array (i.e., an array of boolean 
values).
hth, Alan Isaac


On 12/22/2020 2:28 PM, Ivan Pozdeev via Python-Dev wrote:


You sure about that? For me, bool(np.array) raises an exception:

In [12]: np.__version__ Out[12]: '1.19.4'

In [11]: if [False, False]==np.array([False, False]): print("foo") <...> ValueError: The truth value of an array with more than one 
element is ambiguous.

Use a.any() or a.all()







On 22.12.2020 21:52, Alan G. Isaac wrote:

The following test fails because because `seq1 == seq2` returns a (boolean) 
NumPy array whenever either seq is a NumPy array.

import unittest import numpy as np 
unittest.TestCase().assertSequenceEqual([1.,2.,3.], np.array([1.,2.,3.]))

I expected `unittest` to rely only on features of a `collections.abc.Sequence`, which based on 
https://docs.python.org/3/glossary.html#term-sequence, I believe are satisfied by a NumPy array. Specifically, I see no requirement 
that a sequence implement __eq__ at all much less in any particular way.


In short: a test named `assertSequenceEqual` should, I would think, work for any sequence and therefore (based on the available 
documentation) should not depend on the class-specific implementation of __eq__.


Is that wrong?

Thank you, Alan Isaac ___ Python-Dev mailing list -- python-dev@python.org To unsubscribe 
send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/6Z43SU2RPIHTRABYAXBOGRKWGTLIFJYK/ Code of Conduct: 
http://python.org/psf/codeofconduct/



___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/74CUML37OCQXXKMVUFZORMSCOYP2W5GH/
Code of Conduct: http://python.org/psf/codeofconduct/

--
Regards,
Ivan

___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/MKNN64A4JFKSBG37KZJ4HFYQ5TIE35AX/
Code of Conduct: http://python.org/psf/codeofconduct/


--
Regards,
Ivan

___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/WYHFEGPV5WBQZLZZFCQW4STVRGDXNGP3/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: unittest of sequence equality

2020-12-22 Thread Ivan Pozdeev via Python-Dev

Okay, I see that by "fails", you probably meant "raises this exception" rather 
than fails the usual way (i.e. raises anAssertionError).

On 22.12.2020 22:38, Alan G. Isaac wrote:

Here, `seq1 == seq2` produces a boolean array (i.e., an array of boolean 
values).
hth, Alan Isaac


On 12/22/2020 2:28 PM, Ivan Pozdeev via Python-Dev wrote:


You sure about that? For me, bool(np.array) raises an exception:

In [12]: np.__version__ Out[12]: '1.19.4'

In [11]: if [False, False]==np.array([False, False]): print("foo") <...> ValueError: The truth value of an array with more than one 
element is ambiguous.

Use a.any() or a.all()







On 22.12.2020 21:52, Alan G. Isaac wrote:

The following test fails because because `seq1 == seq2` returns a (boolean) 
NumPy array whenever either seq is a NumPy array.

import unittest import numpy as np 
unittest.TestCase().assertSequenceEqual([1.,2.,3.], np.array([1.,2.,3.]))

I expected `unittest` to rely only on features of a `collections.abc.Sequence`, which based on 
https://docs.python.org/3/glossary.html#term-sequence, I believe are satisfied by a NumPy array. Specifically, I see no requirement that 
a sequence implement __eq__ at all much less in any particular way.


In short: a test named `assertSequenceEqual` should, I would think, work for any sequence and therefore (based on the available 
documentation) should not depend on the class-specific implementation of __eq__.


Is that wrong?

Thank you, Alan Isaac ___ Python-Dev mailing list -- python-dev@python.org To unsubscribe 
send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/6Z43SU2RPIHTRABYAXBOGRKWGTLIFJYK/ Code of Conduct: 
http://python.org/psf/codeofconduct/



___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/74CUML37OCQXXKMVUFZORMSCOYP2W5GH/
Code of Conduct: http://python.org/psf/codeofconduct/


--
Regards,
Ivan

___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/MKNN64A4JFKSBG37KZJ4HFYQ5TIE35AX/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: unittest of sequence equality

2020-12-22 Thread Alan G. Isaac

Here, `seq1 == seq2` produces a boolean array (i.e., an array of boolean 
values).
hth, Alan Isaac


On 12/22/2020 2:28 PM, Ivan Pozdeev via Python-Dev wrote:


You sure about that? For me, bool(np.array) raises an exception:

In [12]: np.__version__ Out[12]: '1.19.4'

In [11]: if [False, False]==np.array([False, False]): print("foo") <...> 
ValueError: The truth value of an array with more than one element is ambiguous.
Use a.any() or a.all()







On 22.12.2020 21:52, Alan G. Isaac wrote:

The following test fails because because `seq1 == seq2` returns a (boolean) 
NumPy array whenever either seq is a NumPy array.

import unittest import numpy as np 
unittest.TestCase().assertSequenceEqual([1.,2.,3.], np.array([1.,2.,3.]))

I expected `unittest` to rely only on features of a `collections.abc.Sequence`, which based on https://docs.python.org/3/glossary.html#term-sequence, I 
believe are satisfied by a NumPy array. Specifically, I see no requirement that a sequence implement __eq__ at all much less in any particular way.


In short: a test named `assertSequenceEqual` should, I would think, work for any sequence and therefore (based on the available documentation) should not 
depend on the class-specific implementation of __eq__.


Is that wrong?

Thank you, Alan Isaac ___ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to 
python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/6Z43SU2RPIHTRABYAXBOGRKWGTLIFJYK/ Code of Conduct: 
http://python.org/psf/codeofconduct/



___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/74CUML37OCQXXKMVUFZORMSCOYP2W5GH/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: unittest of sequence equality

2020-12-22 Thread David Mertz
On Tue, Dec 22, 2020 at 6:57 PM Alan G. Isaac  wrote:

> The following test fails because because `seq1 == seq2` returns a
> (boolean) NumPy array
> whenever either seq is a NumPy array.
>
>  import unittest
>  import numpy as np
>  unittest.TestCase().assertSequenceEqual([1.,2.,3.],
> np.array([1.,2.,3.]))
>
> I expected `unittest` to rely only on features of a
> `collections.abc.Sequence`,
> which based on https://docs.python.org/3/glossary.html#term-sequence,
> I believe are satisfied by a NumPy array.


If you know you might be dealing with NumPy arrays (as the import
suggests), I think it's simply right to spell it as:

unittest.TestCase().assertTrue(np.array_equal([1., 2., 3.], np.array([1.,
2., 3.])))

Or for pytest etc., simply:

assert np.array_equal([1., 2., 3.], np.array([1., 2., 3.]))
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/JJHAURGJCFG5PZUQFOUPTJWXGQABOSBT/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: unittest of sequence equality

2020-12-22 Thread Ivan Pozdeev via Python-Dev


On 22.12.2020 21:52, Alan G. Isaac wrote:

The following test fails because because `seq1 == seq2` returns a (boolean) 
NumPy array
whenever either seq is a NumPy array.


You sure about that? For me, bool(np.array) raises an exception:

In [12]: np.__version__
Out[12]: '1.19.4'

In [11]: if [False, False]==np.array([False, False]): print("foo")
<...>
ValueError: The truth value of an array with more than one element is 
ambiguous. Use a.any() or a.all()




    import unittest
    import numpy as np
    unittest.TestCase().assertSequenceEqual([1.,2.,3.], np.array([1.,2.,3.]))

I expected `unittest` to rely only on features of a `collections.abc.Sequence`,
which based on https://docs.python.org/3/glossary.html#term-sequence,
I believe are satisfied by a NumPy array. Specifically, I see no requirement
that a sequence implement __eq__ at all much less in any particular way.

In short: a test named `assertSequenceEqual` should, I would think,
work for any sequence and therefore (based on the available documentation)
should not depend on the class-specific implementation of __eq__.

Is that wrong?

Thank you, Alan Isaac
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/6Z43SU2RPIHTRABYAXBOGRKWGTLIFJYK/
Code of Conduct: http://python.org/psf/codeofconduct/


--
Regards,
Ivan
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/JG7VF7Z3WPHZENEECVPJDPJCB63KB4EH/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: unittest of sequence equality

2020-12-22 Thread Guido van Rossum
Interesting. Did you look at the code? It is here (that's the `==` operator
you're complaining about):

https://github.com/python/cpython/blob/6afb730e2a8bf0b472b4c3157bcf5b44aa7e6d56/Lib/unittest/case.py#L970

The code does already analyze the length of the sequence

You are right that collections.abc.Sequence (or its ancestors other than
object) does not implement `__eq__`, so it would seem that the `==`
operator would have to be replaced with a simple loop:
```
for x, y in zip(seq1, seq2):
if x is not y and x != y:
break
else:
return  # They are all equal
```
Making that change would probably slow things down. (Note that the odd
check "x is not y and x != y" is needed to keep the previous behavior
regarding NaN and other objects that aren't equal to themselves.)

One could also argue that the docstring warns about this issue:
```
For the purposes of this function, a valid ordered sequence type is one
which can be indexed, has a length, and has an equality operator.
```
IOW, I think this ship has actually sailed.

On Tue, Dec 22, 2020 at 10:56 AM Alan G. Isaac  wrote:

> The following test fails because because `seq1 == seq2` returns a
> (boolean) NumPy array
> whenever either seq is a NumPy array.
>
>  import unittest
>  import numpy as np
>  unittest.TestCase().assertSequenceEqual([1.,2.,3.],
> np.array([1.,2.,3.]))
>
> I expected `unittest` to rely only on features of a
> `collections.abc.Sequence`,
> which based on https://docs.python.org/3/glossary.html#term-sequence,
> I believe are satisfied by a NumPy array. Specifically, I see no
> requirement
> that a sequence implement __eq__ at all much less in any particular way.
>
> In short: a test named `assertSequenceEqual` should, I would think,
> work for any sequence and therefore (based on the available documentation)
> should not depend on the class-specific implementation of __eq__.
>
> Is that wrong?
>
> Thank you, Alan Isaac
> ___
> Python-Dev mailing list -- python-dev@python.org
> To unsubscribe send an email to python-dev-le...@python.org
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-dev@python.org/message/6Z43SU2RPIHTRABYAXBOGRKWGTLIFJYK/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*

___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/AQRLRVY7EJT4LTOCV7CLOFK3FJJTVYYM/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: unittest of sequence equality

2020-12-22 Thread Brett Cannon
On Tue, Dec 22, 2020 at 10:54 AM Alan G. Isaac  wrote:

> The following test fails because because `seq1 == seq2` returns a
> (boolean) NumPy array
> whenever either seq is a NumPy array.
>
>  import unittest
>  import numpy as np
>  unittest.TestCase().assertSequenceEqual([1.,2.,3.],
> np.array([1.,2.,3.]))
>
> I expected `unittest` to rely only on features of a
> `collections.abc.Sequence`,
> which based on https://docs.python.org/3/glossary.html#term-sequence,
> I believe are satisfied by a NumPy array. Specifically, I see no
> requirement
> that a sequence implement __eq__ at all much less in any particular way.
>
> In short: a test named `assertSequenceEqual` should, I would think,
> work for any sequence and therefore (based on the available documentation)
> should not depend on the class-specific implementation of __eq__.
>
> Is that wrong?
>

Yes and no. :) I don't agree that `seq1 == seq2` should not be tried if the
sequences support it, but the function does work on sequences that lack a
definition of `__eq__` as you would expect (e.g. user-defined sequences
where you just didn't want to bother). The fact that numpy chooses to
implement __eq__ in such a way that its result would be surprising if used
in an `if` guard I think is more a design choice/issue of numpy than a
suggestion that you can't trust `==` in testing because it _can_ be
something other than True/False.
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/LZUQ54HKGRP5TUUETQXGG2ZJ5RWAARXK/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] unittest of sequence equality

2020-12-22 Thread Alan G. Isaac

The following test fails because because `seq1 == seq2` returns a (boolean) 
NumPy array
whenever either seq is a NumPy array.

import unittest
import numpy as np
unittest.TestCase().assertSequenceEqual([1.,2.,3.], np.array([1.,2.,3.]))

I expected `unittest` to rely only on features of a `collections.abc.Sequence`,
which based on https://docs.python.org/3/glossary.html#term-sequence,
I believe are satisfied by a NumPy array. Specifically, I see no requirement
that a sequence implement __eq__ at all much less in any particular way.

In short: a test named `assertSequenceEqual` should, I would think,
work for any sequence and therefore (based on the available documentation)
should not depend on the class-specific implementation of __eq__.

Is that wrong?

Thank you, Alan Isaac
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/6Z43SU2RPIHTRABYAXBOGRKWGTLIFJYK/
Code of Conduct: http://python.org/psf/codeofconduct/