Skip to content

🐛 Do not mutate the passed library in write_string/write_file - #614

Merged
MiWeiss merged 1 commit into
mainfrom
fix/write-does-not-mutate-library
Sep 3, 2026
Merged

🐛 Do not mutate the passed library in write_string/write_file#614
MiWeiss merged 1 commit into
mainfrom
fix/write-does-not-mutate-library

Conversation

@MiWeiss

@MiWeiss MiWeiss commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

write_string / write_file mutated the library passed in whenever a middleware with the default allow_inplace_modification=True ran before the copying default unparse stack — e.g. the prepend_middleware example from the docs:

lib = bibtexparser.parse_string("@article{a, month = {January}, title = {T}}")
bibtexparser.write_string(lib, prepend_middleware=[MonthAbbreviationMiddleware()])
lib.entries[0]["month"]  # "jan" — input was modified

Fix: deep-copy the library once before running the stack, unless every middleware allows in-place modification. Guarantee: write_string/write_file never modify the passed library, unless every middleware in the unparse stack allows in-place modification (an all-in-place stack is the explicit opt-in). Default stack behavior unchanged; no new parameters.

Tests: input unchanged after write_string/write_file with an in-place prepended middleware (output still transformed); default write does not mutate; all-in-place stack may mutate; copying-only stack gets no extra upfront copy. Full suite: 2725 passed, 12 skipped.

🤖 Generated with Claude Code

Middlewares passed via prepend_middleware default to in-place modification and run
before the copying default unparse stack, so writing mutated the caller's library
(e.g. month "January" became "jan"). Deep-copy the library once upfront unless every
middleware in the stack allows in-place modification (the explicit opt-in).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@MiWeiss
MiWeiss force-pushed the fix/write-does-not-mutate-library branch from ebce299 to da956e4 Compare September 3, 2026 20:45
@MiWeiss
MiWeiss merged commit 261318d into main Sep 3, 2026
16 checks passed
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