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: Type checking in set comparisons.
Type: behavior Stage: resolved
Components: Versions: Python 3.5, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: SilentGhost, nyoshimizu
Priority: normal Keywords:

Created on 2016-11-29 19:53 by nyoshimizu, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test_comparison.py nyoshimizu, 2016-11-29 19:53 Examples
Messages (3)
msg282036 - (view) Author: nyoshimizu (nyoshimizu) Date: 2016-11-29 19:53
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?
msg282039 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2016-11-29 20:02
You seem to be misunderstanding how the intersection/union/etc are supposed to be used:

>>> ab = {'a', 'b'}
>>> ab.intersection('bc')
{'b'}

Using set.intersection (where set is a built-in class, rather than an instance thereof) requires the first argument to be set (which is the actual instance of set class). This is no different from usage of any other class / object across Python, however, it is highly uncommon.
msg282040 - (view) Author: nyoshimizu (nyoshimizu) Date: 2016-11-29 20:14
I see. Sorry & thanks!
History
Date User Action Args
2022-04-11 14:58:40adminsetgithub: 73020
2016-11-29 20:14:35nyoshimizusetmessages: + msg282040
2016-11-29 20:02:27SilentGhostsetstatus: open -> closed

nosy: + SilentGhost
messages: + msg282039

resolution: not a bug
stage: resolved
2016-11-29 19:53:07nyoshimizucreate