Skip to content

Commit 56b2620

Browse files
committed
fix: guard _on_change in add_with_encoding/clear against no-op mutations; correct lc() docstrings
- add_with_encoding and clear now only fire _on_change when the set actually changes, consistent with remove()'s existing changed guard - Correct 'no periods' to 'leading/trailing-periods-stripped' in lc(), is_prefix, is_first_name_prefix, and add_with_encoding docstrings
1 parent c130d40 commit 56b2620

3 files changed

Lines changed: 14 additions & 11 deletions

File tree

‎nameparser/config/__init__.py‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def __len__(self) -> int:
8787

8888
def add_with_encoding(self, s: str, encoding: str | None = None) -> None:
8989
"""
90-
Add the lower case and no-period version of the string to the set. Pass an
90+
Add the lowercased, leading/trailing-periods-stripped version of the string to the set. Pass an
9191
explicit `encoding` parameter to specify the encoding of binary strings that
9292
are not DEFAULT_ENCODING (UTF-8).
9393
"""
@@ -97,13 +97,15 @@ def add_with_encoding(self, s: str, encoding: str | None = None) -> None:
9797
encoding = encoding or stdin_encoding or DEFAULT_ENCODING
9898
if isinstance(s, bytes):
9999
s = s.decode(encoding)
100-
self.elements.add(lc(s))
101-
if self._on_change:
102-
self._on_change()
100+
normalized = lc(s)
101+
if normalized not in self.elements:
102+
self.elements.add(normalized)
103+
if self._on_change:
104+
self._on_change()
103105

104106
def add(self, *strings: str) -> Self:
105107
"""
106-
Add the lower case and no-period version of the string arguments to the set.
108+
Add the lowercased, leading/trailing-periods-stripped version of the string arguments to the set.
107109
Can pass a list of strings. Returns ``self`` for chaining.
108110
"""
109111
for s in strings:
@@ -127,9 +129,10 @@ def remove(self, *strings: str) -> Self:
127129

128130
def clear(self) -> Self:
129131
"""Remove all entries from the set. Returns ``self`` for chaining."""
130-
self.elements.clear()
131-
if self._on_change:
132-
self._on_change()
132+
if self.elements:
133+
self.elements.clear()
134+
if self._on_change:
135+
self._on_change()
133136
return self
134137

135138

‎nameparser/parser.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ def is_conjunction(self, piece: str) -> bool:
551551

552552
def is_prefix(self, piece: str) -> bool:
553553
"""
554-
Lowercase and no periods version of piece is in the
554+
Lowercased, leading/trailing-periods-stripped version of piece is in the
555555
:py:data:`~nameparser.config.prefixes.PREFIXES` set.
556556
"""
557557
if isinstance(piece, list):
@@ -562,7 +562,7 @@ def is_prefix(self, piece: str) -> bool:
562562
return lc(piece) in self.C.prefixes
563563

564564
def is_first_name_prefix(self, piece: str) -> bool:
565-
"""Lowercase and no periods version of piece is in :py:attr:`~nameparser.config.Constants.first_name_prefixes`."""
565+
"""Lowercased, leading/trailing-periods-stripped version of piece is in :py:attr:`~nameparser.config.Constants.first_name_prefixes`."""
566566
return lc(piece) in self.C.first_name_prefixes
567567

568568
def _join_first_name_prefix(self, pieces: list[str], reserve_last: bool) -> list[str]:

‎nameparser/util.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
def lc(value: str) -> str:
14-
"""Lower case and remove any periods to normalize for comparison."""
14+
"""Lowercase and strip leading/trailing periods to normalize for comparison."""
1515
if not value:
1616
return ''
1717
return value.lower().strip('.')

0 commit comments

Comments
 (0)