Skip to content

🦖 Make Library.add fail on duplicate keys by default - #527

Merged
MiWeiss merged 7 commits into
mainfrom
fix/400-fail-on-duplicate-key-default
Jun 12, 2026
Merged

MiWeiss merged 7 commits into
mainfrom
fix/400-fail-on-duplicate-key-default

Conversation

@MiWeiss

@MiWeiss MiWeiss commented Jun 12, 2026 •

Copy link
Copy Markdown
Collaborator

Context

Companion to #526, addressing the root cause of #400: library.add(entry) silently converted a programmatically created entry into a DuplicateBlockKeyBlock when its key already existed. Users only discovered this much later (e.g. at write time), far from the add() call that caused it.

Change

Breaking (pre-2.0-final): Library.add and the Library constructor now default to fail_on_duplicate_key=True and raise a ValueError on duplicate keys.

The failing add is atomic: duplicates (including duplicates within the same add call) are detected by a pre-check, so the exception is raised before any mutation and the library is left untouched. Passing fail_on_duplicate_key=False restores the silent-replacement behavior (duplicates become DuplicateBlockKeyBlocks, exposed via library.failed_blocks).

This also makes add consistent with Library.replace, which has always defaulted to fail_on_duplicate_key=True.

Parsing behavior is unchanged: the splitter and the middleware library reconstruction (BlockMiddleware.transform, SortBlocksByTypeAndKeyMiddleware) explicitly pass fail_on_duplicate_key=False, so bibtex files with duplicate keys still parse leniently into library.failed_blocks (covered by the existing test_handles_duplicate_entries).

Since v2 is still in beta (v2.0.0b9), this seems like the last cheap moment to flip the default.

🤖 Generated with Claude Code

MiWeiss and others added 2 commits June 12, 2026 07:54
Programmatically adding an entry with an existing key previously replaced
it silently with a DuplicateBlockKeyBlock, surprising users at write time
(see #400). Library.add and the Library constructor now raise a ValueError
on duplicate keys by default.

Parsing behavior is unchanged: the splitter and middleware library
reconstruction explicitly pass fail_on_duplicate_key=False, so bibtex
files with duplicate keys still parse into library.failed_blocks.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Instead of inserting the duplicate as a DuplicateBlockKeyBlock and then
raising, add() now pre-checks all blocks (including duplicates within the
same call) and raises before any mutation, making the operation atomic.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@MiWeiss
MiWeiss force-pushed the fix/400-fail-on-duplicate-key-default branch from 8b7202e to 022721d Compare June 12, 2026 05:54
MiWeiss and others added 5 commits June 12, 2026 07:59
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
After merging #526's tests into this branch, the duplicate must be added
with fail_on_duplicate_key=False so the test still exercises the
write-time error path.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@MiWeiss

MiWeiss commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator Author

reviewed

@MiWeiss
MiWeiss merged commit 4a1ee5c into main Jun 12, 2026
19 checks passed
@MiWeiss
MiWeiss deleted the fix/400-fail-on-duplicate-key-default branch June 12, 2026 10:52
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