classification
Title: Inconsistent behavior between set and dict_keys/dict_items: for non-iterable object x, set().__or__(x) raises NotImplementedError, but {}.keys().__or__(x) raises TypeError
Type: behavior Stage:
Components: Versions: Python 3.6, Python 3.4, Python 3.5, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: canjo, pitrou, rhettinger
Priority: low Keywords:

Created on 2015-06-08 21:04 by canjo, last changed 2015-06-10 05:08 by rhettinger.

Messages (2)
msg245044 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-06-09 05:32
The dictviews_or() function in Objects/dictobject.c is converting the keys to a set and calling the set.update() method with the given argument.  The set.update() method doesn't return NotImplemented because it has no reflected operation.

It looks like dictviews_or() should instead call set.__or__() to allow it a chance to return NotImplemented.  The other dictview set operations are similarly afflicted.
msg245046 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-06-09 06:06
One other thought:  Returning NotImplemented may be an easy change but it isn't clear that it would provide a sensible capability for mapping views.  The non-iterable *other* argument would need to have a __ror__() method than could do something useful with a KeysView or ItemsView argument.  That seems like an improbable use case (which is why I presume this has never come up before).
History
Date User Action Args
2015-06-10 05:08:23rhettingersetnosy: + pitrou
2015-06-09 06:06:16rhettingersetmessages: + msg245046
2015-06-09 05:32:56rhettingersetpriority: normal -> low

messages: + msg245044
versions: + Python 2.7, Python 3.5, Python 3.6
2015-06-08 23:00:43ned.deilysetnosy: + rhettinger
2015-06-08 21:04:25canjocreate