Title: Remove __eq__ and __hash__ in 2.0; compare names via explicit methods · Issue #223 · derek73/python-nameparser · GitHub
Open Graph Title: Remove __eq__ and __hash__ in 2.0; compare names via explicit methods · Issue #223 · derek73/python-nameparser
X Title: Remove __eq__ and __hash__ in 2.0; compare names via explicit methods · Issue #223 · derek73/python-nameparser
Description: Summary 2.0 will remove HumanName.__eq__ and HumanName.__hash__. Instances revert to Python's default identity semantics (still safely usable in sets/dicts — see below). Value comparison moves to two explicit methods, shipped in 1.3.0 so...
Open Graph Description: Summary 2.0 will remove HumanName.__eq__ and HumanName.__hash__. Instances revert to Python's default identity semantics (still safely usable in sets/dicts — see below). Value comparison moves to t...
X Description: Summary 2.0 will remove HumanName.__eq__ and HumanName.__hash__. Instances revert to Python's default identity semantics (still safely usable in sets/dicts — see below). Value comparison moves ...
Opengraph URL: https://github.com/derek73/python-nameparser/issues/223
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Remove __eq__ and __hash__ in 2.0; compare names via explicit methods","articleBody":"## Summary\n\n2.0 will remove `HumanName.__eq__` and `HumanName.__hash__`. Instances revert to Python's default identity semantics (still safely usable in sets/dicts — see below). Value comparison moves to two explicit methods, shipped in 1.3.0 so the APIs overlap for a full minor release: `matches()` and `comparison_key()` (added in PR #247).\n\nFeedback welcome — this is a breaking change and the rationale is laid out in full below. The deprecation warnings shipped in 1.3.0 (#224, implemented in PR #247; released).\n\n## Why the current design can't be fixed in place\n\n`__eq__` currently compares `str(self).lower()` against `str(other).lower()`, and `__hash__` (added in 1.2.0, #138) hashes that string. This makes three promises:\n\n1. **Case-insensitive** equality\n2. **Cross-type** equality with plain strings (`hn == \"John Smith\"` → True)\n3. **Hashability** (usable in sets/dicts)\n\nThese are mathematically incompatible. If `hn == \"John Smith\"` and `hn == \"john smith\"` are both True, the hash invariant (`a == b` ⇒ `hash(a) == hash(b)`) requires `hash(hn)` to equal two different values. Consequence today: `{hn} | {\"John Smith\"}` is a two-element set whose elements compare equal — broken dedup, the very use case #138 added hashing for. No implementation can fix this; one of the three promises has to go.\n\n## Additional defects in the current design\n\n- **Mutation strands container entries.** `HumanName` is mutable; assigning any attribute changes the hash, so an instance already in a set/dict becomes unfindable. (Python nulls `__hash__` when a class defines `__eq__` for exactly this reason — #138 re-enabled what the language had disabled.)\n- **Equality depends on display config.** `str(self)` goes through `string_format`, so changing `CONSTANTS.string_format` retroactively changes whether two existing objects are equal. Same class of shared-mutable-state problem as #222.\n- **`maiden` is invisible to equality** — the default `string_format` has no `{maiden}` placeholder.\n- **`hn == 42` can be True** (if the name is \"42\"); `__eq__` stringifies any operand instead of returning `NotImplemented`.\n\n## Why removal rather than component-based `__eq__`/`__hash__`\n\nA component-based `==` was considered and is workable, but it still privileges one equality semantic for a domain that has none (is \"J. Smith\" equal to \"John Smith\"? \"Smith, John\"? every application answers differently), and still carries the mutate-while-hashed caveat. Removing both dunders is the only option with no footguns left: default identity hash is always stable and always consistent with identity equality, so sets/dicts of HumanName keep working — they just mean \"these objects\" rather than \"these name values\". Value semantics become explicit:\n\n- `hn.matches(other)` — parses a string operand, compares parsed components\n- `hn.comparison_key()` — hashable tuple of components, for dedup/dicts/groupby\n\n## Migration (from 1.3.0's deprecated `==`)\n\n| Before | After |\n|---|---|\n| `hn == x` | `hn.matches(x)` |\n| `\"john smith\" == hn` (reflected) | `hn.matches(\"john smith\")` — must flip operands |\n| `hn == maybe_none` | `maybe_none is not None and hn.matches(maybe_none)` — `matches()` raises `TypeError` on non-`str`/`HumanName` |\n| `hn in names`, `.index()`, `assertIn` | `any(hn.matches(n) for n in names)` or `comparison_key()` sets |\n| set/dict dedup via `hash(hn)` | `{n.comparison_key(): n for n in names}.values()` |\n\nUsers with a lossy custom `string_format` (e.g. `\"{first} {last}\"`) currently get projection equality; `matches()` compares all components and is stricter. Compare the specific attributes instead.\n\n## Checklist\n\n- [ ] Remove `__eq__` and `__hash__` from `HumanName`\n- [ ] Replace equality/hash tests with identity-semantics tests\n- [ ] Update the `docs/usage.rst` deprecation note (the equality example already uses `matches()` as of 1.3.0)\n- [ ] Migration section in docs + release log entry\n\n\n","author":{"url":"https://github.com/derek73","@type":"Person","name":"derek73"},"datePublished":"2026-07-05T20:56:11.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/223/python-nameparser/issues/223"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:b4e2a323-a3d6-0545-0a88-decf15b803c4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BAD6:324367:2375810:339763D:6A566116 |
| html-safe-nonce | 4375248aa10a7b2bbb07e77ca6b67cab1655a92e64dea6d45e040fb8a842ab10 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQUQ2OjMyNDM2NzoyMzc1ODEwOjMzOTc2M0Q6NkE1NjYxMTYiLCJ2aXNpdG9yX2lkIjoiNjc1MDQ2NjA1ODcyMDAxMDUxOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 8b5c350b0e1049fbd9905a953f576130aec20f7b62ba985556339cd949bf48cf |
| hovercard-subject-tag | issue:4814410809 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/derek73/python-nameparser/223/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6b16af349dfcd60c98a743a5deee658ff3f89c446e70593dd3ab37345ffa67ca/derek73/python-nameparser/issues/223 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6b16af349dfcd60c98a743a5deee658ff3f89c446e70593dd3ab37345ffa67ca/derek73/python-nameparser/issues/223 |
| og:image:alt | Summary 2.0 will remove HumanName.__eq__ and HumanName.__hash__. Instances revert to Python's default identity semantics (still safely usable in sets/dicts — see below). Value comparison moves to t... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | derek73 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 6a3afe34dff341ad10912db44becf282289d7f2af20576241738530fbb28de01 |
| turbo-cache-control | no-preview |
| go-import | github.com/derek73/python-nameparser git https://github.com/derek73/python-nameparser.git |
| octolytics-dimension-user_id | 18306 |
| octolytics-dimension-user_login | derek73 |
| octolytics-dimension-repository_id | 18353524 |
| octolytics-dimension-repository_nwo | derek73/python-nameparser |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 18353524 |
| octolytics-dimension-repository_network_root_nwo | derek73/python-nameparser |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | b71002134df95575e9f6c2945a64d454b0be181e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width