Skip to content

Add a collision policy to NormalizeFieldKeys - #641

Open
spandante wants to merge 3 commits into
sciunto-org:mainfrom
spandante:field-collision-policy
Open

spandante wants to merge 3 commits into
sciunto-org:mainfrom
spandante:field-collision-policy

Conversation

@spandante

Copy link
Copy Markdown

When an entry has both Title and title, NormalizeFieldKeys lowercases the keys and keeps the last value. This change adds a keyword-only collision_policy="first" option so callers can retain the first value. The default remains "last", and the existing positional allow_inplace_modification argument still works. Both policies retain the position of the first occurrence and warn when they discard a value.

Addresses the field-key collision request in #477. Entry identifiers and entry types are outside this change.

import bibtexparser
from bibtexparser.middlewares import NormalizeFieldKeys

library = bibtexparser.parse_string(
    "@misc{example, Title={First}, title={Last}}",
    append_middleware=[NormalizeFieldKeys(collision_policy="first")],
)
assert library.entries_dict["example"]["title"] == "First"

Validation includes regression tests for both policies, field order, copy behavior, invalid values, warnings, and parse/write/reparse. The full local suite passed with 2,831 tests passing, 12 existing skips, and four existing deprecation warnings. The documentation build also passed.

AI assistance was used for the initial patch, regression tests, and draft description.

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