Skip to content

Commit 211f8c3

Browse files
committed
Address PR review: pin down suffix_delimiter edge cases, clarify comments
- Lock in hn.middle for the "Doe, Mary - Kate, RN" case so the stray hyphen artifact (a pre-existing tokenization quirk, reproducible without suffix_delimiter set) can't silently drift. - Add coverage for the parts[1:] loop expanding more than one comma segment, for a multi-word token on one side of the delimiter in the detection check, and for delimiter no-op parity with the no-delimiter baseline when the format isn't detected as suffix-comma. - Note in expand_suffix_delimiter's docstring that it's a no-op without a configured delimiter, and comment why detection needs the delimiter-expanded view of parts[1].
1 parent 021823e commit 211f8c3

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

nameparser/parser.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,8 @@ def expand_suffix_delimiter(self, part: str) -> list[str]:
678678
"""Split a single post-comma part on :py:attr:`suffix_delimiter`,
679679
if configured. Used only at suffix-consumption sites, where a part
680680
has already been identified as a suffix group, so splitting it
681-
further can't misparse an unrelated name segment.
681+
further can't misparse an unrelated name segment. Returns ``[part]``
682+
unchanged if no delimiter is configured.
682683
"""
683684
if not self.suffix_delimiter:
684685
return [part]
@@ -1067,6 +1068,9 @@ def parse_full_name(self) -> None:
10671068

10681069
post_comma_pieces = self.parse_pieces(parts[1].split(' '), 1)
10691070

1071+
# Detection must see the delimiter-expanded words too, or a
1072+
# delimiter-joined suffix group like "RN - CRNA" would never be
1073+
# recognized as suffix-comma format in the first place.
10701074
suffix_delimiter_pieces = [word for part in self.expand_suffix_delimiter(parts[1])
10711075
for word in part.split(' ')]
10721076

tests/test_suffixes.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,40 @@ def test_suffix_delimiter_inverted_format_not_misparsed(self) -> None:
273273
self.m(hn.first, "Mary", hn)
274274
self.m(hn.last, "Doe", hn)
275275
self.m(hn.suffix, "RN", hn)
276+
# "Kate" stays in the given-name segment rather than being pulled
277+
# into the suffix, since it's separated from "RN" by its own comma.
278+
# The bare "-" landing in middle is a pre-existing, delimiter-
279+
# independent quirk of tokenizing a lone hyphen (reproducible with
280+
# suffix_delimiter unset), not something this fix is responsible for.
281+
self.m(hn.middle, "- Kate", hn)
282+
283+
def test_suffix_delimiter_expands_each_comma_segment(self) -> None:
284+
# parts[1:] holds two separate comma segments here ("MD - PhD" and
285+
# "FACS"); each must be expanded on its own, not just the first.
286+
hn = HumanName("John Doe, MD - PhD, FACS", suffix_delimiter=" - ")
287+
self.m(hn.first, "John", hn)
288+
self.m(hn.last, "Doe", hn)
289+
self.m(hn.suffix, "MD, PhD, FACS", hn)
290+
291+
def test_suffix_delimiter_detection_with_multi_word_side(self) -> None:
292+
# The suffix-comma detection check flattens on spaces after
293+
# expanding on the delimiter, so a multi-word token on one side of
294+
# the delimiter is still tokenized correctly.
295+
hn = HumanName("Doe, John, MD PhD - FACS Fellow", suffix_delimiter=" - ")
296+
self.m(hn.first, "John", hn)
297+
self.m(hn.last, "Doe", hn)
298+
self.m(hn.suffix, "MD PhD, FACS Fellow", hn)
299+
300+
def test_suffix_delimiter_no_effect_when_not_suffix_comma(self) -> None:
301+
# When the comma format isn't recognized as suffix-comma (here the
302+
# last-name part is a single word), the delimiter must not affect
303+
# parsing at all: output should match the no-delimiter baseline.
304+
with_delim = HumanName("Smith, MD - PhD - FACS", suffix_delimiter=" - ")
305+
without_delim = HumanName("Smith, MD - PhD - FACS")
306+
self.assertEqual(
307+
(with_delim.first, with_delim.middle, with_delim.last, with_delim.suffix),
308+
(without_delim.first, without_delim.middle, without_delim.last, without_delim.suffix),
309+
)
276310

277311
def test_suffix_acronyms_ambiguous_is_customizable(self) -> None:
278312
from nameparser.config import Constants

0 commit comments

Comments
 (0)