You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: add initials_separator; fix or-defaulting for format/delimiter kwargs (#171)
* fix: add initials_separator; fix or-defaulting for format/delimiter kwargs (closes#152)
- Add initials_separator = " " to Constants: controls the joiner between
consecutive initials within a name group, distinct from initials_delimiter
which is the trailing character after each individual initial
- Add initials_separator kwarg to HumanName.__init__
- Fix or-defaulting to is-not-None for string_format, initials_format, and
initials_delimiter kwargs so empty string '' is accepted as a valid value
- Use initials_separator in __process_initial__ and initials() in place of
hardcoded " "
- Document initials_separator in usage.rst with examples
* fix: honor initials_separator kwarg in __process_initial__; fix string_format='' in __str__
- `__process_initial__` was reading `self.C.initials_separator` (Constants)
instead of `self.initials_separator` (per-instance), so the constructor kwarg
was silently ignored for multi-word name parts
- `__str__` used `if self.string_format:` (truthiness) so `string_format=""`
fell through to the default format despite the assignment fix in the PR
- Add regression tests for both kwarg paths through the affected code
- Fix `test_initials_separator_multiword_name_part` to set instance attr instead
of Constants attr, so it tests the now-correct code path
- Correct `Constants.initials_separator` docstring example (requires
initials_format="{first}{middle}{last}" to produce "J.A.D.")
- Clarify usage.rst: initials_separator only removes intra-group spaces;
inter-group spacing is still governed by initials_format
* test: strengthen test suite after PR review
- Add assertNotEqual shim to HumanNameTestBase
- Tighten test_initials_format_empty_string_kwarg: use assertNotEqual +
assertFalse instead of vacuous or-equality check
- Add test_initials_separator_custom_value: non-empty separator on a
multi-word token via __process_initial__
- Add test_str_default_behavior_unchanged: regression guard for the
or→is-not-None __str__ fix
- Remove duplicate test_initials_separator_multiword_name_part
- Wrap CONSTANTS mutation tests in try/finally to ensure state is
restored even when an assertion fails
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: docs/usage.rst
+20-2Lines changed: 20 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -203,8 +203,26 @@ Furthermore, the delimiter for the string output can be set through:
203
203
204
204
>>> HumanName("Doe, John A. Kenneth, Jr.", initials_delimiter=";").initials()
205
205
'J; A; K; D;'
206
-
>>> HumanName("Doe, John A. Kenneth, Jr.", initials_format="{first}{middle}{last}", initials_delimiter=".").initials()
207
-
'J.A. K.D.'
206
+
207
+
The separator between consecutive initials *within* a name group (e.g. two middle
208
+
names) is controlled by :py:attr:`~nameparser.config.Constants.initials_separator`,
209
+
which defaults to ``" "``. Setting it to ``""`` removes that space within a group;
210
+
spacing *between* groups is still governed by ``initials_format``.
211
+
212
+
``initials_delimiter``, ``initials_separator``, and ``initials_format`` work together:
213
+
214
+
- ``initials_delimiter`` — appended *after* each individual initial (default ``"."``)
215
+
- ``initials_separator`` — placed *after* the delimiter between consecutive initials in the same group (default ``" "``), so with ``delimiter="."`` and ``separator=" "`` you get ``A. K.``
216
+
- ``initials_format`` — controls how the first, middle, and last groups are arranged
217
+
218
+
For example, to produce compact period-separated initials with no spaces:
219
+
220
+
.. doctest:: initials separator
221
+
222
+
>>> HumanName("Doe, John A. Kenneth, Jr.", initials_separator="", initials_format="{first}{middle}{last}").initials()
223
+
'J.A.K.D.'
224
+
>>> HumanName("Doe, John A. Kenneth, Jr.", initials_delimiter="", initials_separator="", initials_format="{first}{middle}{last}").initials()
225
+
'JAKD'
208
226
209
227
To get a list representation of the initials, use :py:meth:`~nameparser.HumanName.initials_list`.
210
228
This function is unaffected by :py:attr:`~nameparser.config.Constants.initials_format`
0 commit comments