Skip to content

Commit 5c19547

Browse files
authored
Merge pull request #164 from patchwright/fix/capitalize-leading-space
Fix leading space in surnames after capitalize() with empty middle name
2 parents 1f46568 + 06cd543 commit 5c19547

2 files changed

Lines changed: 59 additions & 5 deletions

File tree

nameparser/parser.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -984,11 +984,12 @@ def capitalize(self, force: bool | None = None) -> None:
984984

985985
if not force and not (name == name.upper() or name == name.lower()):
986986
return
987-
self.title_list = self.cap_piece(self.title, 'title').split(' ')
988-
self.first_list = self.cap_piece(self.first, 'first').split(' ')
989-
self.middle_list = self.cap_piece(self.middle, 'middle').split(' ')
990-
self.last_list = self.cap_piece(self.last, 'last').split(' ')
991-
self.suffix_list = self.cap_piece(self.suffix, 'suffix').split(', ')
987+
self.title_list = self.cap_piece(self.title, 'title').split()
988+
self.first_list = self.cap_piece(self.first, 'first').split()
989+
self.middle_list = self.cap_piece(self.middle, 'middle').split()
990+
self.last_list = self.cap_piece(self.last, 'last').split()
991+
# suffix is stored comma-separated ("Ph.D., J.D."), not space-separated
992+
self.suffix_list = [s for s in self.cap_piece(self.suffix, 'suffix').split(', ') if s]
992993

993994
def handle_capitalization(self) -> None:
994995
"""

tests/test_capitalization.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,59 @@ def test_capitization_middle_initial_is_also_a_conjunction(self) -> None:
4040
hn.capitalize()
4141
self.m(str(hn), 'Scott E. Werner', hn)
4242

43+
def test_capitalize_empty_middle_produces_no_leading_space_in_surnames(self) -> None:
44+
# str.split(' ') on an empty string returns [''] rather than [], so an
45+
# absent middle produced a spurious token that leaked into surnames_list
46+
# and caused a leading space in the surnames property (' Doe' not 'Doe').
47+
hn = HumanName('john doe')
48+
hn.capitalize()
49+
self.m(hn.surnames, 'Doe', hn)
50+
self.assertEqual(hn.middle_list, [])
51+
self.assertEqual(hn.surnames_list, ['Doe'])
52+
53+
def test_capitalize_force_empty_middle_produces_no_leading_space_in_surnames(self) -> None:
54+
# Without force=True, capitalize() exits early for mixed-case names and
55+
# never reaches the split lines. Confirm the fix covers that path too.
56+
hn = HumanName('Jane Doe')
57+
hn.capitalize(force=True)
58+
self.m(hn.surnames, 'Doe', hn)
59+
self.assertEqual(hn.middle_list, [])
60+
61+
def test_capitalize_empty_attributes_produce_no_spurious_tokens(self) -> None:
62+
# Confirm the fix extends beyond surnames: empty attribute lists are []
63+
# not [''], and non-empty ones contain only real tokens.
64+
hn = HumanName('Jane Doe')
65+
hn.capitalize(force=True)
66+
self.assertEqual(hn.title_list, [])
67+
self.assertEqual(hn.first_list, ['Jane'])
68+
self.assertEqual(hn.last_list, ['Doe'])
69+
70+
def test_capitalize_title_and_last_only_no_spurious_tokens(self) -> None:
71+
# title+last with no first or middle leaves first_list and middle_list
72+
# both empty. All-caps triggers capitalize() without force=True.
73+
hn = HumanName('DR DOE')
74+
hn.capitalize()
75+
self.assertEqual(hn.first_list, [])
76+
self.assertEqual(hn.middle_list, [])
77+
self.m(str(hn), 'Dr Doe', hn)
78+
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+
4396
# Leaving already-capitalized names alone
4497
def test_no_change_to_mixed_chase(self) -> None:
4598
hn = HumanName('Shirley Maclaine')

0 commit comments

Comments
 (0)