You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Set operations don't work for dictionary views #52651
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/avassalotti'closed_at=<Date2010-05-04.03:50:28.481>created_at=<Date2010-04-15.01:23:46.882>labels= ['type-bug', 'release-blocker']
title="Set operations don't work for dictionary views"updated_at=<Date2010-05-06.15:33:04.660>user='https://github.com/akuchling'
-> ./python.exe
Python 2.7b1+ (trunk:80084:80085M, Apr 14 2010, 21:17:06)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}
>>> keys = dishes.viewkeys()
>>> keys & {'eggs', 'bacon', 'salad'}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for &: 'dict_keys' and 'set'
>>> keys | {'eggs'}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'dict_keys' and 'set'
Is this a documentation bug, and set operations are only supported in 3.x? Or does the code need to be fixed?
(Assigned to Alexandre, since he committed the backport patch; please feel free to reassign. Marking as release blocker; if it's a documentation bug, we can lower the priority.)
First, the dictviews_as_number is broken; the field for classic division was removed in 3.x, so everything is shifted by one. I included a patch to fix this.
Unfortunately, this isn't enough to fix the issue. There seems to be some overly restrictive type checking going on in the method wrappers. However, I don't have the time to investigate this further today. I should be able to check this next weekend.
I found the issue. The view types didn't have Py_TPFLAGS_CHECKTYPES set, so the types were using the old-style binary operators.
Here's a patch that fixes the issue. Please review.
AlexanderBelopolsky commented on Apr 29, 2010
AlexanderBelopolskymannequin
Mannequin
More actions
The patch looks fine. I verified that the new tests pass on trunk and py3k. I am attaching a patch for py3k with a forward port of set opereations and repr tests.
(commenting on a closed bug, because I'm not sure it should be re-opened)
While coming up with examples, I found a weird inconsistency. Was it intentional for viewkeys() and viewitems() to support set operations, but not viewvalues()?
>>> d1 =dict((i*10, chr(65+i)) for i inrange(26))
>>> d2 =dict((i**.5, i) for i inrange(1000))
>>> d1.viewkeys() |set('abc')
set([0, 130, 10, 140, 20, 150, 30, 160, 40, 170, 50, 180, 60, 190, 70, 200, 80, 210, 90, 220, 'a', 'c', 'b', 100, 230, 110, 240, 120, 250])
>>> d1.viewitems() |set('abc')
set([(70, 'H'), (0, 'A'), ....)
>>> d1.viewvalues() |set('abc')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'dict_values' and 'set'
>>> d1.viewvalues() | d2.viewvalues()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'dict_values' and 'dict_values'
Why do you expect dict_values to support set operations? Dict values unlike keys are not sets, they are more like lists. Set operations of dict_values are not supported in 3.x either.
Activity
akuchling commentedon Apr 15, 2010
The examples of set operations in http://docs.python.org/dev/library/stdtypes#dictionary-view-objects don't work in the current 2.7 trunk:
Is this a documentation bug, and set operations are only supported in 3.x? Or does the code need to be fixed?
(Assigned to Alexandre, since he committed the backport patch; please feel free to reassign. Marking as release blocker; if it's a documentation bug, we can lower the priority.)
avassalotti commentedon Apr 15, 2010
It is a bug.
First, the dictviews_as_number is broken; the field for classic division was removed in 3.x, so everything is shifted by one. I included a patch to fix this.
Unfortunately, this isn't enough to fix the issue. There seems to be some overly restrictive type checking going on in the method wrappers. However, I don't have the time to investigate this further today. I should be able to check this next weekend.
avassalotti commentedon Apr 17, 2010
I found the issue. The view types didn't have Py_TPFLAGS_CHECKTYPES set, so the types were using the old-style binary operators.
Here's a patch that fixes the issue. Please review.
AlexanderBelopolsky commentedon Apr 29, 2010
The patch looks fine. I verified that the new tests pass on trunk and py3k. I am attaching a patch for py3k with a forward port of set opereations and repr tests.
avassalotti commentedon May 4, 2010
Committed in r80749 and r80751 (for py3k).
Thank you!
akuchling commentedon May 5, 2010
(commenting on a closed bug, because I'm not sure it should be re-opened)
While coming up with examples, I found a weird inconsistency. Was it intentional for viewkeys() and viewitems() to support set operations, but not viewvalues()?
akuchling commentedon May 5, 2010
The fix is easy, I think; just add Py_TPFLAGS_CHECKTYPES to the PyDictValues_Type's definition.
abalkin commentedon May 5, 2010
Why do you expect dict_values to support set operations? Dict values unlike keys are not sets, they are more like lists. Set operations of dict_values are not supported in 3.x either.
akuchling commentedon May 6, 2010
Ah, of course! It didn't occur to me that .values() isn't necessarily a set.
rhettinger commentedon May 6, 2010
FWIW, this agrees with the specs in _abcoll which show KeysView and ItemsView as sets but not ValuesView.