From 555b3a6a2c8120c405719cabf7444f32136775e1 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Wed, 22 Jan 2025 23:04:20 +0000 Subject: [PATCH] [`pyflakes`] Treat arguments passed to the `default=` parameter of `TypeVar` as type expressions (`F821`) (#15679) --- .../resources/test/fixtures/pyflakes/F821_32.pyi | 6 ++++++ crates/ruff_linter/src/checkers/ast/mod.rs | 2 +- crates/ruff_linter/src/rules/pyflakes/mod.rs | 1 + ...ff_linter__rules__pyflakes__tests__F821_F821_32.pyi.snap | 4 ++++ 4 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 crates/ruff_linter/resources/test/fixtures/pyflakes/F821_32.pyi create mode 100644 crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F821_F821_32.pyi.snap diff --git a/crates/ruff_linter/resources/test/fixtures/pyflakes/F821_32.pyi b/crates/ruff_linter/resources/test/fixtures/pyflakes/F821_32.pyi new file mode 100644 index 0000000000000..d8d1280a47d36 --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/pyflakes/F821_32.pyi @@ -0,0 +1,6 @@ +from typing import TypeVar + +# Forward references are okay for both `bound` and `default` in a stub file +_P = TypeVar("_P", bound=X, default=X) + +class X: ... diff --git a/crates/ruff_linter/src/checkers/ast/mod.rs b/crates/ruff_linter/src/checkers/ast/mod.rs index e456481f2cbcd..82857bd2aac58 100644 --- a/crates/ruff_linter/src/checkers/ast/mod.rs +++ b/crates/ruff_linter/src/checkers/ast/mod.rs @@ -1326,7 +1326,7 @@ impl<'a> Visitor<'a> for Checker<'a> { range: _, } = keyword; if let Some(id) = arg { - if id.as_str() == "bound" { + if matches!(&**id, "bound" | "default") { self.visit_type_definition(value); } else { self.visit_non_type_definition(value); diff --git a/crates/ruff_linter/src/rules/pyflakes/mod.rs b/crates/ruff_linter/src/rules/pyflakes/mod.rs index 0e20e2924675f..a3fc5d81a7d39 100644 --- a/crates/ruff_linter/src/rules/pyflakes/mod.rs +++ b/crates/ruff_linter/src/rules/pyflakes/mod.rs @@ -164,6 +164,7 @@ mod tests { #[test_case(Rule::UndefinedName, Path::new("F821_28.py"))] #[test_case(Rule::UndefinedName, Path::new("F821_30.py"))] #[test_case(Rule::UndefinedName, Path::new("F821_31.py"))] + #[test_case(Rule::UndefinedName, Path::new("F821_32.pyi"))] #[test_case(Rule::UndefinedExport, Path::new("F822_0.py"))] #[test_case(Rule::UndefinedExport, Path::new("F822_0.pyi"))] #[test_case(Rule::UndefinedExport, Path::new("F822_1.py"))] diff --git a/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F821_F821_32.pyi.snap b/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F821_F821_32.pyi.snap new file mode 100644 index 0000000000000..d0b409f39ee0b --- /dev/null +++ b/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F821_F821_32.pyi.snap @@ -0,0 +1,4 @@ +--- +source: crates/ruff_linter/src/rules/pyflakes/mod.rs +--- +