Skip to content

Commit cafdc37

Browse files
Add functional test for spelling checker
1 parent 1188340 commit cafdc37

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

pylint/checkers/spelling.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@
3535
WikiWordFilter,
3636
get_tokenizer,
3737
)
38+
39+
PYENCHANT_AVAILABLE = True
3840
except ImportError:
3941
enchant = None
42+
PYENCHANT_AVAILABLE = False
4043

4144
class EmailFilter: # type: ignore[no-redef]
4245
...
@@ -62,17 +65,16 @@ def get_tokenizer(
6265
return Filter()
6366

6467

65-
if enchant is not None:
66-
br = enchant.Broker()
67-
dicts = br.list_dicts()
68-
dict_choices = [""] + [d[0] for d in dicts]
69-
dicts = [f"{d[0]} ({d[1].name})" for d in dicts]
70-
dicts = ", ".join(dicts)
71-
instr = ""
68+
INSTALL_ENCHANT = " To make it work, install the 'python-enchant' package."
69+
if PYENCHANT_AVAILABLE:
70+
broker = enchant.Broker()
71+
enchant_dicts = broker.list_dicts()
72+
enchant_dict_choices = [""] + [d[0] for d in enchant_dicts]
73+
enchant_dicts = [f"{d[0]} ({d[1].name})" for d in enchant_dicts]
74+
enchant_dicts = ", ".join(enchant_dicts)
7275
else:
73-
dicts = "none"
74-
dict_choices = [""]
75-
instr = " To make it work, install the 'python-enchant' package."
76+
enchant_dicts = "none"
77+
enchant_dict_choices = [""]
7678

7779

7880
class WordsWithDigitsFilter(Filter):
@@ -235,9 +237,9 @@ class SpellingChecker(BaseTokenChecker):
235237
"default": "",
236238
"type": "choice",
237239
"metavar": "<dict name>",
238-
"choices": dict_choices,
240+
"choices": enchant_dict_choices,
239241
"help": "Spelling dictionary name. "
240-
f"Available dictionaries: {dicts}.{instr}",
242+
f"Available dictionaries: {enchant_dicts}.{INSTALL_ENCHANT}",
241243
},
242244
),
243245
(
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""Functional test for speling.""" # [wrong-spelling-in-docstring, wrong-spelling-in-comment]
2+
3+
# mispeled text # [wrong-spelling-in-comment]
4+
5+
def function_name():
6+
"""mispeled text.""" # [wrong-spelling-in-docstring, wrong-spelling-in-comment]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[SPELLING]
2+
spelling-dict=en_US
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
wrong-spelling-in-comment:1:0:None:None::"Wrong spelling of a word 'docstring' in a comment:
2+
# [wrong-spelling-in-docstring, wrong-spelling-in-comment]
3+
^^^^^^^^^
4+
Did you mean: ''doc string' or 'doc-string' or 'stringing''?":UNDEFINED
5+
wrong-spelling-in-docstring:1:0:None:None::"Wrong spelling of a word 'speling' in a docstring:
6+
Functional test for speling.
7+
^^^^^^^
8+
Did you mean: ''spieling' or 'spelling' or 'spewing' or 'peeling''?":UNDEFINED
9+
wrong-spelling-in-comment:3:0:None:None::"Wrong spelling of a word 'mispeled' in a comment:
10+
# mispeled text # [wrong-spelling-in-comment]
11+
^^^^^^^^
12+
Did you mean: ''misspelled' or 'dispelled' or 'misspell' or 'misled''?":UNDEFINED
13+
wrong-spelling-in-comment:6:0:None:None::"Wrong spelling of a word 'docstring' in a comment:
14+
# [wrong-spelling-in-docstring, wrong-spelling-in-comment]
15+
^^^^^^^^^
16+
Did you mean: ''doc string' or 'doc-string' or 'stringing''?":UNDEFINED
17+
wrong-spelling-in-docstring:6:0:None:None::"Wrong spelling of a word 'mispeled' in a docstring:
18+
mispeled text.
19+
^^^^^^^^
20+
Did you mean: ''misspelled' or 'dispelled' or 'misspell' or 'misled''?":UNDEFINED

0 commit comments

Comments
 (0)