Skip to content

Commit e8d4522

Browse files
authored
Merge pull request #213 from derek73/fix/doctest-state-leaks-and-ci
Fix shared-CONSTANTS state leaks in doctests, wire up doctest CI
2 parents 54d2f46 + ff09147 commit e8d4522

4 files changed

Lines changed: 29 additions & 14 deletions

File tree

.github/workflows/python-package.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,5 @@ jobs:
4444
python -m build --sdist
4545
twine check dist/*
4646
sphinx-build -b html docs dist/docs
47+
sphinx-build -b doctest docs dist/doctest
48+
python -m doctest README.rst

docs/customize.rst

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,9 @@ constant so that "Hon" can be parsed as a first name.
345345
:options: +ELLIPSIS, +NORMALIZE_WHITESPACE
346346

347347
>>> from nameparser import HumanName
348-
>>> hn = HumanName("Hon Solo")
348+
>>> from nameparser.config import Constants
349+
>>> constants = Constants()
350+
>>> hn = HumanName("Hon Solo", constants=constants)
349351
>>> hn
350352
<HumanName : [
351353
title: 'Hon'
@@ -356,10 +358,9 @@ constant so that "Hon" can be parsed as a first name.
356358
nickname: ''
357359
maiden: ''
358360
]>
359-
>>> from nameparser.config import CONSTANTS
360-
>>> CONSTANTS.titles.remove('hon')
361-
SetManager({'right', ..., 'tax'})
362-
>>> hn = HumanName("Hon Solo")
361+
>>> constants.titles.remove('hon')
362+
SetManager({'10th', ..., 'zoologist'})
363+
>>> hn = HumanName("Hon Solo", constants=constants)
363364
>>> hn
364365
<HumanName : [
365366
title: ''
@@ -374,7 +375,11 @@ constant so that "Hon" can be parsed as a first name.
374375

375376
If you don't want to detect any titles at all, you can remove all of them:
376377

377-
>>> CONSTANTS.titles.clear()
378+
.. doctest::
379+
:options: +ELLIPSIS, +NORMALIZE_WHITESPACE
380+
381+
>>> constants.titles.clear()
382+
SetManager(set())
378383

379384

380385
Adding a Title
@@ -399,7 +404,7 @@ making them lower case and removing periods.
399404
>>> from nameparser.config import Constants
400405
>>> constants = Constants()
401406
>>> constants.titles.add('dean', 'Chemistry')
402-
SetManager({'right', ..., 'tax'})
407+
SetManager({'10th', ..., 'zoologist'})
403408
>>> hn = HumanName("Assoc Dean of Chemistry Robert Johns", constants=constants)
404409
>>> hn
405410
<HumanName : [
@@ -427,7 +432,7 @@ the config on one instance could modify the behavior of another instance.
427432
>>> from nameparser import HumanName
428433
>>> instance = HumanName("")
429434
>>> instance.C.titles.add('dean')
430-
SetManager({'right', ..., 'tax'})
435+
SetManager({'10th', ..., 'zoologist'})
431436
>>> other_instance = HumanName("Dean Robert Johns")
432437
>>> other_instance # Dean parses as title
433438
<HumanName : [
@@ -455,7 +460,7 @@ reference to the module-level config values with the behavior described above.
455460
>>> instance.has_own_config
456461
False
457462
>>> instance.C.titles.add('dean')
458-
SetManager({'right', ..., 'tax'})
463+
SetManager({'10th', ..., 'zoologist'})
459464
>>> other_instance = HumanName("Dean Robert Johns", None) # <-- pass None for per-instance config
460465
>>> other_instance
461466
<HumanName : [
@@ -483,8 +488,8 @@ You can turn this off by setting the ``emoji`` regex to ``False``.
483488
>>> constants = Constants()
484489
>>> constants.regexes.emoji = False
485490
>>> hn = HumanName("Sam 😊 Smith", constants=constants)
486-
>>> hn
487-
"Sam 😊 Smith"
491+
>>> str(hn)
492+
'Sam 😊 Smith'
488493

489494
Config Changes May Need Parse Refresh
490495
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -529,9 +534,9 @@ directly to the attribute.
529534
>>> hn = HumanName("Dr. John A. Kenneth Doe")
530535
>>> hn.title = ["Associate","Professor"]
531536
>>> hn.suffix = "Md."
532-
>>> hn.suffix
537+
>>> hn
533538
<HumanName : [
534-
title: 'Associate Processor'
539+
title: 'Associate Professor'
535540
first: 'John'
536541
middle: 'A. Kenneth'
537542
last: 'Doe'

docs/usage.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ To apply capitalization to all `HumanName` instances, set
112112
>>> name = HumanName("bob v. de la macdole-eisenhower phd")
113113
>>> str(name)
114114
'Bob V. de la MacDole-Eisenhower Ph.D.'
115+
>>> CONSTANTS.capitalize_name = False
115116

116117
To force the capitalization of mixed case strings on all `HumanName` instances,
117118
set :py:attr:`~nameparser.config.Constants.force_mixed_case_capitalization` to `True`.
@@ -125,6 +126,7 @@ set :py:attr:`~nameparser.config.Constants.force_mixed_case_capitalization` to `
125126
>>> name.capitalize()
126127
>>> str(name)
127128
'Shirley MacLaine'
129+
>>> CONSTANTS.force_mixed_case_capitalization = False
128130

129131

130132
Nickname Handling

nameparser/config/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ def __call__(self) -> Set[str]:
7575
return self.elements
7676

7777
def __repr__(self) -> str:
78-
return f"SetManager({self.elements})" # used for docs
78+
# Sorted so repr is stable across runs -- set() iteration order
79+
# depends on string hash randomization, which varies per process.
80+
elements = "{" + ", ".join(repr(e) for e in sorted(self.elements)) + "}" if self.elements else "set()"
81+
return f"SetManager({elements})" # used for docs
7982

8083
def __iter__(self) -> Iterator[str]:
8184
return iter(self.elements)
@@ -363,6 +366,7 @@ class Constants:
363366
None
364367
>>> name.first
365368
'John'
369+
>>> CONSTANTS.empty_attribute_default = ''
366370
367371
"""
368372

@@ -378,6 +382,7 @@ class Constants:
378382
>>> name = HumanName("bob v. de la macdole-eisenhower phd")
379383
>>> str(name)
380384
'Bob V. de la MacDole-Eisenhower Ph.D.'
385+
>>> CONSTANTS.capitalize_name = False
381386
382387
"""
383388

@@ -394,6 +399,7 @@ class Constants:
394399
>>> name.capitalize()
395400
>>> str(name)
396401
'Shirley MacLaine'
402+
>>> CONSTANTS.force_mixed_case_capitalization = False
397403
398404
"""
399405

0 commit comments

Comments
 (0)