Skip to content

Commit

Permalink
[spelling chacker] Better help message and code organization
Browse files Browse the repository at this point in the history
Following a failed attempt to add functional tests for spelling checker in pylint-dev#6137
  • Loading branch information
Pierre-Sassoulas committed Feb 24, 2023
1 parent 8bf1120 commit 0e0606c
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions pylint/checkers/spelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import sys
import tokenize
from re import Pattern
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

from astroid import nodes

Expand All @@ -35,8 +35,11 @@
WikiWordFilter,
get_tokenizer,
)

PYENCHANT_AVAILABLE = True
except ImportError:
enchant = None
PYENCHANT_AVAILABLE = False

class EmailFilter: # type: ignore[no-redef]
...
Expand All @@ -62,17 +65,26 @@ def get_tokenizer(
return Filter()


if enchant is not None:
br = enchant.Broker()
dicts = br.list_dicts()
dict_choices = [""] + [d[0] for d in dicts]
dicts = [f"{d[0]} ({d[1].name})" for d in dicts]
dicts = ", ".join(dicts)
instr = ""
else:
dicts = "none"
dict_choices = [""]
instr = " To make it work, install the 'python-enchant' package."
def _get_enchant_dict() -> 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_help() -> str:
enchant_dicts = _get_enchant_dict()
if enchant_dicts:
dict_as_str = [f"{d[0]} ({d[1].name})" for d in 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:
enchant_help += "both the python package and "
enchant_help += "the system dependency for enchant to work."
return f"Spelling dictionary name. {enchant_help}."


class WordsWithDigitsFilter(Filter): # type: ignore[misc]
Expand Down Expand Up @@ -237,9 +249,8 @@ class SpellingChecker(BaseTokenChecker):
"default": "",
"type": "choice",
"metavar": "<dict name>",
"choices": dict_choices,
"help": "Spelling dictionary name. "
f"Available dictionaries: {dicts}.{instr}",
"choices": _get_enchant_dict_choices(),
"help": _get_enchant_dict_help(),
},
),
(
Expand Down Expand Up @@ -297,7 +308,7 @@ class SpellingChecker(BaseTokenChecker):

def open(self) -> None:
self.initialized = False
if enchant is None:
if not PYENCHANT_AVAILABLE:
return
dict_name = self.linter.config.spelling_dict
if not dict_name:
Expand Down

0 comments on commit 0e0606c

Please sign in to comment.