New submission from nyoshimizu:

The non-operator versions of set comparisons (intersection(), union(), etc.) 
exhibit
inconsistent type checking. They only check the first input before deciding 
whether or not to raise a TypeError exception. 

Therefore, it's possible to pass a set first, then other objects (e.g. lists, 
dicts, tuples) and a correct 'intersection' is returned (apparently by ignoring 
ordering and using the keys in dicts). I've attached demonstrative example for 
Python 3.5, although Python 2.7 appears to exhibit the same behavior.

I'm not sure what the intended behavior was (whether or not to require sets). 
8.7.1 Set Objects states: "Note, the non-operator versions of union(), 
intersection(), difference(), and symmetric_difference() will accept any 
iterable as an argument."

Note that #8743 and #17626 appear to confirm that the operator versions should 
not accept non-sets and matches the observed behavior. As the latter issue 
points out, it's documented -- again in 8.7.1 -- that "...their operator based 
counterparts require their arguments to be sets." 

Is this behavior necessary but just not documented? The documentation states 
that "This precludes error-prone constructions like Set('abc') & 'cbs' in favor 
of the more readable Set('abc').intersection('cbs')." In the second example, a 
first set is needed to do the intersection, then 'cbs' gets typecast into a set 
(although I guess so was 'abc'). Then should the first inputs also be typecast 
as sets?

----------
files: test_comparison.py
messages: 282036
nosy: nyoshimizu
priority: normal
severity: normal
status: open
title: Type checking in set comparisons.
type: behavior
versions: Python 2.7, Python 3.5
Added file: http://bugs.python.org/file45694/test_comparison.py

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue28834>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to