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