Skip to content

Simplify _match.py - #1432

Merged
derek73 merged 3 commits into
masterfrom
simplify/match
Nov 16, 2026
Merged

derek73 merged 3 commits into
masterfrom
simplify/match

Conversation

@derek73

@derek73 derek73 commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Release checklist step 0: a /simplify pass over nameparser/_match.py (new this cycle). It was reviewed as a whole file from four angles (reuse, simplification, efficiency, altitude). Every change is behavior-identical: no match result, key, repr, message text, exception type or public name moves.

Simplifications

  • Matching.check: under other_readings, the right side's readings are built once. Before, self._readings(right) ran again for every left reading, which redid its Parser.revise sub-parses: Van Gogh / Van Morrison made 3 revise calls where 2 are needed.
  • The middle-remainder step is one helper, Matching._middle_left. _pick_across ranked candidates by removing the consumed middle word and comparing the rest; _resolved then did the same deletion and comparison again for the winner. _pick_across now returns the winner's remainder and outcome, and _resolved uses them.
  • _resolved returns early when across-roles is not accepted. Before, both callers in _result wrapped it in the same conditional.
  • _resolved reads the middle words through _cross_texts(name, Role.MIDDLE), the same text it was building inline with _role_text(...).split().
  • _familiar and _alternate were one function with a different pair table. They are now _paired(sa, sb, pairs), which takes spelling sets already built. _familiar_outcome builds each pair's two spelling sets once instead of three times (once each for listed, alternate and familiar).
  • _keys builds the first given word's spelling set once instead of three times, and loops over one table (_pair_tables) for the familiar-form and alternate-spelling letters instead of repeating the same block for each. _paired_forms now requires its pairs argument.
  • New helpers _letters_of, which replaces four copies of {_letter(g) for g in ...} - {""}, and _row_more, which replaces two copies of a row's more on conflict.
  • _is_initial reads _pipeline._vocab.in_initialless_script instead of a third local copy of the initialless-script matcher, and the _policy imports of _NO_INITIAL_RANGES and _ranges_matcher go. The two agree on every code point and its fold, checked exhaustively. tests/v2/test_script_properties.py read the removed _match._initialless: its column test now asserts through _match._is_initial, and the matcher-agreement test reads _vocab.in_initialless_script.
  • _familiar_outcome: dropped a branch that the line after it already covers (if alt in accept and fam in accept: return fam).
  • _first_half: dropped the redundant "-" not in w, which parts[0] == w already implies.
  • _text_outcomes: dropped defaults that its only caller never uses.
  • _RUN_ROLES moved beside the other role constants. Removed an extra blank line, and the quotes around Lexicon in _Compare.
  • Docstrings and comments:
  • Root AGENTS.md's layering bullet and docs/design/mechanisms.md's script-properties entry now say _match reads the initialless scripts through _vocab.in_initialless_script. AGENTS.md asks for a layering note to be updated in the same change. In tests/v2/test_layering.py only the comment on the _match.py entry changes, to say the same. Its ALLOWED tuple is unchanged: _match already imported _pipeline._vocab, and still imports _policy, for Policy.

Lines: _match.py went from 1,653 to 1,663. The new helpers' docstrings and the import block, which is now on several lines, outweigh the code removed. Code duplication fell; line count did not.

Measurements

  • uv run pytest: 49,786 passed, 45 skipped, 3 xfailed.
  • uv run pytest -n0 -m serial: 38 passed.
  • uv run mypy: no issues. uv run ruff check: clean.
  • match_diff.py --base <detached worktree of origin/master 90b15c84>, run with a private NAMEPARSER_TOOLS_CACHE of symlinks:
    • Result: 0 of 690,382 rows moved (31,381 pairs × 22 settings), and 0 consistent pairs share no key.
    • Harness proof: a scratch copy with two deliberate mutations (_first_half always false; the familiar and alternate given letters dropped from the keys) reported 38,146 rows moved, 18,898 of them keys.
  • git diff --stat origin/master -- tools/differential/snapshot.jsonl: empty.
  • Frames on the reference row (_calls_per_parse, py3.11): 285.8 before, 285.8 after. A parse never enters _match.

Reviewed and not taken

  • Threading one per-comparison spelling-set memo (_SpellingSets keyed by group) through _text_outcomes, _part_agrees and _familiar_outcome. Measured, check('John Smith', 'Jon Smyth') builds _spellings 16 times for about 4 distinct inputs. The change is wider than a cleanup and should be its own PR.
  • Caching each ParsedName's role texts per check.
  • A _RoleRule field for the word-by-word middle reading, and one encoding the family-versus-maiden mode in place of bases plus chain. Both are mechanical but touch every comparator.
  • Moving the NFC step into _capitals_word.

Behavior-changing candidates

None found. Two splits that look like duplication are deliberate, and each carries its reason:

🤖 Generated with Claude Code

derek73 and others added 2 commits November 16, 2026 00:09
…ing set once, read _vocab's initialless test

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…pts in AGENTS.md and mechanisms.md

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@derek73 derek73 added the matching Name matching: Parser.matching(), check() and keys() label Nov 16, 2026
@derek73 derek73 self-assigned this Nov 16, 2026
…me through _pipeline/_vocab

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@derek73
derek73 merged commit a15a13f into master Nov 16, 2026
14 checks passed
@derek73
derek73 deleted the simplify/match branch November 16, 2026 09:32
@derek73 derek73 added this to the 3.1 milestone Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

matching Name matching: Parser.matching(), check() and keys()

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant