Skip to content

Commit 50b95d5

Browse files
committed
Fix leading space in surnames after capitalize() with empty middle name
capitalize() split each attribute with str.split(' '), which returns [''] (not []) for an empty string. cap_piece() returns '' for an empty part, so an empty middle name produced middle_list = [''], which leaked into surnames_list (middle_list + last_list) and yielded a leading space in the surnames property: >>> hn = HumanName('john doe'); hn.capitalize(); hn.surnames ' Doe' # leading space (should be 'Doe') The same spurious '' element also appeared in title_list/first_list/last_list for empty attributes. Using str.split() instead returns [] for empty strings and is equivalent for the already-whitespace-collapsed pieces cap_piece() returns. The suffix split (', ') is intentionally left unchanged. Added a regression test in HumanNameCapitalizationTestCase.
1 parent dc44d85 commit 50b95d5

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

nameparser/parser.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -980,10 +980,10 @@ def capitalize(self, force: bool | None = None) -> None:
980980

981981
if not force and not (name == name.upper() or name == name.lower()):
982982
return
983-
self.title_list = self.cap_piece(self.title, 'title').split(' ')
984-
self.first_list = self.cap_piece(self.first, 'first').split(' ')
985-
self.middle_list = self.cap_piece(self.middle, 'middle').split(' ')
986-
self.last_list = self.cap_piece(self.last, 'last').split(' ')
983+
self.title_list = self.cap_piece(self.title, 'title').split()
984+
self.first_list = self.cap_piece(self.first, 'first').split()
985+
self.middle_list = self.cap_piece(self.middle, 'middle').split()
986+
self.last_list = self.cap_piece(self.last, 'last').split()
987987
self.suffix_list = self.cap_piece(self.suffix, 'suffix').split(', ')
988988

989989
def handle_capitalization(self) -> None:

tests.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,6 +2124,28 @@ def test_capitization_middle_initial_is_also_a_conjunction(self) -> None:
21242124
hn.capitalize()
21252125
self.m(str(hn), 'Scott E. Werner', hn)
21262126

2127+
def test_capitalize_empty_name_part_has_no_leading_space_in_surnames(self) -> None:
2128+
# capitalize() splits each attribute with str.split(' '), which returns
2129+
# [''] (rather than []) for an empty string. That spurious element
2130+
# leaked into surnames_list (middle_list + last_list) and produced a
2131+
# leading space in the surnames property, e.g. ' Doe' instead of 'Doe'.
2132+
hn = HumanName('john doe')
2133+
hn.capitalize()
2134+
self.m(hn.surnames, 'Doe', hn)
2135+
self.assertEqual(hn.middle_list, [])
2136+
self.assertEqual(hn.surnames_list, ['Doe'])
2137+
2138+
# force=True on a mixed-case name hits the same code path
2139+
hn = HumanName('Jane Doe')
2140+
hn.capitalize(force=True)
2141+
self.m(hn.surnames, 'Doe', hn)
2142+
self.assertEqual(hn.middle_list, [])
2143+
2144+
# other empty attribute lists are also free of the spurious '' element
2145+
self.assertEqual(hn.title_list, [])
2146+
self.assertEqual(hn.first_list, ['Jane'])
2147+
self.assertEqual(hn.last_list, ['Doe'])
2148+
21272149
# Leaving already-capitalized names alone
21282150
def test_no_change_to_mixed_chase(self) -> None:
21292151
hn = HumanName('Shirley Maclaine')

0 commit comments

Comments
 (0)