diff --git a/doc/whatsnew/fragments/5327.false_positive b/doc/whatsnew/fragments/5327.false_positive new file mode 100644 index 0000000000..6751a4619d --- /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 diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 9efb9e1be6..5973b7db01 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -30,6 +30,7 @@ ) from pylint.constants import ( PY39_PLUS, + PY310_PLUS, TYPING_NEVER, TYPING_NORETURN, TYPING_TYPE_CHECKS_GUARDS, @@ -1694,6 +1695,10 @@ def _check_consumer( if not ( self._postponed_evaluation_enabled and isinstance(stmt, (nodes.AnnAssign, nodes.FunctionDef)) + ) and not ( + 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"