Skip to content

Reuse parsed value enclosures by default #592

Description

@claell

Problem

RemoveEnclosingMiddleware already records whether each parsed value used braces, quotes, or no enclosure. The default writer nevertheless configures AddEnclosingMiddleware(reuse_previous_enclosing=False), so an unchanged parse/write cycle converts quoted and bare numeric values to braces.

That creates unrelated diffs and means editing a field's value also changes its established enclosure style. This is avoidable layout mutation because the parser already has the necessary metadata.

Proposed default

Set reuse_previous_enclosing=True in the default unparse stack:

  • parsed quoted values remain quoted;
  • parsed braced values remain braced;
  • parsed bare integers remain bare;
  • explicitly demanded bare macro/expression values remain bare; and
  • new programmatic fields without parser metadata continue using braces.

Safety for edited bare values

Reusing no-enclosing metadata requires one guard. A bare integer may later be edited to ordinary text while retaining the original metadata. Emitting that text bare could create invalid BibTeX. When no-enclosing comes only from parser metadata (rather than an explicit field.enclosing demand), reuse it only while the current value is still numeric; otherwise fall back to the configured default enclosure.

Tests should cover unchanged quote/brace/bare values, edits to quoted and braced fields, the edited-bare fallback, and values created without metadata.

Compatibility

This is a formatting-default change, not a semantic transformation. Callers who prefer uniform braces can continue to supply AddEnclosingMiddleware(reuse_previous_enclosing=False, ...) explicitly. Preserving source enclosure choices is the less surprising default for ordinary read/edit/write use.

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