[issue24434] ItemsView.__contains__ does not mimic dict_items

2016-04-30 Thread Xiang Zhang

Xiang Zhang added the comment:

After reading issue4296, I agree with Serhiy's point on the second issue. Right 
now, (1, math.nan) in ItemsView({1: math.nan}) returns false which seems not 
acceptable.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24434] ItemsView.__contains__ does not mimic dict_items

2016-04-29 Thread Xiang Zhang

Xiang Zhang added the comment:

Caleb's resolution looks good, just like the C version does, at least seems 
correct.

Serhiy, the corner case is interesting. math.nan == math.nan should return 
false so I think (1, math.nan) in ItemsView({1: math.nan} is a right behaviour. 
But the C version, which calls PyObject_RichCompareBool and then do a pointer 
check first seems having some problem.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24434] ItemsView.__contains__ does not mimic dict_items

2016-04-28 Thread Xiang Zhang

Changes by Xiang Zhang :


--
nosy: +xiang.zhang

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24434] ItemsView.__contains__ does not mimic dict_items

2016-04-26 Thread Raymond Hettinger

Changes by Raymond Hettinger :


--
assignee: rhettinger -> 

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24434] ItemsView.__contains__ does not mimic dict_items

2015-12-19 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

Yet one corner case:

>>> (1, math.nan) in {1: math.nan}.items()
True
>>> (1, math.nan) in ItemsView({1: math.nan})
False

This can be resolved if compare not v with value, but a tuple (key, v) with 
item.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24434] ItemsView.__contains__ does not mimic dict_items

2015-06-15 Thread Martin Panter

Martin Panter added the comment:

The trouble with Serhiy’s suggestion is that it would still try to iterate the 
argument:

 i = iter(lambda: print(ITERATION), infinity)
 i in dict()  # No iteration
False
 i in ItemsView(dict())
ITERATION
ITERATION
ITERATION
False

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue24434
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24434] ItemsView.__contains__ does not mimic dict_items

2015-06-11 Thread Caleb Levy

Changes by Caleb Levy caleb.l...@berkeley.edu:


--
components: Library (Lib)
nosy: clevy, rhettinger, stutzbach
priority: normal
severity: normal
status: open
title: ItemsView.__contains__ does not mimic dict_items
type: behavior
versions: Python 2.7, Python 3.2, Python 3.3, Python 3.4, Python 3.5, Python 3.6

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue24434
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24434] ItemsView.__contains__ does not mimic dict_items

2015-06-11 Thread Caleb Levy

New submission from Caleb Levy:

The current implementation ItemsView.__contains__ reads

class ItemsView(MappingView, Set):
...
def __contains__(self, item):
key, value = item
try:
v = self._mapping[key]
except KeyError:
return False
else:
return v == value
...

This poses several problems. First, any non-iterable or iterable not having 
exactly two elements will raise an error instead of returning false. 

Second, an ItemsView object is roughly the same as a set of tuple-pairs hashed 
by the first element. Thus, for example,

[a, 1] in d.items()

will return False for any dict d, yet in the current ItemsView implementation, 
this is True.

The patch changes behavior to immediately return false for non-tuple items and 
tuples not of length 2, avoiding unnecessary exceptions. It also adds tests to 
collections which fail under the old behavior and pass with the update.

--
keywords: +patch
Added file: http://bugs.python.org/file39682/ItemsView_contains.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue24434
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24434] ItemsView.__contains__ does not mimic dict_items

2015-06-11 Thread Martin Panter

Martin Panter added the comment:

Added a couple suggestions for the test case on Reitveld.

--
nosy: +vadmium
stage:  - patch review

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue24434
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24434] ItemsView.__contains__ does not mimic dict_items

2015-06-11 Thread Caleb Levy

Caleb Levy added the comment:

@serhiy.storchaka: I don't think that will work.

First of all,

x, y = item

will raise a ValueError if fed an iterable whose length is not exactly 2, so 
you would have to check for that. Moreover, if item is something like a dict, 
for example, then:

{a: 1, b: 2} in DictLikeMapping(a=b)

could return True, which I don't think would be expected behavior.

I'm not terribly fond of the instance check myself, but in this case I can't 
see any other way to do it: the built in dict_items necessarily consists of 
*tuples* of key-value pairs.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue24434
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24434] ItemsView.__contains__ does not mimic dict_items

2015-06-11 Thread Caleb Levy

Caleb Levy added the comment:

Sorry; that should be DictLikeMapping(a=b).items(), where DictLikeMapping is 
defined in the patch unit tests.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue24434
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24434] ItemsView.__contains__ does not mimic dict_items

2015-06-11 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

Additional check hits performance. First issue can be resolved by changing code 
to

try:
key, value = item
except TypeError:
return False

Second issue can be resolved by comparing not v with value, but (key, v) with 
item. However I'm not sure that fixing it is worth such complication of the 
code.

--
nosy: +serhiy.storchaka
versions:  -Python 3.2, Python 3.3

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue24434
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24434] ItemsView.__contains__ does not mimic dict_items

2015-06-11 Thread Raymond Hettinger

Changes by Raymond Hettinger raymond.hettin...@gmail.com:


--
assignee:  - rhettinger

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue24434
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com