Skip to content

Commit

Permalink
Refactor to be able to tests and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas committed Feb 24, 2023
1 parent 0e0606c commit 4d14d60
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
27 changes: 17 additions & 10 deletions pylint/checkers/spelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,35 @@ def get_tokenizer(
return Filter()


def _get_enchant_dict() -> list[tuple[Any, enchant.ProviderDesc]]:
def _get_enchant_dicts() -> list[tuple[Any, enchant.ProviderDesc]]:
# Broker().list_dicts() is not typed in enchant, but it does return tuples
return enchant.Broker().list_dicts() if PYENCHANT_AVAILABLE else [] # type: ignore[no-any-return]


def _get_enchant_dict_choices() -> list[str]:
return [""] + [d[0] for d in _get_enchant_dict()]
def _get_enchant_dict_choices(
inner_enchant_dicts: list[tuple[Any, enchant.ProviderDesc]]
) -> list[str]:
return [""] + [d[0] for d in inner_enchant_dicts]


def _get_enchant_dict_help() -> str:
enchant_dicts = _get_enchant_dict()
if enchant_dicts:
dict_as_str = [f"{d[0]} ({d[1].name})" for d in enchant_dicts]
def _get_enchant_dict_help(
inner_enchant_dicts: list[tuple[Any, enchant.ProviderDesc]],
pyenchant_available: bool,
) -> str:
if inner_enchant_dicts:
dict_as_str = [f"{d[0]} ({d[1].name})" for d in inner_enchant_dicts]
enchant_help = f"Available dictionaries: {', '.join(dict_as_str)}"
else:
enchant_help = "No available dictionaries : You need to install "
if not PYENCHANT_AVAILABLE:
if not pyenchant_available:
enchant_help += "both the python package and "
enchant_help += "the system dependency for enchant to work."
return f"Spelling dictionary name. {enchant_help}."


enchant_dicts = _get_enchant_dicts()


class WordsWithDigitsFilter(Filter): # type: ignore[misc]
"""Skips words with digits."""

Expand Down Expand Up @@ -249,8 +256,8 @@ class SpellingChecker(BaseTokenChecker):
"default": "",
"type": "choice",
"metavar": "<dict name>",
"choices": _get_enchant_dict_choices(),
"help": _get_enchant_dict_help(),
"choices": _get_enchant_dict_choices(enchant_dicts),
"help": _get_enchant_dict_help(enchant_dicts, PYENCHANT_AVAILABLE),
},
),
(
Expand Down
12 changes: 12 additions & 0 deletions tests/checkers/unittest_spelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest

from pylint.checkers import spelling
from pylint.checkers.spelling import _get_enchant_dict_help
from pylint.testutils import CheckerTestCase, MessageTest, _tokenize_str, set_config

# try to create enchant dictionary
Expand All @@ -25,6 +26,17 @@
pass


def test_spelling_dict_help() -> None:
assert (
"both the python package and the system dependency"
in _get_enchant_dict_help([], False)
)
assert "need to install the system dependency" in _get_enchant_dict_help([], True)
assert "Available dictionaries: " in _get_enchant_dict_help(
enchant.Broker().list_dicts(), True
)


class TestSpellingChecker(CheckerTestCase): # pylint:disable=too-many-public-methods
# This is a test case class, not sure why it would be relevant to have
# this pylint rule enforced for test case classes.
Expand Down

0 comments on commit 4d14d60

Please sign in to comment.