Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support comparisons with other types. #18

Open
dset0x opened this issue Nov 26, 2014 · 6 comments
Open

Support comparisons with other types. #18

dset0x opened this issue Nov 26, 2014 · 6 comments

Comments

@dset0x
Copy link

dset0x commented Nov 26, 2014

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.

@kaplun
Copy link
Member

kaplun commented Dec 2, 2014

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?

@dset0x
Copy link
Author

dset0x commented Dec 2, 2014

>  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

@kaplun
Copy link
Member

kaplun commented Dec 2, 2014

 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
>>> 

@dset0x
Copy link
Author

dset0x commented Dec 3, 2014

# 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.

kaplun added a commit to kaplun/intbitset that referenced this issue Dec 8, 2014
* Comparing an intbitset with anything that is not an intbitset
  should behave as a set would. E.g.
  intbitset([1,2,3]) == set([1,2,3]) is False.
  (closes inveniosoftware-contrib#18)

Signed-off-by: Samuele Kaplun <samuele.kaplun@cern.ch>
Reported-by: Dimitrios Semitsoglou-Tsiapos <dsemitso@cern.ch>
@dset0x
Copy link
Author

dset0x commented Feb 25, 2015

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}

kaplun added a commit to kaplun/intbitset that referenced this issue Jul 13, 2015
* Comparing an intbitset with anything that is not an intbitset
  should behave as a set would. E.g.
  intbitset([1,2,3]) == set([1,2,3]) is False.
  (closes inveniosoftware-contrib#18)

Signed-off-by: Samuele Kaplun <samuele.kaplun@cern.ch>
Reported-by: Dimitrios Semitsoglou-Tsiapos <dsemitso@cern.ch>
@kaplun kaplun removed their assignment Jan 5, 2017
pombredanne added a commit that referenced this issue Feb 22, 2022
* Comparing an intbitset with anything that is not an intbitset
  should behave as a set would. E.g.
  intbitset([1,2,3]) == set([1,2,3]) is False.

Referenced-by: #18
Contributed-by: Samuele Kaplun <samuele.kaplun@cern.ch>
Reported-by: Dimitrios Semitsoglou-Tsiapos <dsemitso@cern.ch>
Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
@pombredanne
Copy link
Collaborator

@dset0x Please check out the latest #78 that has this commit:
3ec8a41

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants