From cdae2f0e6719c5c836f8a299b56138cb1b5f5e41 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Mon, 2 Jan 2023 18:13:45 +0100 Subject: [PATCH] Fix typing::match_annotated_subscript matching ExprKind::Call (#1554) --- resources/test/fixtures/pyflakes/F841_2.py | 6 ++++++ src/pyflakes/mod.rs | 1 + .../snapshots/ruff__pyflakes__tests__F841_F841_2.py.snap | 6 ++++++ src/python/typing.rs | 8 +++++++- 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 resources/test/fixtures/pyflakes/F841_2.py create mode 100644 src/pyflakes/snapshots/ruff__pyflakes__tests__F841_F841_2.py.snap diff --git a/resources/test/fixtures/pyflakes/F841_2.py b/resources/test/fixtures/pyflakes/F841_2.py new file mode 100644 index 0000000000..54ad9bda33 --- /dev/null +++ b/resources/test/fixtures/pyflakes/F841_2.py @@ -0,0 +1,6 @@ +from __future__ import annotations + +# test case for https://github.com/charliermarsh/ruff/issues/1552 +def _(): + x = 0 + list()[x:] diff --git a/src/pyflakes/mod.rs b/src/pyflakes/mod.rs index 51ace2e464..23f5613b45 100644 --- a/src/pyflakes/mod.rs +++ b/src/pyflakes/mod.rs @@ -102,6 +102,7 @@ mod tests { #[test_case(CheckCode::F823, Path::new("F823.py"); "F823")] #[test_case(CheckCode::F841, Path::new("F841_0.py"); "F841_0")] #[test_case(CheckCode::F841, Path::new("F841_1.py"); "F841_1")] + #[test_case(CheckCode::F841, Path::new("F841_2.py"); "F841_2")] #[test_case(CheckCode::F842, Path::new("F842.py"); "F842")] #[test_case(CheckCode::F901, Path::new("F901.py"); "F901")] fn checks(check_code: CheckCode, path: &Path) -> Result<()> { diff --git a/src/pyflakes/snapshots/ruff__pyflakes__tests__F841_F841_2.py.snap b/src/pyflakes/snapshots/ruff__pyflakes__tests__F841_F841_2.py.snap new file mode 100644 index 0000000000..43ccad66f9 --- /dev/null +++ b/src/pyflakes/snapshots/ruff__pyflakes__tests__F841_F841_2.py.snap @@ -0,0 +1,6 @@ +--- +source: src/pyflakes/mod.rs +expression: checks +--- +[] + diff --git a/src/python/typing.rs b/src/python/typing.rs index 061e0bd71f..947a28893c 100644 --- a/src/python/typing.rs +++ b/src/python/typing.rs @@ -1,6 +1,6 @@ use once_cell::sync::Lazy; use rustc_hash::{FxHashMap, FxHashSet}; -use rustpython_ast::Expr; +use rustpython_ast::{Expr, ExprKind}; use crate::ast::helpers::{collect_call_paths, dealias_call_path, match_call_path}; @@ -219,6 +219,12 @@ pub fn match_annotated_subscript( where F: Fn(&str) -> bool, { + if !matches!( + expr.node, + ExprKind::Name { .. } | ExprKind::Attribute { .. } + ) { + return None; + } let call_path = dealias_call_path(collect_call_paths(expr), import_aliases); if !call_path.is_empty() { for (module, member) in SUBSCRIPTS {