From aaf9192673614315781dca24a862b719354c0b54 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Wed, 6 Aug 2025 23:41:00 +0200 Subject: [PATCH] Fix false-negative for used-before-assignment with postponed evaluation in function defs --- 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 5ada60a2bc..e7cb769c23 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -1929,7 +1929,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)