Skip to content

Trailing backslashes break name round trips #567

Description

@claell

Describe the bug

Trailing backslashes break the intended inverse relationship between
NameParts.merge_last_name_first and parse_single_name_into_parts.

Two related cases are involved:

  • Parsing a name whose final token ends in a backslash appends that backslash
    twice because the end-of-input branch does not stop further handling of the
    same character.
  • merge_last_name_first escapes the final section even though no comma follows
    it, changing a terminal backslash unnecessarily.

The existing inverse-property test skips strict and non-strict examples with
odd trailing backslashes, concealing the round-trip failure.

Reproducing

Version: current main (08e370a)

Code:

from bibtexparser.middlewares.names import NameParts
from bibtexparser.middlewares.names import parse_single_name_into_parts

parts = NameParts(
    first=["Christina", "A.", "L.\\"],
    von=[],
    last=["Thiele"],
    jr=[],
)
merged = parts.merge_last_name_first
assert parse_single_name_into_parts(merged) == parts

The assertion fails on current main.

Expected behavior

Last-name-first serialization and reparsing should preserve the NameParts
value in both strict and non-strict modes, including terminal backslashes. Only
sections followed by a comma need delimiter-protecting escaping.

Workaround

Avoid terminal backslashes in name parts or normalize affected names manually.

Remaining Questions (Optional)

  • I would be willing to contribute a PR to fix this issue.
  • This issue is a blocker, I'd be grateful for an early fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions