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

Skip to content

Underdocumented and partially untested behavior of set-like dict views #96408

Closed
@filiplajszczak

Description

@filiplajszczak

Unlike sets, set-like dict view objects behave identical with both operator and non-operator versions of set operations.

In case of sets only non-operator versions accept any iterable as an argument, and one has to explicitly convert iterable to set to use operator on it. That's documented and tested.

s = {"a", "b"}
s.union(["a", "c"])  # valid
s | ["a", "c"]       # raises TypeError

Set-like dict view objects, however, accept any iterable after an operator.

d = {"a": "A", "b": "B"}
d.keys() | ["a", "c"]     # valid
d.items() | [("c", "C")]  # valid

As far as we were able to find that difference in behavior is not documented but is tested for keys but not for items. It's also reflected in the typeshed stubs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions