Skip to content

Preserve relationships when ManyToMany replacement keys collide - #528

Closed
adiazpar wants to merge 1 commit into
mahmoud:masterfrom
adiazpar:fix-manytomany-replace-collisions
Closed

adiazpar wants to merge 1 commit into
mahmoud:masterfrom
adiazpar:fix-manytomany-replace-collisions

Conversation

@adiazpar

@adiazpar adiazpar commented Oct 3, 2026

Copy link
Copy Markdown

The change

ManyToMany.replace() loses existing destination relationships when the replacement key is already present, while leaving their inverse entries behind:

from boltons.dictutils import ManyToMany

relation = ManyToMany([('old', 'a'), ('new', 'b')])
relation.replace('old', 'new')
relation['new']        # currently frozenset({'a'})
relation.inv['b']      # currently frozenset({'new'})
relation.remove('new', 'b')  # currently raises KeyError

Replacing a key should substitute it in each relationship pair. Merge the source relationships into the destination set, then rewire only the original source relationships in the inverse. This preserves destination-only relationships and collapses overlaps naturally. Validate that the destination is hashable before removing the source so invalid input cannot leave the relation inconsistent.

The change affects one relationship dimension; it does not rename matching values in the other dimension. Existing missing-key and self-replacement behavior is preserved, and the method still returns None. No dependencies or public signatures change.

This is separate from #526, which addresses shared sets in ManyToMany.update() and construction from another instance. These regression fixtures are built from pairs rather than copied ManyToMany objects.

Validation on Linux:

  • Reproduced the collision on released 26.2.0 and master 4e5faa3d7e4008d89e0d8bf1ea87b6d9a061a16d.
  • Five of the 14 added regression cases fail against unchanged master; all 14 pass with the fix.
  • uvx --with tox-uv tox -e py312,py313 -- -q: 692 tests and doctests, plus 12 subtests, passed in each built-package environment (CPython 3.12.14 and 3.13.5).
  • Independent AI review found no blocking issues; 36,864 exhaustive graph replacements and 50,000 deterministic mixed operations passed against a pair-set model.
  • uv build: source distribution and wheel built successfully.
  • git diff --check: passed.

Windows, macOS, the remaining Python versions, and upstream CI have not been run for this proposed contribution.

The backstory

This contribution was prepared by OpenAI's assistant in response to a request for one useful, bounded open-source contribution. Source inspection of the dictionary utilities exposed a mismatch with the documented synchronized-inverse behavior, and a small replacement example reproduced it. The practical use case is merging duplicate keys or canonicalizing aliases while preserving their associated tags or memberships; this is an illustrative use case, not a claim of prior personal use of boltons.

AI assistance

  • Harness/tooling: OpenAI assistant with a cloud coding workspace, Git, Python, pytest, uv and tox. The assistant performed project screening, reproduction, implementation, testing, technical review and drafting. Technical review was performed by separate AI reviewers, not by a human reviewer.
  • Model(s): OpenAI model; the exact model identifier is not exposed in this runtime.
  • Original prompt, relevant task excerpt: "Prepare one useful, bounded open-source contribution and handle the research, implementation, testing and technical review yourself. Understand the problem, check current issues and related pull requests to avoid duplicate work, reproduce the problem where possible, build the solution and test it."

@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