From 7774791e54dc3d0837ff34d32635f7d38980ba45 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Thu, 7 Aug 2025 11:41:40 +0200 Subject: [PATCH] Fix false-negative for used-before-assignment with postponed evaluation in function defs (#10482) (cherry picked from commit d363fca110725ac58a009a01a2c505b867bed1d3) --- doc/whatsnew/fragments/10482.false_negative | 3 +++ pylint/checkers/variables.py | 12 +++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 doc/whatsnew/fragments/10482.false_negative diff --git a/doc/whatsnew/fragments/10482.false_negative b/doc/whatsnew/fragments/10482.false_negative new file mode 100644 index 0000000000..f689436b8b --- /dev/null +++ b/doc/whatsnew/fragments/10482.false_negative @@ -0,0 +1,3 @@ +Fix false-negative for ``used-before-assignment`` with ``from __future__ import annotations`` in function definitions. + +Refs #10482 diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 5f5259e79a..f54153341c 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -1949,7 +1949,17 @@ def _check_consumer( # and unevaluated annotations inside a function body if not ( self._postponed_evaluation_enabled - and isinstance(stmt, (nodes.AnnAssign, nodes.FunctionDef)) + and ( + isinstance(stmt, nodes.AnnAssign) + or ( + isinstance(stmt, nodes.FunctionDef) + and node + not in { + *(stmt.args.defaults or ()), + *(stmt.args.kw_defaults or ()), + } + ) + ) ) and not ( isinstance(stmt, nodes.AnnAssign) and utils.get_node_first_ancestor_of_type(stmt, nodes.FunctionDef)