New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document dict behavior when setting equal but not identical key #91081
Comments
When a key that is equal to an existing key (but not the same object identity) is used to set a new value, the key itself is not replaced. This manifests perhaps most clearly in Consider two equal keys, k1 and k2: d = WeakKeyDictionary()
d[k1] = 1
d[k2] = 2
del k1 We would expect the dictionary to have a single entry k2 => 2. But in fact it is empty now. |
As @methane also said on the PR, this is a backward compatibility break and we can't just change the behavior. I'd be opposed to a change here: the proposed behavior isn't clearly better than the existing behavior (sometimes you want one behavior, sometimes another), and it will be difficult to find and adjust code that relies on the existing behavior. |
I concur with Jelle and Methane that we can't do this without breaking code. Also if you don't care about dict order, the work around is easy. Just remove the old key:
|
How about if we make PR instead to the documentation about this behaviour? I think this behaviour is a little surprising (you need to know the internals of how WeakKeyDict keys are constructed and checked) and while I understand you do not want breaking change, maybe this should be explained to the users and example given how to apply the workaround? Just following the "least surprise" principle. |
Agree, I couldn't find a place where this behavior is documented. The second paragraph in https://docs.python.org/3.10/library/stdtypes.html#mapping-types-dict comes close. Reopening as a documentation issue. |
The weakref docs in particular should point out the OP's example and highlight the workaround. |
Java's HashMap has also the (current) Python behavior. An existing same key is not replaced. |
I don't know much about Java, but Java's WeakHashMap is same to Python's WeakKeyDictionary. https://docs.oracle.com/javase/9/docs/api/java/util/WeakHashMap.html """ |
Yeah. Sounds like adding docs is indeed needed :) |
…aced entry (#91499) Co-authored-by: Pieter Eendebak <P.T.eendebak@tudelft.nl> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
…a replaced entry (pythonGH-91499) (cherry picked from commit c615286) Co-authored-by: Stanley <46876382+slateny@users.noreply.github.com> Co-authored-by: Pieter Eendebak <P.T.eendebak@tudelft.nl> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
…a replaced entry (pythonGH-91499) (cherry picked from commit c615286) Co-authored-by: Stanley <46876382+slateny@users.noreply.github.com> Co-authored-by: Pieter Eendebak <P.T.eendebak@tudelft.nl> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
malthe mannequin commentedMar 4, 2022
•
edited by bedevere-bot
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:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: