This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Dict View binops permit non-set types
Type: behavior Stage: resolved
Components: Versions: Python 3.4, Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Joshua Morton, r.david.murray, steven.daprano
Priority: normal Keywords:

Created on 2016-05-08 04:24 by Joshua Morton, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg265110 - (view) Author: Joshua Morton (Joshua Morton) Date: 2016-05-08 04:24
Following the comments in python ideas [1], I'm submitting a bug report. In python 3, dictionary views (KeysView and ItemsView specifically) do not adhere to the same interface as Sets. Specifically, the __and__, __or__, __xor__, and __sub__ methods on Views will accept a non-set type (`{}.keys() | []`), while the same throws a TypeError on a Set. The suggested, not-backwards-compatible solution, was to have dictviews raise errors in the same way. 

[1] https://mail.python.org/pipermail/python-ideas/2016-April/039469.html
msg265114 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2016-05-08 06:00
On Sun, May 08, 2016 at 04:24:59AM +0000, Joshua Morton wrote:

> Following the comments in python ideas [1] [...]
> 
> [1] https://mail.python.org/pipermail/python-ideas/2016-April/039469.html

Am I missing something? I don't see anything resembling a consensus that 
the behaviour of dict views is a bug in that thread, in fact I hardly 
see any discussion about this specific behaviour. (Other related topics 
are discussed in more depth.)

I don't think it is a bug for views to permit non-set arguments with 
binops:

py> {'a': 1, 'b': 2}.keys() & ['a', 'c']
{'a'}

I think it is a matter of taste. My own taste tells me that sets should 
be more restrictive, only accepting other [frozen]sets or subclasses, 
but views should be less restrictive, and perform more duck-typing of 
"set-like" objects, including lists. No, I can't justify it, except by 
an appeal to status quo: that's how it is now, and I don't think that 
changing it is worth breaking backwards compatibility.
msg265117 - (view) Author: Joshua Morton (Joshua Morton) Date: 2016-05-08 07:08
There (seemed to be) consensus between the one or two on topic commenters that something was off, although much of the discussion was on a tangent. Although on looking back, there was even less discussion than I originally thought. Heh.

My response was going to be very different, however I did some additional digging: the `collections.abc.Set` class can be used as a mixin for implementing set-likes. In its default implementations of the various binops, they will attempt to convert the right hand argument of an operator to the correct type, via _from_iterable. This in effect makes the set the special case that is more permissive than Set is. 

This strikes me as strange, I'd expect set and Set to be 'the same', or in other words a Set to act like a set and vice versa. But it seems that python swings toward your intuition and not mine. And the opposite, making set *more* permissive also doesn't sit well, nor does it feel necessary.
msg265158 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-05-08 19:06
In any case I think the backward compatibility argument torpedoes making the views more restrictive.

Since you agree there's no real consensus on python-ideas, I'm going to close this.  If there is some other consistency change that seems to make sense and gains consensus on python-ideas, you can open another issue.
History
Date User Action Args
2022-04-11 14:58:30adminsetgithub: 71160
2016-05-08 19:06:04r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg265158

resolution: rejected
stage: resolved
2016-05-08 07:08:07Joshua Mortonsetmessages: + msg265117
2016-05-08 06:00:36steven.dapranosetnosy: + steven.daprano
messages: + msg265114
2016-05-08 04:24:58Joshua Mortoncreate