Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[spelling checker] Use a context manager to access the private dict #6493

Merged
merged 1 commit into from
May 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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