1616Copyright (C) 2011 ProFUSION embedded systems
1717"""
1818
19- import re
2019import os
20+ import re
2121from typing import (
22- Dict ,
23- Sequence ,
2422 Container ,
25- Optional ,
23+ Dict ,
24+ FrozenSet ,
25+ Generic ,
2626 Iterable ,
27+ Optional ,
2728 Protocol ,
28- Generic ,
29+ Sequence ,
2930 TypeVar ,
3031)
3132
108109
109110_builtin_default_as_tuple = tuple (_builtin_default .split ("," ))
110111
112+ _inline_ignore_regex = re .compile (r"[^\w\s]\s?codespell:ignore\b(\s+(?P<words>[\w,]*))?" )
113+
111114
112115class UnknownBuiltinDictionaryError (ValueError ):
113116 def __init__ (self , name : str ) -> None :
@@ -206,12 +209,21 @@ def __init__(
206209 if builtin_dictionaries :
207210 self .load_builtin_dictionaries (builtin_dictionaries )
208211
212+ def _parse_inline_ignore (self , line : str ) -> Optional [FrozenSet [str ]]:
213+ inline_ignore_match = _inline_ignore_regex .search (line )
214+ if inline_ignore_match :
215+ words = frozenset (
216+ filter (None , (inline_ignore_match .group ("words" ) or "" ).split ("," ))
217+ )
218+ return words if words else None
219+ return frozenset ()
220+
209221 def spellcheck_line (
210222 self ,
211223 line : str ,
212224 tokenizer : LineTokenizer [T_co ],
213225 * ,
214- extra_words_to_ignore : Container [ str ] = frozenset ()
226+ respect_inline_ignore : bool = True ,
215227 ) -> Iterable [DetectedMisspelling [T_co ]]:
216228 """Tokenize and spellcheck a line
217229
@@ -220,12 +232,19 @@ def spellcheck_line(
220232
221233 :param line: The line to spellcheck.
222234 :param tokenizer: A callable that will tokenize the line
223- :param extra_words_to_ignore: Extra words to ignore for this particular line
224- (such as content from a `codespell:ignore` comment)
235+ :param respect_inline_ignore: Whether to check the line for
236+ `codespell:ignore` instructions
237+ :returns: An iterable of discovered typos.
225238 """
226239 misspellings = self ._misspellings
227240 ignore_words_cased = self .ignore_words_cased
228241
242+ extra_words_to_ignore = (
243+ self ._parse_inline_ignore (line ) if respect_inline_ignore else frozenset ()
244+ )
245+ if extra_words_to_ignore is None :
246+ return
247+
229248 for token in tokenizer (line ):
230249 word = token .group ()
231250 if word in ignore_words_cased :
0 commit comments