Visitar URL original
fix: make CharsetMatch.__eq__ total for non-alias strings by chuenchen309 · Pull Request #773 · jawah/charset_normalizer · GitHub
Skip to content

fix: make CharsetMatch.__eq__ total for non-alias strings - #773

Merged
Ousret merged 1 commit into
jawah:masterfrom
chuenchen309:fix/charsetmatch-eq-total
Jul 19, 2026
Merged

Ousret merged 1 commit into
jawah:masterfrom
chuenchen309:fix/charsetmatch-eq-total

Conversation

@chuenchen309

@chuenchen309 chuenchen309 commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

CharsetMatch.__eq__ compares a match to a string via iana_name(other) with the default strict=True, so any operand that isn't a known encoding alias raises ValueError instead of returning False:

m = from_bytes(b"Hello world plain ascii text here.").best()
m == "ascii"                # True
m == "utf-8"                # False
m == "not-a-real-encoding"  # -> ValueError: Unable to retrieve IANA for 'not_a_real_encoding'
"hello" in [m]              # -> ValueError (membership uses __eq__)

That breaks match == label / label in matches for arbitrary strings and violates the data model, which requires __eq__ to return a value for any operand (never raise). The fix uses non-strict iana_name, mirroring the tolerant lookup already in CharsetMatches.__getitem__ (which does iana_name(item, False)). Valid aliases compare exactly as before; unknown strings now compare unequal.

Added a regression test; full suite stays green (164 passed). Happy to add a CHANGELOG entry if you curate those manually.

Found, fixed, and tested by an AI coding agent (Claude Code) running autonomously on this account; the human account holder reviews every change and is accountable for it. The repro and test are real and re-runnable from it — just say the word if this isn't a contribution you want.

Comparing a CharsetMatch to a string used `iana_name(other)` with the default
`strict=True`, so any operand that is not a known encoding alias raised
`ValueError` instead of comparing unequal. That breaks `match == label` and
`label in matches` for arbitrary strings and violates the data model, which
requires `__eq__` to return a value for any operand. Use non-strict
`iana_name`, matching the tolerant lookup already used in
`CharsetMatches.__getitem__`.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@chuenchen309
chuenchen309 requested a review from Ousret as a code owner July 17, 2026 16:35

@Ousret Ousret left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Indeed it should not raise ideally.
Using AI is alright as long as disclosed.

regards,

@Ousret
Ousret merged commit 667b93c into jawah:master Jul 19, 2026
42 checks passed
@Ousret Ousret mentioned this pull request Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants