From b38da7324ca65597af2ab080a448e2737389b113 Mon Sep 17 00:00:00 2001 From: jakubo Date: Fri, 18 Nov 2022 10:18:40 +0100 Subject: [PATCH 1/7] Fix false positive `used-before-assignment` in pattern matching with a guard #5327 --- pylint/checkers/variables.py | 2 ++ .../u/used/used_before_assignment_matching.py | 10 ++++++++++ .../u/used/used_before_assignment_matching.txt | 2 ++ 3 files changed, 14 insertions(+) create mode 100644 tests/functional/u/used/used_before_assignment_matching.py create mode 100644 tests/functional/u/used/used_before_assignment_matching.txt diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 627e82ef06..a6841effc1 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -1641,6 +1641,8 @@ def _check_consumer( if not ( self._postponed_evaluation_enabled and isinstance(stmt, (nodes.AnnAssign, nodes.FunctionDef)) + ): and not ( + isinstance(stmt, nodes.Match) and isinstance(node.parent, nodes.Compare) ): self.add_message( "used-before-assignment", diff --git a/tests/functional/u/used/used_before_assignment_matching.py b/tests/functional/u/used/used_before_assignment_matching.py new file mode 100644 index 0000000000..87ee1a817e --- /dev/null +++ b/tests/functional/u/used/used_before_assignment_matching.py @@ -0,0 +1,10 @@ +"""used-before-assignment cases in matching""" + +match 4: + case a if a in {2, 4, 6}: # [used-before-assignment] + pass + + +match ("example", "one"): + case (x, y) if x == "example": # [used-before-assignment] + pass diff --git a/tests/functional/u/used/used_before_assignment_matching.txt b/tests/functional/u/used/used_before_assignment_matching.txt new file mode 100644 index 0000000000..c6218224e7 --- /dev/null +++ b/tests/functional/u/used/used_before_assignment_matching.txt @@ -0,0 +1,2 @@ +used-before-assignment:4:14:4:15::Using variable 'a' before assignment:HIGH +used-before-assignment:9:19:9:20::Using variable 'x' before assignment:HIGH From c7e012274be49120074f9f704e72d1b312d18b91 Mon Sep 17 00:00:00 2001 From: jakubo Date: Fri, 18 Nov 2022 10:27:40 +0100 Subject: [PATCH 2/7] Typo fix --- pylint/checkers/variables.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index a6841effc1..446c0f0a80 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -1641,7 +1641,7 @@ def _check_consumer( if not ( self._postponed_evaluation_enabled and isinstance(stmt, (nodes.AnnAssign, nodes.FunctionDef)) - ): and not ( + ) and not ( isinstance(stmt, nodes.Match) and isinstance(node.parent, nodes.Compare) ): self.add_message( From b6bfabee8f0bce0b3a9c1e5b94e0f70bc42cd3dd Mon Sep 17 00:00:00 2001 From: jakubo Date: Fri, 18 Nov 2022 12:49:25 +0100 Subject: [PATCH 3/7] Added whatsnew --- doc/whatsnew/fragments/5327.false_positive | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 doc/whatsnew/fragments/5327.false_positive diff --git a/doc/whatsnew/fragments/5327.false_positive b/doc/whatsnew/fragments/5327.false_positive new file mode 100644 index 0000000000..5da9bc0401 --- /dev/null +++ b/doc/whatsnew/fragments/5327.false_positive @@ -0,0 +1,3 @@ +Don't emit ``used-before-assignment`` in pattern matching with a guard + +Closes #5327 From 2e5baefd532f49fbe432e6436069e5425e378ab9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 18 Nov 2022 11:51:17 +0000 Subject: [PATCH 4/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pylint/checkers/variables.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 446c0f0a80..99bd7395e0 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -1642,7 +1642,8 @@ def _check_consumer( self._postponed_evaluation_enabled and isinstance(stmt, (nodes.AnnAssign, nodes.FunctionDef)) ) and not ( - isinstance(stmt, nodes.Match) and isinstance(node.parent, nodes.Compare) + isinstance(stmt, nodes.Match) + and isinstance(node.parent, nodes.Compare) ): self.add_message( "used-before-assignment", From 5a0e6eebe8fb63734cc3832bc1cf094f00a7120e Mon Sep 17 00:00:00 2001 From: jakubo Date: Fri, 18 Nov 2022 12:54:06 +0100 Subject: [PATCH 5/7] Whatsnew format fix --- doc/whatsnew/fragments/5327.false_positive | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whatsnew/fragments/5327.false_positive b/doc/whatsnew/fragments/5327.false_positive index 5da9bc0401..6751a4619d 100644 --- a/doc/whatsnew/fragments/5327.false_positive +++ b/doc/whatsnew/fragments/5327.false_positive @@ -1,3 +1,3 @@ -Don't emit ``used-before-assignment`` in pattern matching with a guard +Don't emit ``used-before-assignment`` in pattern matching with a guard. Closes #5327 From 727feaa757b2244e90d0509566d729beda5c0946 Mon Sep 17 00:00:00 2001 From: jakubo Date: Fri, 18 Nov 2022 14:36:08 +0100 Subject: [PATCH 6/7] Py10 and up only --- pylint/checkers/variables.py | 5 ++++- pylint/constants.py | 1 + .../u/used/used_before_assignment_matching.py | 10 ---------- .../u/used/used_before_assignment_matching.txt | 2 -- 4 files changed, 5 insertions(+), 13 deletions(-) delete mode 100644 tests/functional/u/used/used_before_assignment_matching.py delete mode 100644 tests/functional/u/used/used_before_assignment_matching.txt diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 446c0f0a80..34322d7d19 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -29,6 +29,7 @@ is_postponed_evaluation_enabled, ) from pylint.constants import ( + PY310_PLUS, PY39_PLUS, TYPING_NEVER, TYPING_NORETURN, @@ -1642,7 +1643,9 @@ def _check_consumer( self._postponed_evaluation_enabled and isinstance(stmt, (nodes.AnnAssign, nodes.FunctionDef)) ) and not ( - isinstance(stmt, nodes.Match) and isinstance(node.parent, nodes.Compare) + PY310_PLUS + and isinstance(stmt, nodes.Match) + and isinstance(node.parent, nodes.Compare) ): self.add_message( "used-before-assignment", diff --git a/pylint/constants.py b/pylint/constants.py index d9ff20c466..aca5dec4a4 100644 --- a/pylint/constants.py +++ b/pylint/constants.py @@ -18,6 +18,7 @@ PY38_PLUS = sys.version_info[:2] >= (3, 8) PY39_PLUS = sys.version_info[:2] >= (3, 9) +PY310_PLUS = sys.version_info[:2] >= (3, 10) IS_PYPY = platform.python_implementation() == "PyPy" diff --git a/tests/functional/u/used/used_before_assignment_matching.py b/tests/functional/u/used/used_before_assignment_matching.py deleted file mode 100644 index 87ee1a817e..0000000000 --- a/tests/functional/u/used/used_before_assignment_matching.py +++ /dev/null @@ -1,10 +0,0 @@ -"""used-before-assignment cases in matching""" - -match 4: - case a if a in {2, 4, 6}: # [used-before-assignment] - pass - - -match ("example", "one"): - case (x, y) if x == "example": # [used-before-assignment] - pass diff --git a/tests/functional/u/used/used_before_assignment_matching.txt b/tests/functional/u/used/used_before_assignment_matching.txt deleted file mode 100644 index c6218224e7..0000000000 --- a/tests/functional/u/used/used_before_assignment_matching.txt +++ /dev/null @@ -1,2 +0,0 @@ -used-before-assignment:4:14:4:15::Using variable 'a' before assignment:HIGH -used-before-assignment:9:19:9:20::Using variable 'x' before assignment:HIGH From 0b3276b7cded7bd7751eec3ba8908f9914fef45e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 18 Nov 2022 13:46:58 +0000 Subject: [PATCH 7/7] [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, 1 insertion(+), 1 deletion(-) diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 34322d7d19..beab1ee0b9 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -29,8 +29,8 @@ is_postponed_evaluation_enabled, ) from pylint.constants import ( - PY310_PLUS, PY39_PLUS, + PY310_PLUS, TYPING_NEVER, TYPING_NORETURN, TYPING_TYPE_CHECKS_GUARDS,