Skip to content

Correct complement set symmetric differences - #487

Open
a3ylf wants to merge 2 commits into
mahmoud:masterfrom
a3ylf:fix-complement-symmetric-difference
Open

a3ylf wants to merge 2 commits into
mahmoud:masterfrom
a3ylf:fix-complement-symmetric-difference

Conversation

@a3ylf

@a3ylf a3ylf commented Sep 7, 2026

Copy link
Copy Markdown

complement({1, 2}) ^ {2, 3} currently returns complement({1}), so it still contains 3 even though 3 belongs to both operands. symmetric_difference_update() gives a different wrong result: it leaves 2 excluded although 2 belongs only to the right operand.

Apply symmetric difference to the stored finite sets when exactly one operand is complemented. This follows (~A) ^ B == ~(A ^ B) and fixes both the operator and the mutating method.

Add 40 regression cases covering overlapping, identical, disjoint, and empty inputs; ordinary sets, frozensets, and both complement representations; reversed operands; and applying the update twice to restore the original membership. Expected results come from ordinary set operations on sampled membership, including values outside both finite inputs.

Validation on Linux:

  • Before the fix: 19 of the 40 regression cases fail.
  • Python 3.10.20: python -m pytest --doctest-modules boltons tests -q — 712 passed.
  • Python 3.14: uvx --with tox-uv tox -e py314 -- -q — 712 passed, including package build and doctests.
  • git diff --check passes.

Made with Astra on Codex

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.

1 participant