From d9dc89cfe78881f9ad210a17d0d4292d1d94037b Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Sat, 21 Aug 2021 19:00:50 +0200 Subject: [PATCH] Fix a crash from astroid.InferenceError raised on copy.copy Closes #4891 --- ChangeLog | 6 ++++++ pylint/checkers/stdlib.py | 10 +++++++--- tests/functional/r/regression/regression_4891.py | 16 ++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 tests/functional/r/regression/regression_4891.py diff --git a/ChangeLog b/ChangeLog index 49fc5d968f..832f4b6a72 100644 --- a/ChangeLog +++ b/ChangeLog @@ -22,6 +22,12 @@ Release date: TBA Closes #4886 +* Fix a crash in the checker raising ``shallow-copy-environ`` when failing to infer + on ``copy.copy`` + + Closes #4896 + + What's New in Pylint 2.10.1? ============================ diff --git a/pylint/checkers/stdlib.py b/pylint/checkers/stdlib.py index cffc8a4d55..b7a6e031b7 100644 --- a/pylint/checkers/stdlib.py +++ b/pylint/checkers/stdlib.py @@ -483,9 +483,13 @@ def _check_for_check_kw_in_run(self, node): if "check" not in kwargs: self.add_message("subprocess-run-check", node=node) - def _check_shallow_copy_environ(self, node): + def _check_shallow_copy_environ(self, node: nodes.Call) -> None: arg = utils.get_argument_from_call(node, position=0) - for inferred in arg.inferred(): + try: + inferred_args = arg.inferred() + except astroid.InferenceError: + return + for inferred in inferred_args: if inferred.qname() == OS_ENVIRON: self.add_message("shallow-copy-environ", node=node) break @@ -505,7 +509,7 @@ def _check_shallow_copy_environ(self, node): "unspecified-encoding", "forgotten-debug-statement", ) - def visit_call(self, node): + def visit_call(self, node: nodes.Call) -> None: """Visit a Call node.""" self.check_deprecated_class_in_call(node) for inferred in utils.infer_all(node.func): diff --git a/tests/functional/r/regression/regression_4891.py b/tests/functional/r/regression/regression_4891.py new file mode 100644 index 0000000000..34945e8126 --- /dev/null +++ b/tests/functional/r/regression/regression_4891.py @@ -0,0 +1,16 @@ +# pylint: disable=missing-module-docstring +# pylint: disable=too-few-public-methods +import copy + +class MyData: + ''' + class docstring + ''' + def __init__(self): + self.data = {} + + def process(self): + ''' + another method is responsible for putting "static_key" + ''' + copy.copy(self.data['static_key'])