Thanks to visit codestin.com
Credit goes to github.com

Skip to content

dict items views have set like operations even when the values are not hashable. #111622

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

Closed
EvanTheB opened this issue Nov 1, 2023 · 5 comments
Closed
Labels
docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@EvanTheB
Copy link

EvanTheB commented Nov 1, 2023

Documentation

Keys views are set-like since their entries are unique and :term:hashable. If all values are hashable, so that (key, value) pairs are unique and hashable, then the items view is also set-like. (Values views are not treated as set-like since the entries are generally not unique.) For set-like views, all of the operations defined for the abstract base class :class:collections.abc.Set are available (for example, ==, <, or ^). While using set operators, set-like views accept any iterable as the other operand, unlike sets which only accept sets as the input.

> {1:2, 3:4}.items() >= {1:2}.items()
True
> {1:[2], 3:4}.items() >= {1:[2]}.items()
True
> set({1:[2], 3:4}.items())
TypeError: unhashable type: 'list'

It seems like this documentation is not accurate. dict.items() does have set operations even when the values are not hashable.

See also: https://stackoverflow.com/questions/77399592/set-like-behaviour-of-dict-items-for-non-hashable-values

Linked PRs

@EvanTheB EvanTheB added the docs Documentation in the Doc dir label Nov 1, 2023
@terryjreedy terryjreedy added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Nov 1, 2023
@terryjreedy
Copy link
Member

My guess is the same as in 'Blcknight's answer: it was realized that items is read only, making hash((key,value)) = hash(key) is sufficient, making the caveat in the doc unnecessary. To be sure, someone should check that the items for the example dict works for all of the supported operations. And check the dict items code and the git blame history of revisions of that code.

@pochmann
Copy link
Contributor

pochmann commented Nov 2, 2023

Not all set operations actually work, for example {0: []}.items() | set() fails with TypeError: unhashable type: 'list'.

@rhettinger
Copy link
Contributor

Perhaps replace with second sentence with:

Item views also have set-like operations since the (key, value) pairs are unique
and the keys are hashable. If the values in the items view are hashable as well,
then the items views can interoperate with other sets.

terryjreedy added a commit to terryjreedy/cpython that referenced this issue Nov 14, 2023
They are set-like even when some values are not hashable,
but work even better when all are.
@terryjreedy
Copy link
Member

#112051 is mostly Raymond's suggestion.

terryjreedy added a commit that referenced this issue Nov 14, 2023
They are set-like even when some values are not hashable,
but work even better when all are.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 14, 2023
They are set-like even when some values are not hashable,
but work even better when all are.
(cherry picked from commit e31d65e)

Co-authored-by: Terry Jan Reedy <[email protected]>
terryjreedy added a commit that referenced this issue Nov 14, 2023
They are set-like even when some values are not hashable,
but work even better when all are.
(cherry picked from commit e31d65e)

Co-authored-by: Terry Jan Reedy <[email protected]>
@terryjreedy
Copy link
Member

3.11 backport failed (conflict)

aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
They are set-like even when some values are not hashable,
but work even better when all are.
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
They are set-like even when some values are not hashable,
but work even better when all are.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs)
Projects
None yet
Development

No branches or pull requests

4 participants