Skip to content

gh-150165: Clarify membership test semantics for hash-based containers and object identity#150166

Closed
KRRT7 wants to merge 1 commit into
python:mainfrom
KRRT7:clarify-membership-docs
Closed

gh-150165: Clarify membership test semantics for hash-based containers and object identity#150166
KRRT7 wants to merge 1 commit into
python:mainfrom
KRRT7:clarify-membership-docs

Conversation

@KRRT7
Copy link
Copy Markdown

@KRRT7 KRRT7 commented May 20, 2026

As discussed on discuss.python.org.

Changes

§6.10.2 Membership test operations:

  • Split the equivalence: sequence types (list, tuple, deque) use the existing ``any(x is e or x == e ...)`` linear scan
  • Hash-based containers (set, frozenset, dict) first hash ``x`` to locate a candidate, then check ``is``/``==``
  • Added a note that the ``is`` check can make membership succeed via identity for NaN and state-dependent ``eq``, but this behavior is implementation-dependent

§6.2.3.1 Literals and object identity:

  • Added a sentence clarifying that non-literal expressions (calls, comprehensions, etc.) make no identity guarantees

Motivation

This came from a real bug: Nuitka/Nuitka#3889. Nuitka's constant blob cached ``float('nan')`` as a singleton, making ``nan2 in {nan1}`` return ``True`` instead of ``False``. Both were spec-compliant because the spec doesn't address whether calls return the same object — that ambiguity is what this PR addresses.

Related issues

…tainers and object identity

Split the membership test equivalence in §6.10.2 to distinguish
sequence types (linear scan) from hash-based containers (hash lookup).
Add a note acknowledging that the x is e check has observable
consequences for NaN and state-dependent __eq__, and that object
identity is unspecified for non-literal expressions.

Also add a clarifying sentence in §6.2.3.1 noting that non-literal
expressions make no identity guarantees.
@KRRT7 KRRT7 requested review from AA-Turner and willingc as code owners May 20, 2026 17:18
@bedevere-app bedevere-app Bot added docs Documentation in the Doc dir skip news labels May 20, 2026
@python-cla-bot
Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@read-the-docs-community
Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #32781364 | 📁 Comparing 0ecf8ec against main (2443001)

  🔍 Preview build  

1 file changed
± reference/expressions.html

@picnixz picnixz closed this May 20, 2026
@picnixz
Copy link
Copy Markdown
Member

picnixz commented May 20, 2026

Closing because the DPO thread does not have support from core devs yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

Membership test documentation doesn't account for hashing in sets/dicts

2 participants