Skip to content

馃悰 Apply parse stack only to new blocks in parse_string(library=...) - #603

Merged
MiWeiss merged 1 commit into
mainfrom
fix/parse-string-existing-library
Sep 2, 2026
Merged

MiWeiss merged 1 commit into
mainfrom
fix/parse-string-existing-library

Conversation

@MiWeiss

@MiWeiss MiWeiss commented Sep 2, 2026 •

Copy link
Copy Markdown
Collaborator

parse_string(library=existing) runs the parse stack over the merged library, re-transforming blocks an earlier call already transformed. The middlewares are not idempotent.

lib = bibtexparser.parse_string('@string{me = "My Name"}\n@article{a, title = {Hello World}}')
lib = bibtexparser.parse_string('@article{bb, title = {Second}}', library=lib)

Before: a spurious "RemoveEnclosingMiddleware must not run before..." warning, and output containing title = Hello World and @string{me = My Name}. Unenclosed, invalid, does not round-trip.

Fix: split into a fresh library, run the stack there, merge with fail_on_duplicate_key=False.

Cross-call string resolution works today, so it is kept: tagged deep copies of the caller's @string blocks are seeded into the fresh library and filtered out before the merge. Originals untouched.

Worth a decision: Splitter.split(library=...) is now unused inside the package. Left public and untouched.

Tests: 12 cases. The parameter had no coverage at all before. Suite 2588 passed, from 2576.


馃 Generated with Claude Code

@MiWeiss
MiWeiss force-pushed the fix/parse-string-existing-library branch 3 times, most recently from 0be036e to e42a2c1 Compare September 2, 2026 20:00
Parsing into an existing library re-ran the full parse stack over the
blocks already contained in that library. As the parse-stack middlewares
are not idempotent, previously parsed entries and strings were corrupted:
RemoveEnclosingMiddleware stripped them a second time and stamped a
`no-enclosing` demand on the already-stripped values (so the library no
longer round-tripped to valid bibtex), and ResolveStringReferencesMiddleware
warned about a wrong middleware order.

The splitter now fills a fresh library, the parse stack is applied to that
one only, and the resulting blocks are merged into the passed library
(with `fail_on_duplicate_key=False`, so cross-call duplicates still become
DuplicateBlockKeyBlock instances). To keep string references in the new
content resolvable against @string blocks defined by earlier calls, tagged
deep copies of those strings are seeded into the fresh library and dropped
again before merging.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@MiWeiss
MiWeiss force-pushed the fix/parse-string-existing-library branch from e42a2c1 to 64c208c Compare September 2, 2026 20:03
@MiWeiss

MiWeiss commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator Author

lgtm

@MiWeiss
MiWeiss merged commit f4b8cd3 into main Sep 2, 2026
31 checks passed
MiWeiss added a commit that referenced this pull request Sep 2, 2026
Since #603, `parse_string` builds a fresh `Library` and merges the
result instead of handing the caller's library to the splitter, so
nothing in the package (or the tests, docs or README) passes
`library=` to `Splitter.split()` any more.

Removing it rather than keeping an unused public parameter, while
2.0.0 is still unreleased (2.0.0b9). `split()` now always returns a
new `Library`; the `Splitter` class and `split()` itself stay public.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
MiWeiss added a commit that referenced this pull request Sep 2, 2026
Since #603, `parse_string` builds a fresh `Library` and merges the
result instead of handing the caller's library to the splitter, so
nothing in the package (or the tests, docs or README) passes
`library=` to `Splitter.split()` any more.

Removing it rather than keeping an unused public parameter, while
2.0.0 is still unreleased (2.0.0b9). `split()` now always returns a
new `Library`; the `Splitter` class and `split()` itself stay public.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
@MiWeiss
MiWeiss deleted the fix/parse-string-existing-library branch September 10, 2026 20:04
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