[issue43078] Equality Errors when Unpickling and Pickling a Dictionary with a nan

2021-01-30 Thread Rohan Prasad


Rohan Prasad  added the comment:

I see this makes a ton of sense, thanks! I'll close this issue.

--
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

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



[issue43078] Equality Errors when Unpickling and Pickling a Dictionary with a nan

2021-01-30 Thread Dennis Sweeney


Dennis Sweeney  added the comment:

I think this is the expected behavior.

It is expected by IEEE 754 that nan != nan, so that behavior exists:

>>> nan = float('nan')
>>> nan2 = float('nan')
>>> assert nan != nan2
>>> assert nan != nan

However, for "practicality beats purity" (speed) reasons, we compare containers 
by first checking if the entries are identical (aliases of the same object), 
before falling back to the actual comparisons. So identical entries make for 
equal containers, even if the entries don't compare equal.

>>> assert nan is nan   # quick check for identity succeeds,
>>> assert [nan] == [nan]   # therefore these compare equal

>>> assert nan is not nan2  # quick check for identity fails,
>>> assert nan != nan2  # fall back to actual comparison, which fails
>>> assert [nan] != [nan2]  # therefore these do not compare equal

When you serialize and deserialize the container with pickle, you make new 
entries that are no longer aliases.

>From https://docs.python.org/3.9/reference/expressions.html#comparisons:
"The built-in containers typically assume identical objects are equal to 
themselves."

--
nosy: +Dennis Sweeney

___
Python tracker 

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



[issue43078] Equality Errors when Unpickling and Pickling a Dictionary with a nan

2021-01-30 Thread Rohan Prasad


New submission from Rohan Prasad :

Hi,

I'm having a weird issue with floats in dictionaries and equality comparison 
(see attached screenshot for an example). When I create two dictionaries with a 
nan value they pass an equivalence test. However, if I pickle and unpickle one 
of the dictionaries they become no longer equivalent. I would expect that the 
behavior would be the same irrespective of whether they are pickled/unpickled. 
Is there something else I should be doing here or is there a way to easily 
resolve this (short of writing custom ways to compare equality)?

--
files: numpy-nan-dict-error.png
messages: 385990
nosy: rp4fx12
priority: normal
severity: normal
status: open
title: Equality Errors when Unpickling and Pickling a Dictionary with a nan
type: behavior
versions: Python 3.7, Python 3.8
Added file: https://bugs.python.org/file49781/numpy-nan-dict-error.png

___
Python tracker 

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