Skip to content

Commit 7fbb73c

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 1f46568 commit 7fbb73c

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
@@ -984,10 +984,10 @@ 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(' ')
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()
991991
self.suffix_list = self.cap_piece(self.suffix, 'suffix').split(', ')
992992

993993
def handle_capitalization(self) -> None:

tests/test_capitalization.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ 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_name_part_has_no_leading_space_in_surnames(self) -> None:
44+
# capitalize() split each attribute with str.split(' '), which returns
45+
# [''] (rather than []) for an empty string. That spurious element
46+
# leaked into surnames_list (middle_list + last_list) and produced a
47+
# leading space in the surnames property, e.g. ' Doe' instead of 'Doe'.
48+
hn = HumanName('john doe')
49+
hn.capitalize()
50+
self.m(hn.surnames, 'Doe', hn)
51+
self.assertEqual(hn.middle_list, [])
52+
self.assertEqual(hn.surnames_list, ['Doe'])
53+
54+
# force=True on a mixed-case name hits the same code path
55+
hn = HumanName('Jane Doe')
56+
hn.capitalize(force=True)
57+
self.m(hn.surnames, 'Doe', hn)
58+
self.assertEqual(hn.middle_list, [])
59+
60+
# other empty attribute lists are also free of the spurious '' element
61+
self.assertEqual(hn.title_list, [])
62+
self.assertEqual(hn.first_list, ['Jane'])
63+
self.assertEqual(hn.last_list, ['Doe'])
64+
4365
# Leaving already-capitalized names alone
4466
def test_no_change_to_mixed_chase(self) -> None:
4567
hn = HumanName('Shirley Maclaine')

0 commit comments

Comments
 (0)