Repository navigation
🦖 Make Library.add fail on duplicate keys by default - #527
Merged
Merged
Conversation
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
force-pushed
the
fix/400-fail-on-duplicate-key-default
branch
from
June 12, 2026 05:54
8b7202e to
022721d
Compare
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>
Collaborator
Author
|
reviewed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Companion to #526, addressing the root cause of #400:
library.add(entry)silently converted a programmatically created entry into aDuplicateBlockKeyBlockwhen its key already existed. Users only discovered this much later (e.g. at write time), far from theadd()call that caused it.Change
Breaking (pre-2.0-final):
Library.addand theLibraryconstructor now default tofail_on_duplicate_key=Trueand raise aValueErroron duplicate keys.The failing
addis atomic: duplicates (including duplicates within the sameaddcall) are detected by a pre-check, so the exception is raised before any mutation and the library is left untouched. Passingfail_on_duplicate_key=Falserestores the silent-replacement behavior (duplicates becomeDuplicateBlockKeyBlocks, exposed vialibrary.failed_blocks).This also makes
addconsistent withLibrary.replace, which has always defaulted tofail_on_duplicate_key=True.Parsing behavior is unchanged: the splitter and the middleware library reconstruction (
BlockMiddleware.transform,SortBlocksByTypeAndKeyMiddleware) explicitly passfail_on_duplicate_key=False, so bibtex files with duplicate keys still parse leniently intolibrary.failed_blocks(covered by the existingtest_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