From 759e2cc225b322db9f54f17c1943318c115ba552 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 16 Oct 2023 15:15:06 +0000 Subject: [PATCH] Fix `used-before-assignment` FP for generic type syntax (Py 3.12) (#9150) (#9154) * Fix `used-before-assignment` FP for generic type syntax (Py 3.12) * Bump astroid to 3.0.1 (cherry picked from commit cb29fbdf4e9afc9e9e8b979f52d2189fa7063fa5) Co-authored-by: Jacob Walls --- doc/whatsnew/fragments/9110.false_positive | 3 +++ pylint/checkers/variables.py | 3 +++ pyproject.toml | 2 +- requirements_test_min.txt | 2 +- tests/functional/u/used/used_before_assignment_py312.py | 6 ++++++ tests/functional/u/used/used_before_assignment_py312.rc | 2 ++ 6 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 doc/whatsnew/fragments/9110.false_positive create mode 100644 tests/functional/u/used/used_before_assignment_py312.py create mode 100644 tests/functional/u/used/used_before_assignment_py312.rc diff --git a/doc/whatsnew/fragments/9110.false_positive b/doc/whatsnew/fragments/9110.false_positive new file mode 100644 index 0000000000..f71deed987 --- /dev/null +++ b/doc/whatsnew/fragments/9110.false_positive @@ -0,0 +1,3 @@ +Fix ``used-before-assignment`` false positive for generic type syntax (PEP 695, Python 3.12). + +Closes #9110 diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index b0723cdf1d..6cf3924665 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -120,6 +120,7 @@ nodes.Expr, nodes.Return, nodes.Match, + nodes.TypeAlias, ) @@ -2329,6 +2330,8 @@ def _maybe_used_and_assigned_at_once(defstmt: nodes.Statement) -> bool: return any(case.guard for case in defstmt.cases) if isinstance(defstmt, nodes.IfExp): return True + if isinstance(defstmt, nodes.TypeAlias): + return True if isinstance(defstmt.value, nodes.BaseContainer): return any( VariablesChecker._maybe_used_and_assigned_at_once(elt) diff --git a/pyproject.toml b/pyproject.toml index ac52bd9c23..322e6d1b3d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,7 +41,7 @@ dependencies = [ # Also upgrade requirements_test_min.txt. # Pinned to dev of second minor update to allow editable installs and fix primer issues, # see https://github.com/pylint-dev/astroid/issues/1341 - "astroid>=3.0.0,<=3.1.0-dev0", + "astroid>=3.0.1,<=3.1.0-dev0", "isort>=4.2.5,<6", "mccabe>=0.6,<0.8", "tomli>=1.1.0;python_version<'3.11'", diff --git a/requirements_test_min.txt b/requirements_test_min.txt index 4ebc9c2878..8f7fa3ec89 100644 --- a/requirements_test_min.txt +++ b/requirements_test_min.txt @@ -1,6 +1,6 @@ .[testutils,spelling] # astroid dependency is also defined in pyproject.toml -astroid==3.0.0 # Pinned to a specific version for tests +astroid==3.0.1 # Pinned to a specific version for tests typing-extensions~=4.8 py~=1.11.0 pytest~=7.4 diff --git a/tests/functional/u/used/used_before_assignment_py312.py b/tests/functional/u/used/used_before_assignment_py312.py new file mode 100644 index 0000000000..f47e005b85 --- /dev/null +++ b/tests/functional/u/used/used_before_assignment_py312.py @@ -0,0 +1,6 @@ +"""used-before-assignment re: python 3.12 generic typing syntax (PEP 695)""" + +from typing import Callable +type Point[T] = tuple[T, ...] +type Alias[*Ts] = tuple[*Ts] +type Alias[**P] = Callable[P] diff --git a/tests/functional/u/used/used_before_assignment_py312.rc b/tests/functional/u/used/used_before_assignment_py312.rc new file mode 100644 index 0000000000..9c966d4bda --- /dev/null +++ b/tests/functional/u/used/used_before_assignment_py312.rc @@ -0,0 +1,2 @@ +[testoptions] +min_pyver=3.12