From 97ad40eebb55f885e087fd79c0dece1e4718f03a Mon Sep 17 00:00:00 2001 From: Alessandro Chitarrini Date: Tue, 29 Oct 2024 14:33:33 +0100 Subject: [PATCH 1/2] Fix visit_assign and visit_subscript to handle dictionary assignments --- pylint/checkers/variables.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 150dcba838..7073bddc74 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -2072,22 +2072,37 @@ def visit_assign(self, node: nodes.Assign) -> None: non-sequences as well as in case self/cls get assigned. """ self._check_self_cls_assign(node) + + if isinstance(node.targets[0], nodes.Subscript): + try: + if isinstance(node.value, nodes.Dict): + return + else: + return + except Exception as e: + print(f"Error in assignment: {e}") + return + if not isinstance(node.targets[0], (nodes.Tuple, nodes.List)): return targets = node.targets[0].itered() - # Check if we have starred nodes. if any(isinstance(target, nodes.Starred) for target in targets): return try: inferred = utils.safe_infer(node.value) + + if isinstance(node.value, nodes.Dict): + return + if inferred is not None: self._check_unpacking(inferred, node, targets) except astroid.InferenceError: return + # listcomp have now also their scope def visit_listcomp(self, node: nodes.ListComp) -> None: """Visit listcomp: update consumption analysis variable.""" @@ -3326,8 +3341,13 @@ def _check_classdef_metaclasses( def visit_subscript(self, node: nodes.Subscript) -> None: inferred_slice = utils.safe_infer(node.slice) + inferred_value = utils.safe_infer(node.value) + if isinstance(inferred_value, nodes.Dict): + return + self._check_potential_index_error(node, inferred_slice) + def _check_potential_index_error( self, node: nodes.Subscript, From 174882d9120b7ac30a81248389b94e895f777254 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 29 Oct 2024 13:38:14 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pylint/checkers/variables.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 7073bddc74..8264429e44 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -2102,7 +2102,6 @@ def visit_assign(self, node: nodes.Assign) -> None: except astroid.InferenceError: return - # listcomp have now also their scope def visit_listcomp(self, node: nodes.ListComp) -> None: """Visit listcomp: update consumption analysis variable.""" @@ -3347,7 +3346,6 @@ def visit_subscript(self, node: nodes.Subscript) -> None: self._check_potential_index_error(node, inferred_slice) - def _check_potential_index_error( self, node: nodes.Subscript,