Skip to content

fix: dedupe identical NaN objects in append_unique, matching set semantics - #54

Open
rahul05ranjan wants to merge 2 commits into
toumorokoshi:masterfrom
rahul05ranjan:fix/extended-set-identity-equality
Open

rahul05ranjan wants to merge 2 commits into
toumorokoshi:masterfrom
rahul05ranjan:fix/extended-set-identity-equality

Conversation

@rahul05ranjan

Copy link
Copy Markdown

Fixes #53

Summary

ExtendedSet bucket membership compares with == only, but CPython's set membership short-circuits on identity first (PyObject_RichCompareBool uses x is y or x == y). The original set(base)-based append_unique relied on that fast path; the ExtendedSet rewrite silently dropped it, so the same float('nan') object is now appended again instead of being recognized as a duplicate:

>>> nan = float('nan')
>>> nan in {nan}                    # True
True
>>> nan in ExtendedSet([nan])        # False
False
>>> merge([nan], [nan])             # was [nan] before ExtendedSet
[nan, nan]

_insert and __contains__ now compare identity-first via a small _is_or_equal helper that mirrors PyObject_RichCompareBool, restoring plain-set semantics for identical objects whose __eq__ is not reflexive. Two different NaN objects are still both kept, exactly as a plain set does.

Validation

  • New regression tests in tests/strategy/test_list.py:
    • test_strategy_append_unique_identical_nan — fails on un-fixed HEAD.
    • test_extended_set_membership_identity_semantics — direct membership pin, fails on un-fixed HEAD.
    • test_strategy_append_unique_distinct_nan_kept — pins the unchanged distinct-NaN behavior (passes both sides).
  • Full suite: 48 passed on system Python 3.13 (45 baseline + 3 new).
  • Integration battery (standalone): append_unique unchanged for plain values, equal dicts, unhashable lists/tuples, and the Deduplicate incoming values in append_unique #46 repeated-incoming-values cases; same-object unhashables ([1], {"a": 1}) deduped as before.
  • make ready-pr not run on this host (Black/validate-pyproject unavailable); CI to cover.

Notes

This is complementary to (and intentionally does not overlap) #52, which addresses the uninitialized set storage inherited by the class. No shared hunks; branches are independent cuts off master.

rahul05ranjan and others added 2 commits November 13, 2026 07:00
…ntics

ExtendedSet compares bucket members with ==, but set membership
short-circuits on identity first (PyObject_RichCompareBool). The
original set(base)-based append_unique relied on that: the same
float('nan') object was deduplicated, and NaN != NaN made ExtendedSet
append it again. Compare identity-first so identical objects count as
equal in membership and insertion, like a plain set.

Co-authored-by: rahul05ranjan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

append_unique no longer deduplicates an identical NaN object (ExtendedSet lost set's identity fast path)

1 participant