diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index 017a383d20..f116e5e45d 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -60,7 +60,7 @@ ( "rare", "for rare (but valid) words that are likely to be errors", - "_rare", # noqa: E501 + "_rare", None, None, None, @@ -105,7 +105,7 @@ ( "en-GB_to_en-US", "for corrections from en-GB to en-US", - "_en-GB_to_en-US", # noqa: E501 + "_en-GB_to_en-US", True, True, ("en_GB",), @@ -204,12 +204,13 @@ def __init__(self, use_chardet: bool, quiet_level: int) -> None: def init_chardet(self) -> None: try: from chardet.universaldetector import UniversalDetector - except ImportError: - raise ImportError( + except ImportError as e: + msg = ( "There's no chardet installed to import from. " "Please, install it and check your PYTHONPATH " "environment variable" ) + raise ImportError(msg) from e self.encdetector = UniversalDetector() @@ -266,7 +267,8 @@ def open_with_internal(self, filename: str) -> Tuple[List[str], str]: else: break else: - raise Exception("Unknown encoding") + msg = "Unknown encoding" + raise Exception(msg) return lines, encoding @@ -477,7 +479,7 @@ def parse_options( "- 1: disable warnings about wrong encoding.\n" "- 2: disable warnings about binary files.\n" "- 4: omit warnings about automatic fixes that were disabled in the dictionary.\n" # noqa: E501 - "- 8: don't print anything for non-automatic fixes.\n" # noqa: E501 + "- 8: don't print anything for non-automatic fixes.\n" "- 16: don't print the list of fixed files.\n" "- 32: don't print configuration files.\n" "As usual with bitmasks, these levels can be " @@ -510,7 +512,7 @@ def parse_options( "--check-hidden", action="store_true", default=False, - help="check hidden files and directories (those " 'starting with ".") as well.', + help='check hidden files and directories (those starting with ".") as well.', ) parser.add_argument( "-A", @@ -562,10 +564,11 @@ def parse_options( import tomli as tomllib # type: ignore[no-redef] except ImportError as e: if tomllib_raise_error: - raise ImportError( + msg = ( f"tomllib or tomli are required to read pyproject.toml " f"but could not be imported, got: {e}" - ) from None + ) + raise ImportError(msg) from None tomllib = None # type: ignore[assignment] if tomllib is not None: for toml_file in toml_files: diff --git a/codespell_lib/tests/test_dictionary.py b/codespell_lib/tests/test_dictionary.py index 488dff5254..f19669aa52 100644 --- a/codespell_lib/tests/test_dictionary.py +++ b/codespell_lib/tests/test_dictionary.py @@ -30,15 +30,16 @@ ) else: spellers[lang] = aspell.Speller(("lang", lang), ("size", "80")) -except ImportError as exp: +except ImportError as e: if os.getenv("REQUIRE_ASPELL", "false").lower() == "true": - raise RuntimeError( + msg = ( "Cannot run complete tests without aspell when " - f"REQUIRE_ASPELL=true. Got error during import:\n{exp}" + f"REQUIRE_ASPELL=true. Got error during import:\n{e}" ) + raise RuntimeError(msg) from e warnings.warn( "aspell not found, but not required, skipping aspell tests. Got " - f"error during import:\n{exp}", + f"error during import:\n{e}", stacklevel=2, ) @@ -53,7 +54,7 @@ ] fname_params = pytest.mark.parametrize( "fname, in_aspell, in_dictionary", _fnames_in_aspell -) # noqa: E501 +) def test_dictionaries_exist() -> None: @@ -81,7 +82,8 @@ def test_dictionary_formatting( except AssertionError as exp: errors.append(str(exp).split("\n", maxsplit=1)[0]) if errors: - raise AssertionError("\n" + "\n".join(errors)) + msg = "\n" + "\n".join(errors) + raise AssertionError(msg) @pytest.mark.parametrize( @@ -258,7 +260,7 @@ def test_error_checking(err: str, rep: str, match: str) -> None: None, False, "should not be in aspell", - ), # noqa: E501 + ), # One multi-word, second part ("a", "bar abcdef", None, True, "should be in aspell"), ("a", "abcdef back", None, False, "should not be in aspell"),