Skip to content

Commit d9a07f7

Browse files
derek73claude
andcommitted
Small cleanups from the review sweep
_lexicon.__setstate__: compute the drift set once per field instead of three times, in _VOCAB_FIELDS declaration order rather than alphabetized by formatted string, and drop the intermediate dict that existed only to host a cast. _types.ParsedName.__post_init__: nest the token-subset check under the ambiguities guard rather than giving one local two types to skip work the loop already skips. _config_shim: inline a local used once. tests: drop a bare-string __setstate__ test fully subsumed by the parametrized case above it, and stop parsing the same input twice in the unmatched-close assertion. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent cea2a28 commit d9a07f7

3 files changed

Lines changed: 17 additions & 27 deletions

File tree

nameparser/_types.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -380,17 +380,19 @@ def __post_init__(self) -> None:
380380
# stray delimiters does), and the linear form made construction
381381
# quadratic in their product. Set membership uses the same value
382382
# equality the tuple scan did -- Token is frozen and hashable.
383-
known = set(self.tokens) if self.ambiguities else ()
384-
for amb in self.ambiguities:
385-
for tok in amb.tokens:
386-
# membership is by Token's value equality, not identity:
387-
# this only guarantees a value-equal token exists in
388-
# self.tokens, not that `tok` IS one of those objects.
389-
if tok not in known:
390-
raise ValueError(
391-
f"Ambiguity token {tok.text!r} is not a subset of "
392-
f"this ParsedName's tokens"
393-
)
383+
if self.ambiguities:
384+
known = set(self.tokens)
385+
for amb in self.ambiguities:
386+
for tok in amb.tokens:
387+
# membership is by Token's value equality, not
388+
# identity: this only guarantees a value-equal token
389+
# exists in self.tokens, not that `tok` IS one of
390+
# those objects.
391+
if tok not in known:
392+
raise ValueError(
393+
f"Ambiguity token {tok.text!r} is not a "
394+
f"subset of this ParsedName's tokens"
395+
)
394396

395397
def __bool__(self) -> bool:
396398
return bool(self.tokens)

tests/v2/pipeline/test_extract.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,10 @@ def test_unmatched_close_is_reported(text: str, char: str) -> None:
194194
# the scan is opener-driven, so a close with no open to its left was
195195
# never in its search space and went unreported -- even though the
196196
# AmbiguityKind docstring promises "or closed without opening"
197-
kinds = [a.kind for a in parse(text).ambiguities]
198-
assert kinds == [AmbiguityKind.UNBALANCED_DELIMITER]
199-
assert char in parse(text).ambiguities[0].detail
197+
ambiguities = parse(text).ambiguities
198+
assert [a.kind for a in ambiguities] == [
199+
AmbiguityKind.UNBALANCED_DELIMITER]
200+
assert char in ambiguities[0].detail
200201

201202

202203
def test_word_final_apostrophe_is_not_an_unbalanced_delimiter() -> None:

tests/v2/test_facade.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -326,19 +326,6 @@ def test_setstate_rejects_malformed_component_lists(bad: object) -> None:
326326
loaded.__setstate__(state)
327327

328328

329-
def test_setstate_rejects_a_bare_string_component_list() -> None:
330-
# a genuine v1 pickle stores lists, but __setstate__ reads foreign
331-
# state: a bare str is iterable, so entry.split() over its
332-
# CHARACTERS silently produced a plausible-looking wrong name
333-
# ("John" -> first "J o h n") instead of failing at the load site
334-
n = HumanName("John Smith")
335-
state = n.__getstate__()
336-
state["first_list"] = "John"
337-
loaded = HumanName.__new__(HumanName)
338-
with pytest.raises(TypeError, match="first_list"):
339-
loaded.__setstate__(state)
340-
341-
342329
def test_setstate_restores_a_foreign_multi_word_suffix_entry() -> None:
343330
# the parametrized round-trip tests above go through v2's own
344331
# __getstate__, which is self-consistent by construction. The case

0 commit comments

Comments
 (0)