From 689d952ee456d4487ff16e3b57278c9bdd73cce9 Mon Sep 17 00:00:00 2001 From: Nicolas Thumann Date: Mon, 13 Mar 2023 12:54:43 +0100 Subject: [PATCH 1/2] Fix AssertionError when opening with chardet --- codespell_lib/_codespell.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 From 91903d864d248638416607128809d99be67bef63 Mon Sep 17 00:00:00 2001 From: Nicolas Thumann Date: Tue, 14 Mar 2023 09:09:06 +0100 Subject: [PATCH 2/2] Add test for unknown encoding using chardet --- codespell_lib/tests/test_basic.py | 10 ++++++++++ 1 file changed, 10 insertions(+) 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],