Skip to content

Commit 06cd543

Browse files
derek73claude
andcommitted
Fix spurious '' token in suffix_list after capitalize() with no suffix
''.split(', ') returns [''] just like ''.split(' ') did for the other attributes. Use a filtered list comprehension to preserve the comma delimiter while dropping empty tokens, making suffix_list consistent with the [] invariant the rest of the codebase relies on. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 665f148 commit 06cd543

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

nameparser/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ def capitalize(self, force: bool | None = None) -> None:
989989
self.middle_list = self.cap_piece(self.middle, 'middle').split()
990990
self.last_list = self.cap_piece(self.last, 'last').split()
991991
# suffix is stored comma-separated ("Ph.D., J.D."), not space-separated
992-
self.suffix_list = self.cap_piece(self.suffix, 'suffix').split(', ')
992+
self.suffix_list = [s for s in self.cap_piece(self.suffix, 'suffix').split(', ') if s]
993993

994994
def handle_capitalization(self) -> None:
995995
"""

tests/test_capitalization.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ def test_capitalize_title_and_last_only_no_spurious_tokens(self) -> None:
7676
self.assertEqual(hn.middle_list, [])
7777
self.m(str(hn), 'Dr Doe', hn)
7878

79+
def test_capitalize_empty_suffix_produces_no_spurious_tokens(self) -> None:
80+
# ''.split(', ') returns [''] just like ''.split(' ') did for the other
81+
# attributes — an absent suffix should produce suffix_list == [], not [''].
82+
hn = HumanName('JOHN DOE')
83+
hn.capitalize()
84+
self.assertEqual(hn.suffix_list, [])
85+
86+
def test_capitalize_single_suffix_still_works(self) -> None:
87+
hn = HumanName('JOHN DOE PHD')
88+
hn.capitalize()
89+
self.assertEqual(hn.suffix_list, ['Ph.D.'])
90+
91+
def test_capitalize_multiple_suffixes_still_split_correctly(self) -> None:
92+
hn = HumanName('JOHN DOE PHD MD')
93+
hn.capitalize()
94+
self.assertEqual(hn.suffix_list, ['Ph.D.', 'M.D.'])
95+
7996
# Leaving already-capitalized names alone
8097
def test_no_change_to_mixed_chase(self) -> None:
8198
hn = HumanName('Shirley Maclaine')

0 commit comments

Comments
 (0)