Skip to content

Commit

Permalink
Fix ruff alerts (currently) not caught by pre-commit (#3158)
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitriPapadopoulos authored Oct 17, 2023
1 parent d4a9bf3 commit 41e78bf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
21 changes: 12 additions & 9 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
(
"rare",
"for rare (but valid) words that are likely to be errors",
"_rare", # noqa: E501
"_rare",
None,
None,
None,
Expand Down Expand Up @@ -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",),
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 "
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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:
Expand Down
16 changes: 9 additions & 7 deletions codespell_lib/tests/test_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand All @@ -53,7 +54,7 @@
]
fname_params = pytest.mark.parametrize(
"fname, in_aspell, in_dictionary", _fnames_in_aspell
) # noqa: E501
)


def test_dictionaries_exist() -> None:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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"),
Expand Down

0 comments on commit 41e78bf

Please sign in to comment.