intbitset icon indicating copy to clipboard operation
intbitset copied to clipboard

Support comparisons with other types.

Open dset0x opened this issue 10 years ago • 6 comments

Using any comparison operator on an intbiset with anything that is not an intbitset results in the following:

TypeError: Argument 'rhs' has incorrect type (expected intbitset.intbitset, got something_else)

It should instead return False as a set would.

dset0x avatar Nov 26 '14 10:11 dset0x

Mmh but:

>  set.__eq__(set([1,2,3]), [1,2,3])
... NotImplemented
>  set.__eq__(set([1,2,3]), set([1,2,3]))
... True
> set([1,2,3]) == [1,2,3]
... False
>  set([1,2,3]) == set([1,2,3])
... True

So what is actually the reference implementation to follow?

kaplun avatar Dec 02 '14 15:12 kaplun

>  set.__eq__(set([1,2,3]), [1,2,3])
... NotImplemented

I cannot reproduce this. Returns False on my machine, as we are trying to compare different types. (Python2.7, Python2.6, PyPy 2.2.1)

>  set.__eq__(set([1,2,3]), set([1,2,3]))
... True

This is basically set([1,2,3]) == set([1,2,3]), which is True alright.

> set([1,2,3]) == [1,2,3]
... False

This is comparing two different types, which is always False.

>  set([1,2,3]) == set([1,2,3])
... True

dset0x avatar Dec 02 '14 15:12 dset0x

 set.__eq__(set([1,2,3]), [1,2,3])
NotImplemented

I cannot reproduce this. Returns False on my machine, as we are trying to compare different types. (Python2.7, Python2.6, PyPy 2.2.1)

That's weird. Are you actually typing the above?

skaplun@PCSK4 ~➔ python
Python 2.7.8 (default, Oct 20 2014, 15:05:19) 
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> set.__eq__(set([1,2,3]), [1,2,3])
NotImplemented
>>> 

kaplun avatar Dec 02 '14 16:12 kaplun

# python2.7
Python 2.7.7 (default, Sep 24 2014, 12:01:43) 
[GCC 4.7.3] on linux
>>> set.__eq__(set([1,2,3]), [1,2,3])
False
>>> 
# python3.4
Python 3.4.1 (default, Nov  1 2014, 12:34:56) 
[GCC 4.7.3] on linux
>>> set.__eq__(set([1,2,3]), [1,2,3])
NotImplemented
>>> set([1,2,3]) == [1,2,3]
False

After some hunting in the bugtracker, it seems that the internal behavior of where comparisons occur was changed somewhere in 2.7.8. (For example, for ABCs: http://bugs.python.org/issue8743)

The interpreter uses some "other" fallback to provide an answer when the rich comparison operators are used. I am not sure about how this works, but ebb8865dcf54 (and PyAnySet_Check) may lead to the answer.

FWIW,

>>> object.__ne__([1,2,3], set([1,2,3]))
True

Edit: After an IRL chat it seems that one should simply return NotImplemented and let the interpreter resolve that to a False.

dset0x avatar Dec 03 '14 09:12 dset0x

Perhaps a different issue, but could we also support operations with other kinds of sets? For example, the following snippets fail:

intbitset([1,2]) | {1,2,3}
intbitset([1,2]).union({1,2,3})

Edit: Should behave like this:

In [11]: frozenset([1,2]) | {1,2}
Out[11]: frozenset({1, 2})

In [12]: {1,2} | frozenset([1,2])
Out[12]: {1, 2}

dset0x avatar Feb 25 '15 14:02 dset0x

@dset0x Please check out the latest https://github.com/inveniosoftware/intbitset/pull/78 that has this commit: https://github.com/inveniosoftware/intbitset/pull/78/commits/3ec8a41b6e05bc9c559f71b8595867b458fb8610

pombredanne avatar Feb 22 '22 17:02 pombredanne