Skip to content

Fix empty string piece leaking into first_list for comma-only input#217

Merged
derek73 merged 2 commits into
masterfrom
fix/empty-piece-in-first-list
Jul 5, 2026
Merged

Fix empty string piece leaking into first_list for comma-only input#217
derek73 merged 2 commits into
masterfrom
fix/empty-piece-in-first-list

Conversation

@derek73

@derek73 derek73 commented Jul 5, 2026

Copy link
Copy Markdown
Owner

What changed

Degenerate comma inputs left silent empty-string members in the public *_list attributes. Two fixes:

1. parse_pieces() drops tokens that strip to nothing (the common tokenizer feeding all parse paths and the setters):

  • HumanName(',')collapse_whitespace strips the trailing comma, so this flows through the no-comma path with a single '' piece; first_list was ['']
  • HumanName('Doe,, Jr.') — the empty middle segment flows through the lastname-comma path; first_list was ['']
  • HumanName(', John') — empty parts[0] put [''] in last_list
  • Setter paths are covered too, and pinned as intended behavior: hn.first = ' ' now yields an empty first (was the truthy string ' '), and hn.middle = ['a', '', 'b'] / hn['last'] = ['', 'Smith'] drop the empty member. As a side benefit this repairs conjunction joining for double-spaced setter input ("Mr. and Mrs. Smith" previously produced malformed pieces).

2. The suffix-comma path's parts[1:] loop gets the same if part: guard its parts[2:] sibling got in #216 — found in review: HumanName('John Doe, Jr.,,') leaked '' into suffix_list because that loop bypasses parse_pieces via expand_suffix_delimiter('')[''].

The empty members were harmless in str() output (the joined accessors are falsy), but the *_list attributes are public API.

Notes for review

  • Both fixes were TDD'd: regression tests written first and watched fail under both empty_attribute_default config params (test_degenerate_comma_input_leaves_no_empty_pieces, test_assignment_filters_empty_tokens in tests/test_python_api.py; test_suffix_comma_empty_segment_not_added_to_suffix_list in tests/test_suffixes.py).
  • parse_pieces()'s docstring now states the no-empty-pieces guarantee; the release log entry covers all three affected lists and the setter behavior.
  • Joined-string semantics are unchanged for existing truthy inputs; only list contents and whitespace-only setter values differ.
  • Also aligns the suffix-comma "final piece" comment with the no-comma site's wording (a replace_all in Fix bugs hidden in untested parser paths; remove vestigial unparsable #216 missed this deeper-indented copy).
  • Verified: pytest (1304 passed), mypy, ruff, sphinx doctests, README doctests.

Follow-up to #216, which surfaced the original bug during its silent-failure review.

🤖 Generated with Claude Code

HumanName(',') (no-comma path after collapse_whitespace strips the
trailing comma) and HumanName('Doe,, Jr.') (lastname-comma path) both
appended '' to first_list. Filter tokens that strip to nothing in
parse_pieces, the common source feeding all three parse paths, so no
empty piece reaches the parse loops or the public *_list attributes.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@derek73 derek73 self-assigned this Jul 5, 2026
@derek73 derek73 added the bug label Jul 5, 2026
@derek73 derek73 added this to the v1.3.0 milestone Jul 5, 2026
- Add the missing `if part` guard to the suffix-comma parts[1:] loop:
  "John Doe, Jr.,," leaked '' into suffix_list via
  expand_suffix_delimiter('') returning [''], the same bug class this
  branch fixes elsewhere (regression test added)
- Pin the setter-path filtering as intended behavior: whitespace-only
  and empty tokens assigned via attributes, lists, or __setitem__ no
  longer become *_list members
- Pin the lastname_pieces call site: ", John" leaves last_list empty
- Assert sibling lists for HumanName(',') so a relocated (not removed)
  empty piece can't pass
- State the no-empty-pieces guarantee in parse_pieces()'s docstring and
  broaden the release log entry to cover last_list/suffix_list and the
  setter behavior
- Align the suffix-comma "final piece" comment with the no-comma site's
  wording (the PR 216 replace_all missed this deeper-indented copy)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@derek73 derek73 merged commit b62620a into master Jul 5, 2026
8 checks passed
@derek73 derek73 deleted the fix/empty-piece-in-first-list branch July 5, 2026 03:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant