diff options
author | Daniël van Noord <13665637+DanielNoord@users.noreply.github.com> | 2022-05-04 10:32:24 +0200 |
---|---|---|
committer | Daniël van Noord <13665637+DanielNoord@users.noreply.github.com> | 2022-05-04 10:53:34 +0200 |
commit | 710997724e9f77556dfc1fe4bfb54b0469046d7b (patch) | |
tree | e107c4970b2bb3b044abe945fbc388b3b9dd6574 /pylint/checkers/stdlib.py | |
parent | 003336aed86fad8acf8fc3869ede9486cb75eb08 (diff) | |
download | pylint-git-710997724e9f77556dfc1fe4bfb54b0469046d7b.tar.gz |
Create a generic ``_check_open_call`` function
Diffstat (limited to 'pylint/checkers/stdlib.py')
-rw-r--r-- | pylint/checkers/stdlib.py | 55 |
1 files changed, 23 insertions, 32 deletions
diff --git a/pylint/checkers/stdlib.py b/pylint/checkers/stdlib.py index 621d55b8c..6707ddcde 100644 --- a/pylint/checkers/stdlib.py +++ b/pylint/checkers/stdlib.py @@ -20,7 +20,7 @@ if TYPE_CHECKING: from pylint.lint import PyLinter OPEN_FILES_MODE = ("open", "file") -OPEN_FILES_ENCODING = ("open", "read_text", "write_text") +OPEN_FILES_FUNCS = OPEN_FILES_MODE + ("read_text", "write_text") UNITTEST_CASE = "unittest.case" THREADING_THREAD = "threading.Thread" COPY_COPY = "copy.copy" @@ -523,18 +523,13 @@ class StdlibChecker(DeprecatedMixin, BaseChecker): if inferred is astroid.Uninferable: continue if inferred.root().name in OPEN_MODULE: - if ( - isinstance(node.func, nodes.Name) - and node.func.name in OPEN_FILES_MODE - ): - self._check_open_mode(node) - if ( - isinstance(node.func, nodes.Name) - and node.func.name in OPEN_FILES_ENCODING - or isinstance(node.func, nodes.Attribute) - and node.func.attrname in OPEN_FILES_ENCODING - ): - self._check_open_encoded(node, inferred.root().name) + open_func_name: str | None = None + if isinstance(node.func, nodes.Name): + open_func_name = node.func.name + if isinstance(node.func, nodes.Attribute): + open_func_name = node.func.attrname + if open_func_name in OPEN_FILES_FUNCS: + self._check_open_call(node, inferred.root().name, open_func_name) elif inferred.root().name == UNITTEST_CASE: self._check_redundant_assert(node, inferred) elif isinstance(inferred, nodes.ClassDef): @@ -639,25 +634,10 @@ class StdlibChecker(DeprecatedMixin, BaseChecker): ): self.add_message("boolean-datetime", node=node) - def _check_open_mode(self, node: nodes.Call): - """Check that the mode argument of an open or file call is valid.""" - try: - mode_arg = utils.get_argument_from_call(node, position=1, keyword="mode") - except utils.NoSuchArgumentError: - return - if mode_arg: - mode_arg = utils.safe_infer(mode_arg) - if isinstance(mode_arg, nodes.Const) and not _check_mode_str( - mode_arg.value - ): - self.add_message( - "bad-open-mode", - node=node, - args=mode_arg.value or str(mode_arg.value), - ) - - def _check_open_encoded(self, node: nodes.Call, open_module: str) -> None: - """Check that the encoded argument of an open call is valid.""" + def _check_open_call( + self, node: nodes.Call, open_module: str, func_name: str + ) -> None: + """Various checks for an open call.""" mode_arg = None try: if open_module == "_io": @@ -674,6 +654,17 @@ class StdlibChecker(DeprecatedMixin, BaseChecker): if mode_arg: mode_arg = utils.safe_infer(mode_arg) + if ( + func_name in OPEN_FILES_MODE + and isinstance(mode_arg, nodes.Const) + and not _check_mode_str(mode_arg.value) + ): + self.add_message( + "bad-open-mode", + node=node, + args=mode_arg.value or str(mode_arg.value), + ) + if ( not mode_arg or isinstance(mode_arg, nodes.Const) |