Compat with chardet 7 (#3886)
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 891c15f..16bc8ba 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml
@@ -32,6 +32,9 @@ - "ubuntu-24.04-arm" include: - python-version: "3.10" + kind: "old" + os: "ubuntu-24.04-arm" + - python-version: "3.10" kind: "no-toml" os: "ubuntu-24.04-arm" - python-version: "3.13" @@ -58,6 +61,8 @@ pip install -e ".[dev]" # install the codespell dev packages - run: pip install aspell-python-py3 if: startsWith(matrix.os, 'ubuntu') + - run: pip install "chardet<7" + if: ${{ matrix.kind == 'old' }} - run: codespell --help - run: codespell --version - run: make check
diff --git a/.gitignore b/.gitignore index f4d749b..cb3acd2 100644 --- a/.gitignore +++ b/.gitignore
@@ -14,3 +14,4 @@ codespell_lib/_version.py junit-results.xml *.egg-info/ +.DS_Store
diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index 82c2730..c5b5b4e 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py
@@ -223,14 +223,18 @@ def init_chardet(self) -> None: try: - from chardet.universaldetector import UniversalDetector - except ImportError as e: + import chardet # noqa: F401 + except ImportError: msg = ( "There's no chardet installed to import from. " "Please, install it and check your PYTHONPATH " "environment variable" ) - raise ImportError(msg) from e + raise ImportError(msg) + try: + from chardet import UniversalDetector + except ImportError: # chardet < 7 + from chardet.universaldetector import UniversalDetector self.encdetector = UniversalDetector()