diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index f2c6c44cb6..baf77997af 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -228,7 +228,6 @@ def open_with_chardet(self, filename: str) -> Tuple[List[str], str]: break self.encdetector.close() encoding = self.encdetector.result["encoding"] - assert encoding is not None # noqa: S101 try: f = open(filename, encoding=encoding, newline="") @@ -245,7 +244,7 @@ def open_with_chardet(self, filename: str) -> Tuple[List[str], str]: lines = f.readlines() f.close() - return lines, encoding + return lines, f.encoding def open_with_internal(self, filename: str) -> Tuple[List[str], str]: encoding = None diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py index 7a36369bf9..d7bd72de9e 100644 --- a/codespell_lib/tests/test_basic.py +++ b/codespell_lib/tests/test_basic.py @@ -384,6 +384,16 @@ def test_encoding( assert "WARNING: Binary file" in stderr +def test_unknown_encoding_chardet( + tmp_path: Path, + capsys: pytest.CaptureFixture[str], +) -> None: + """Test opening a file with unknown encoding using chardet""" + fname = tmp_path / "tmp" + fname.touch() + assert cs.main("--hard-encoding-detection", fname) == 0 + + def test_ignore( tmp_path: Path, capsys: pytest.CaptureFixture[str],