Skip to content

frozenset could be "updated" by |= operator, is this intended? #126783

@DanielYang59

Description

@DanielYang59

Bug report

Bug description:

The |= operator behaves unexpectedly when used with frozenset, where it creates a new instance. This can be misleading because it effectively updates it. I would expect this to trigger a TypeError?

fs = frozenset([1, 2, 3])

print(id(fs))  # 4379466816

fs |= {4, 5}

print(fs)  # >>> frozenset({1, 2, 3, 4, 5})

print(id(fs))  # ID changed to 4379462560

CPython versions tested on:

3.9, 3.10, 3.11, 3.12, 3.13

Operating systems tested on:

macOS

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