intbitset
intbitset copied to clipboard
Support comparisons with other types.
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.
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?
> 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
set.__eq__(set([1,2,3]), [1,2,3]) NotImplementedI 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
>>>
# 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.
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 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