On Tue, Dec 22, 2020 at 10:54 AM Alan G. Isaac <alan.is...@gmail.com> 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/