Skip to content

Commit

Permalink
[spelling checker] Use a context manager for access to private dict (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas committed May 3, 2022
1 parent 6aa336f commit cadfdb2
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions pylint/checkers/spelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,6 @@ class SpellingChecker(BaseTokenChecker):

def open(self) -> None:
self.initialized = False
self.private_dict_file = None

if enchant is None:
return
dict_name = self.linter.config.spelling_dict
Expand All @@ -292,9 +290,6 @@ def open(self) -> None:
self.spelling_dict = enchant.DictWithPWL(
dict_name, self.linter.config.spelling_private_dict_file
)
self.private_dict_file = open( # pylint: disable=consider-using-with
self.linter.config.spelling_private_dict_file, "a", encoding="utf-8"
)
else:
self.spelling_dict = enchant.Dict(dict_name)

Expand All @@ -316,10 +311,6 @@ def open(self) -> None:
)
self.initialized = True

def close(self) -> None:
if self.private_dict_file:
self.private_dict_file.close()

def _check_spelling(self, msgid: str, line: str, line_num: int) -> None:
original_line = line
try:
Expand Down Expand Up @@ -374,11 +365,13 @@ def _check_spelling(self, msgid: str, line: str, line_num: int) -> None:

# Store word to private dict or raise a message.
if self.linter.config.spelling_store_unknown_words:
if (
lower_cased_word not in self.unknown_words
and self.private_dict_file is not None
):
self.private_dict_file.write(f"{lower_cased_word}\n")
if lower_cased_word not in self.unknown_words:
with open(
self.linter.config.spelling_private_dict_file,
"a",
encoding="utf-8",
) as f:
f.write(f"{lower_cased_word}\n")
self.unknown_words.add(lower_cased_word)
else:
# Present up to N suggestions.
Expand Down

0 comments on commit cadfdb2

Please sign in to comment.