Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ The rules below were distilled from PR #588, which restructured `customize.rst`

**A recipe is a doctest; an edge case is a unit test.** A *recipe* — how to do the thing the reader came to do — belongs in the docs as a `.. doctest::` block, which teaches and is CI-checked at once. The line between the two is whether the prose states the behavior: a claim the section makes to the reader (`edd` repairs to `EDD`, not a mask) earns a doctest beside it, which is what keeps the claim true; an edge case the prose has no reason to mention earns one sentence at most and a unit test, never a doctest (PR #216). An example sitting as prose inside a table cell is the worst of both: it teaches from the wrong place and nothing checks it.

**An example must be non-vacuous.** Show the baseline before the customization, and check that they differ: an `add(titles={"chancellor"})` example (2.x's spelling) once shipped (fixed in 0ab19fdb) where `chancellor` was already a default title, so the add was a no-op and the doctest passed demonstrating nothing. Prefer a word absent from the defaults for a teachable reason (`dean`, which is also a given name).
**An example must be non-vacuous.** Show the baseline before the customization, and check that they differ: an `add(titles={"chancellor"})` example (2.x's spelling) once shipped (fixed in 0ab19fdb) where `chancellor` was already a default title, so the add was a no-op and the doctest passed demonstrating nothing. Prefer a word absent from the defaults for a teachable reason (`dean`, which is also a given name). Run each recipe's example without its edit too and check that the output moves: #1534's review found docs/migrate.rst removing `alhaj` as a religious title (it is a style), the doctest green throughout because the output shown held with or without the call. `Lexicon.remove` warns where no file and no earlier add holds the reading named (#1535), which catches that shape, but not an edit that takes a reading the example never reaches.

**An example must run warning-free.** Readers paste examples, so a warning the doctest build prints is one every reader gets. Fix the example, not the build: a `Lexicon.empty()` used only to drop titles also dropped the Korean surnames and drew the segmenterless warning, and the fix was an example that empties only the field it is about. Run the doctest build under Verifying below and expect no output at all.

Expand Down
35 changes: 35 additions & 0 deletions docs/customize.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,41 @@ its category:
>>> Parser(lexicon=lean).parse("Professor Robert Johns").title
''

A remove that can take nothing warns. One naming a kind the word does
not hold lists the kinds it does, one naming a word no vocabulary file
and no earlier ``add`` lists asks you to check its spelling, and each
opens with the call and "changes nothing". ``alhaj`` is a style, not a
religious title:

.. doctest::

>>> import warnings
>>> with warnings.catch_warnings(record=True) as caught:
... warnings.simplefilter("always")
... inert = Lexicon.default().remove("alhaj", kind="religious")
>>> print(caught[0].message)
remove('alhaj', kind='religious') changes nothing: 'alhaj' has no 'religious' reading in any vocabulary file or earlier add(); it holds: style. Did you mean remove('alhaj', kind='style')?
>>> Parser(lexicon=inert).parse("Alhaj Ahmed Khan").title
'Alhaj'

A reading some file holds counts even where it is off by default, since
a choice of languages may turn it on, and so does one the same lexicon
added earlier. A fragment meant to remove a word another lexicon adds
warns, since ``remove`` cannot see the lexicon it will be joined with.
Do the remove on the joined lexicon, or filter that one warning by its
message, never by its category, which would hide every other warning:

.. doctest::

>>> with warnings.catch_warnings():
... warnings.filterwarnings("ignore", message=r"remove\('zzq'")
... fragment = Lexicon.empty().remove("zzq", kind="title")
>>> with_zzq = Lexicon.default().add("zzq", kind="title")
>>> Parser(lexicon=with_zzq).parse("Zzq Ana Smith").title
'Zzq'
>>> Parser(lexicon=with_zzq | fragment).parse("Zzq Ana Smith").title
''

Seeing what the vocabulary says about a word
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
3 changes: 3 additions & 0 deletions docs/design/decisions/vocabulary-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The vocabulary was a set of base sets plus marker subsets (`particles` and `part
- The configure-by-reading API: `add(*words, kind, **properties)` and `remove(*words, kind=None)` (a `familiar` or `spelling` remove also taking the pairs to drop, `short_for=[...]` or `spelling_of=[...]`, 2026-11-15 #1404), the views reading a word's kind from the lexicon, `readings_of` returning a public `LexiconReading` and a repr counting readings by kind, a five-part state (base, ordered edit log, choice, lists, masks) compared structurally, a clash a later choice exposes raised at `parser_for` naming the choice, the choice recorded on the `Lexicon` with chained choices unioning, the default's base lazy, built in 4i-a to 4i-d with no default parse moving; since 3.1 a 2.x pickle is refused, naming the re-pickle under 3.0, and an old spelling refused naming docs/migrate.rst's table of kinds — 2026-11-02 #754, the `Lexicon` surface (Derek); 2026-11-02 #754, sub-phase 4i-a; 2026-11-02 #754, sub-phase 4i-b; 2026-11-02 #754, sub-phase 4i-c; 2026-11-02 #754, sub-phase 4i-d; 2026-11-08 #1003.
- A whole kind is taken out with `remove(kind=..., every=True)`, every word's readings of it going, those a later choice turns on included, and `words(kind=..., **properties)` lists the words holding a reading of a kind or category — 2026-11-02 #1007, a kind as a whole (Derek).
- Configured by reading, an `add` of a title reading that a `given` reading keeps a title only inside a phrase warns, naming `remove(w, kind="given")`, save one that only updates a held title reading's properties — 2026-11-01 #754, the title warning (Derek).
- A `remove` of a reading no vocabulary file holds, on by default or not, and no earlier `add` of the lexicon gave, warns, its message naming which of five cases it is; a remove of every word's readings and a `familiar` or `spelling` remove are not judged, a fragment removing a word another lexicon adds warns too, and an `add` repeating a held reading does not — 2026-11-22 #1535.
- A `Locale` may carry CLDR-shaped render patterns, display and sorting, a later addition whose shape is agreed; how a call reaches one is open — 2026-10-28 #754, render patterns.
- 3.0's output model keeps the seven roles and adds every new field as a view, read from `Token.reading`, the reading each token took, or for `surname2` from rules/O6's tag: `generation`, `credentials` and, since 2026-11-03, `honorifics` partitioning `suffix` (decisions/R8), `patronymic`, and `surname2` where rules/O6 read two family names; `middle` and `surnames` keep their meaning, a compound given name being a rule a caller or pack turns on; the default `str()` renders a name in the order it was read in with its written separators in a normalized form, through `render(spec: str | None = None)`, `None` being that default; a language file's `[rules]` compile into its `Locale`'s policy patch — 2026-10-28 #754 (Derek).
- `surname2` is read from O6's mark as `Token.reading` publishes it, `Reading.second_surname`, a field not compared, and not from a published tag, which would make an O6 parse unequal to its 2.x pickle (decisions/R10) — 2026-10-29 #754, sub-phase 4d.
Expand Down Expand Up @@ -653,3 +654,5 @@ Open: [#1145](https://github.com/derek73/python-nameparser/issues/1145), the dis
- **Accepted:** under strict `ms`, a ruler's style opening a name reads the given name, as it does by default and under `locales.MS` (`Sultan Rahmat ibni Sultan Kamil` → given 'Sultan'); behind a style it stays the title where the style is read (`His Majesty Sultan Rahmat` under strict `ms` + `en`; strict `ms` alone drops en.toml's `his` and `majesty`, before this change and after).
- **The pairs re-taken here stay ungated, as wanted:** under a strict choice the word is the particle that language writes. Re-taken 2026-11-21 on `81a24221` by the same score with each tag's ungated words given back their `given` reading: `ben` under strict `ar` fixes 62 records (Tunisia 45, Algeria 9, Morocco 8), `bin` under strict `ms` 6 (ORCID 2, PatentsView 4), `del`, `la`, `san` and `santa` under strict `it` 7, `del`, `la` and `san` under strict `fil` 2, `da`, `do` and `santa` under strict `pt-BR` 1, each breaking none. Read record by record, nearly all are a family name written first with the boxes filed that way round (`Ben Amri Salma`, given box `Ben Amri`): strict reads the whole name as the family (rules/P1's fold, `Ben Amri Salma` → family 'Ben Amri Salma'), wrong too, and the gate agrees with the boxes only because it reads the first word as a given name, so neither reading is the name and the gate's is no better. The rest are a given name the particle is spelled like: of another origin (`Bin Qiaoyu`, the Chinese given name, in Malaysia; `La Thuy`), a cost accepted under strict's premise that its data is of one origin (docs/locales.rst, "Reading one language only": strict is "for data you know to be of one origin"), which answers the issue's second question for `Bin`; and one of the language's own (`La Reina Dimaculangan`, a Philippine given name), a cost of reading `la` as the particle that the premise does not answer, left with the pair.
- **Not weighed here:** the rest of `STRICT_UNGATED`, which stays as it was: `gd`, `hr`, `sk`, `sl` and `hi`, and `marquis`, `marquise`, `tho`, `st` and `sushri`, which the issue's samples held too rarely to say anything (its first open question); the pairs of `nl`, `fr`, `ca` and `ga`, which moved none of the 822 records the issue's table read for them, so no reading of a record weighs them; `pt` and `pt-PT` (the issue's table read `pt` with `pt-BR` and holds no `pt-PT`; only `pt-BR` is re-taken here); and `my`'s ranks, which joined after the issue (#1483) and are titles, not particles, their given names written only in en.toml. None is decided as wanted by this entry (#1463).

- 2026-11-22 #1535 — `Lexicon.remove` WARNS WHERE NO VOCABULARY FILE HOLDS THE READING IT NAMES, AND NO EARLIER `add` OF THE LEXICON GAVE IT. Decided 2026-11-22. A remove of a kind the word does not hold, or of a word in no file, took nothing and said nothing, so it could not be told from a working one: docs/migrate.rst's recipe removed `alhaj` as `kind="religious"`, a style, and its doctest passed because the assertion shown held with or without the call (found by #1534's review). THE RULE: for each word of `remove(*words, kind=...)`, the kinds held are every kind the reading index gives the word (`_reading_index.readings_of`, every file's rows whether on by default or not, the word looked up as `remove` stores it) and every kind an `add` earlier in the lexicon's log gave it, a joined lexicon's included; the call warns where no held kind is the one asked or, for a category, of it, and with no kind where the word holds none. A word named twice in one call warns once. THE MESSAGE opens with the call and "changes nothing", so a caller filters one by it, and names its case: (i) the word holds other kinds, listed, with `remove(word, kind=K)` offered only where one kind is plainly meant (the single held kind of the asked kind's category, else the single held kind of all) and never a `familiar` or `spelling` kind, whose remove reaches name matching's pairs; (ii) the word holds no reading but stands in the `surnames` or `given_names` list, which `remove` does not edit, and the kind asked is the list's (none, `family` or `name` for the surnames, none, `given` or `name` for the given names), with the `dataclasses.replace` that drops it offered; (iii) it holds no reading of its own but is the other name of other words' `familiar` or `spelling` pairs, found in the reading index as well as the lexicon's compiled pairs, with the pair remove offered; (iv) it is a suffix acronym or a dotted title written with its full stops, the spellings the parse reads as the bare word (`remove("Ph.D.", kind="credential")`, stored `ph.d`, the vocabulary's `phd`), and the message names the bare word; it offers the call on the bare word where the reading the dotted spelling is read by (the bare word's suffix reading if it is a suffix acronym, else its title reading, in the files or an earlier add) is of the kind asked, or, with no kind asked, is of exactly one kind, which the call names (`remove("M.D.")` offers `remove("md", kind="credential")`, leaving `md`'s given reading, Muhammad written short), and otherwise lists the kinds the dotted spelling is read by and offers nothing; a suffix word written with stops is no such spelling (`J.R.` is not read as `jr`). Accepted: the offered call takes the vocabulary's reading, while rules/S3's dotted shape (`Policy.unlisted_dotted_suffixes`) still reads `Ph.D.` or `D.D.S.` as a suffix by its shape; (v) it is in no file and no earlier add, and the message says to check its spelling or, where a lexicon this one is joined with adds it, to remove it from the joined lexicon. Each offered call is run by a test and checked to take what the inert one missed. Not judged: a remove with no words (`every=True`), and the `familiar` and `spelling` pair removes, which match by folded forms through name matching's index. WHERE: in `Lexicon.remove` alone, through `_warn_at_caller`, so it names the caller's own line through a chained call and inside `Parser(lexicon=...)`; never where a constructor replays the log or a pickle loads. WHY A WARNING: the edit misparses nothing, so a raise is wrong by the invariants rule (AGENTS.md: invariants guard harm, not no-ops), and it is statically decidable and indistinguishable from a working edit, the construction-time warning's case. ACCEPTED, decided 2026-11-22 over staying silent on a fragment built on `Lexicon.empty()`, which would have cost every hand-built fragment its typo check, packs included: a fragment meant to remove a word another lexicon adds warns, since `remove` cannot see the lexicon it will be joined with (tests/v2/test_parser.py's pack-fragment join asserts it); its caller filters that warning by its message, never by category, or does the remove on the joined lexicon, as case (v)'s message and docs/customize.rst say. A fragment removing a file's reading, which is how a pack is written, stays silent. A word spelled in a way the fold does not reach warns as unknown, the intended signal. DECLINED: the same warning on an `add` of a reading the word already holds with the same properties, the issue's open point. An inert remove leaves a reading the caller wanted gone, so the parse is not what they asked for; a redundant add leaves the reading the caller wanted present, so the parse is what they asked for, and its usual cause is a recipe a later release made unnecessary, so the warning would fire on working code after an upgrade each time a release ships a word a caller had added, teaching callers to ignore it. MEASURED on `ffc5d23c` with this change: no parse moves (`tools/differential/snapshot.jsonl` byte-identical), and the reference parse's frames do not move (`tools/perf/call_count.py --against origin/master`: 195.72 on py3.11 both sides), `remove` not being on the parse path; no shipped pack, plain or strict, warns; outside this change's own tests, three removes in the tests named readings nothing held (tests/v2/test_lexicon.py's remove-before-add, test_match.py's `zzrobert`, test_parser.py's pack fragment), each exercising an inert remove on purpose; in docs/, README.rst and tools/, none besides the two examples docs/customize.rst adds to show the warning.
1 change: 1 addition & 0 deletions docs/release_log.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Release Log
- **Change locales.HR and locales.SL to read two family names, as locales.CS and locales.SK do.** ``parser_for(locales.HR).parse("Ana Puž Kukuljan")`` gives family ``Puž Kukuljan``, where 3.0.0 through 3.1.0 gave middle ``Puž``, family ``Kukuljan``; with no given-name list, a second given name is read as a family name too (``Janez Jože Novak`` gives family ``Jože Novak`` under ``locales.SL``), and a family comma keeps both as before. :issue:`1190`
- **Change locales.PT_BR to read two family names, and add locales.PT_PT, which reads names from Portugal the same way.** ``parser_for(locales.PT_BR).parse("Ana Souza Lima")`` gives family ``Souza Lima``, where 3.0.0 through 3.1.0 gave middle ``Souza``, family ``Lima``; a second given name on the packs' Portuguese given-name list stays where it stands (``Ana Beatriz Costa`` keeps family ``Costa``), and ``locales.PT``, chosen for Portuguese names from any country, is unchanged. ``locales.PT_PT`` also reads four given names Portugal writes behind a particle and Brazil does not (``Maria do Rosário Pinto`` gives middle ``do Rosário``, as ``do Carmo``, ``da Conceição`` and ``da Luz`` do). :issue:`1453`, :issue:`1189`
- **Change the Spanish given-name lists, rewritten from their tool.** ``parser_for(locales.ES_AR).parse("Juan Gerhard Lopez")`` gives family ``Gerhard Lopez``, where 3.0.0 through 3.1.0 gave middle ``Gerhard``; nine words leave locales.ES_AR's list (``Gerhard``, ``Zuleyma`` among them) and nine join it (``Azhar``, ``Houari``, ``Grace``), each joining reading as a middle name where it read as the first family name, and locales.ES's gains ``Lahcen`` and drops ``Rim``. :issue:`1471`, :issue:`1476`
- **Add a warning when Lexicon.remove names a reading no vocabulary file holds, which took nothing and said nothing.** ``Lexicon.default().remove("alhaj", kind="religious")`` now warns ``remove('alhaj', kind='religious') changes nothing: 'alhaj' has no 'religious' reading in any vocabulary file or earlier add(); it holds: style. Did you mean remove('alhaj', kind='style')?``, where 3.0.0 through 3.1.0 returned the lexicon unchanged in silence. A fragment that removes a word another lexicon adds, when the two are joined, warns too, since the call cannot see that lexicon. :issue:`1535`

* 3.1.0 - November 16, 2026

Expand Down
Loading