Skip to content

Fix complement set intersection membership and in-place state - #516

Closed
hansu650 wants to merge 1 commit into
mahmoud:masterfrom
hansu650:fix/complement-intersection
Closed

hansu650 wants to merge 1 commit into
mahmoud:masterfrom
hansu650:fix/complement-intersection

Conversation

@hansu650

Copy link
Copy Markdown

The change

Correct three intersection branches in setutils.complement:

  • For an included-form set intersected with a complement, subtract the excluded values from the included values, not the reverse.
  • When &= intersects a complement with a finite set, switch to the included representation.
  • When &= intersects an included-form set with a complement, keep the included representation.

For example, complement(complement({1, 2})) & complement({2, 3}) currently yields {3} instead of {1}. Also, after value = complement({1, 2}); value &= {2, 3}, the result currently excludes 3 and contains unrelated values such as 0, instead of being the finite set {3}.

The fix preserves in-place object identity and leaves the right operand unchanged. It adds 148 regression/control cases covering ordinary sets, both complement representations, set/frozenset backing, overlapping/disjoint/equal/empty inputs, reversed operands, the method API, and self-intersection. Expected membership is computed using ordinary sets, including probes outside both inputs.

Validation on Windows, Python 3.13.15:

  • Before the fix: 52 failed, 96 passed among the added cases.
  • After the fix: setutils tests 164 passed.
  • Full suite with module doctests: 826 passed, 12 subtests passed, both from source and from the built/installed wheel.
  • Both changed methods have 100% statement and branch coverage.
  • Additional local exhaustive check: all pairs of subsets of four values, both backing types independently, and each supported operand representation; 6,144 scenarios / 24,576 operation checks passed, including an outside membership probe.
  • Clean-export sdist/wheel builds, twine check, pip check, and git diff --check passed.

Other Python/OS combinations have not been run locally. This is separate from #487 (symmetric difference) and #494/#503 (IndexedSet operations).

The backstory

This is @hansu650's first PR to boltons, prepared by Codex on a request to find and submit a tested Python contribution. The defect was found during source review of the set-like API, not in a production incident or long-term use of boltons. The documented goal of following standard-library conventions made ordinary set membership a useful reference for checking these branches.

AI assistance

  • Harness/tooling: OpenAI Codex, acting on behalf of @hansu650.
  • Model(s): GPT-5-based Codex agent; the exact backend variant is not exposed in this session.
  • Original prompt: “你再去看一下github的回复啥的哦,还有要不再去新作一个github的pr?” (“Please check the GitHub replies again, and could you make another GitHub PR?”)

Codex checked contribution guidance and existing PRs, reproduced the failures, implemented the fix and tests, ran validation, and drafted/submitted this PR. No independent human code review is claimed.

@mahmoud

mahmoud commented Oct 25, 2026

Copy link
Copy Markdown
Owner

Folded into #609 (see the PR description for credit). Thanks for the report and the repro.

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.

2 participants