gh-140866: Document dict view == excludes non-set iterables - #155858
Open
SomSamantray wants to merge 1 commit into
Open
gh-140866: Document dict view == excludes non-set iterables#155858SomSamantray wants to merge 1 commit into
== excludes non-set iterables#155858SomSamantray wants to merge 1 commit into
Conversation
… iterables collections.abc.Set.__eq__ requires the other operand to be a Set instance, so set-like dict views (keys()/items()) compare equal to another set/frozenset/view but always compare unequal to a list or tuple of the same elements. This is the one exception to the documented "set-like views accept any iterable as the other operand" rule for set operators, and it wasn't called out, leading to user confusion (pythongh-140866). Add the missing clarification plus a doctest example demonstrating the asymmetry.
Documentation build overview
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
collections.abc.Set.__eq__only returns a real comparison when the otheroperand is itself a
Setinstance, sodict.keys()/dict.items()viewscompare equal to another set-like view,
set, orfrozenset, but alwayscompare unequal to a
listortupleof the same elements — even though thedocs say set-like views "accept any iterable as the other operand" for set
operators like
&,|, and^. That sentence doesn't carry over to==,and the docs never said so, which is why users keep reporting it as a bug.
This documents the exception right next to the existing sentence, and
extends the existing dictionary-view doctest example with two lines
demonstrating the asymmetry (
keys == ['bacon', 'spam']isFalse, butkeys == {'bacon', 'spam'}isTrue).Verified locally: the extended doctest block passes (10/10 examples), and a
full Sphinx build of
library/stdtypes.rst(sphinx-build -W --keep-going)produces no new warnings.
Fixes #140866
dict.keys(),dict.values()anddict.items()with==#140866