Replace chardet with charset-normalizer as preferred encoding detection#590
Open
imsurat wants to merge 1 commit intoBachmann1234:mainfrom
Open
Replace chardet with charset-normalizer as preferred encoding detection#590imsurat wants to merge 1 commit intoBachmann1234:mainfrom
imsurat wants to merge 1 commit intoBachmann1234:mainfrom
Conversation
charset-normalizer is now the default encoding detection library. chardet is supported as a fallback if charset-normalizer is not installed. If neither is available, falls back to utf-8 with a helpful warning. Motivation: - chardet 7.0 relicensing dispute (chardet/chardet#327) creates legal uncertainty for downstream users - charset-normalizer has the same API, is MIT-licensed, actively maintained, and already preferred by requests - Makes chardet fully optional — available as an extra for users who prefer it Changes: - snippets.py: try charset_normalizer first, fall back to chardet, then utf-8 with warning if neither installed - pyproject.toml: charset-normalizer as main dep, chardet as optional extra Closes Bachmann1234#313
06dea13 to
22d6bd2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
charset-normalizeris now the preferred encoding detection library (same.detect()API as chardet)chardetis supported as a fallback if charset-normalizer is not installedchardetavailable as an optional extra (pip install diff-cover[chardet])Motivation
charset-normalizeris MIT-licensed, actively maintained, and already the preferred library forrequestsChanges
diff_cover/snippets.py: Trycharset_normalizer.detect()first, fall back tochardet.detect(), then utf-8 with warningpyproject.toml:charset-normalizer >= 2.0.0as main dependency,chardetas optional extrapoetry.lock: Updated to reflect dependency changeKnown behavior difference
test_latin_one_undeclaredwill fail when running withcharset-normalizerinstead ofchardet. This is becausecharset-normalizerdetects the short latin-1 test string as CP932 (Japanese) rather than latin-1 — a known limitation with very short ambiguous text samples.chardethappens to get this one right.This is exactly the edge case mentioned in #313 where a
--src-encodingCLI option would provide a better solution than guessing. The test could be made conditional on which library is installed, or the CLI encoding option could be added as a follow-up.For the vast majority of real source files (which are much longer than the 32-byte test fixture), both libraries produce identical results.
Addresses
Closes #313
Test plan
charset_normalizer.detect()returns same shape aschardet.detect()