mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
Avoid parsing ForwardRef
contents as type references (#3698)
This commit is contained in:
parent
e8d17d23cb
commit
ba43d6bd0b
5 changed files with 28 additions and 10 deletions
8
crates/ruff/resources/test/fixtures/pyflakes/F821_13.py
vendored
Normal file
8
crates/ruff/resources/test/fixtures/pyflakes/F821_13.py
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
"""Test case: ForwardRef."""
|
||||
|
||||
from typing import ForwardRef, TypeVar
|
||||
|
||||
X = ForwardRef("List[int]")
|
||||
Y: ForwardRef("List[int]")
|
||||
|
||||
Z = TypeVar("X", "List[int]", "int")
|
|
@ -3421,9 +3421,7 @@ where
|
|||
keywords,
|
||||
} => {
|
||||
let callable = self.ctx.resolve_call_path(func).and_then(|call_path| {
|
||||
if self.ctx.match_typing_call_path(&call_path, "ForwardRef") {
|
||||
Some(Callable::ForwardRef)
|
||||
} else if self.ctx.match_typing_call_path(&call_path, "cast") {
|
||||
if self.ctx.match_typing_call_path(&call_path, "cast") {
|
||||
Some(Callable::Cast)
|
||||
} else if self.ctx.match_typing_call_path(&call_path, "NewType") {
|
||||
Some(Callable::NewType)
|
||||
|
@ -3450,12 +3448,6 @@ where
|
|||
}
|
||||
});
|
||||
match callable {
|
||||
Some(Callable::ForwardRef) => {
|
||||
self.visit_expr(func);
|
||||
for expr in args {
|
||||
visit_type_definition!(self, expr);
|
||||
}
|
||||
}
|
||||
Some(Callable::Cast) => {
|
||||
self.visit_expr(func);
|
||||
if !args.is_empty() {
|
||||
|
|
|
@ -107,6 +107,7 @@ mod tests {
|
|||
#[test_case(Rule::UndefinedName, Path::new("F821_10.py"); "F821_10")]
|
||||
#[test_case(Rule::UndefinedName, Path::new("F821_11.py"); "F821_11")]
|
||||
#[test_case(Rule::UndefinedName, Path::new("F821_12.py"); "F821_12")]
|
||||
#[test_case(Rule::UndefinedName, Path::new("F821_13.py"); "F821_13")]
|
||||
#[test_case(Rule::UndefinedExport, Path::new("F822_0.py"); "F822_0")]
|
||||
#[test_case(Rule::UndefinedExport, Path::new("F822_1.py"); "F822_1")]
|
||||
#[test_case(Rule::UndefinedExport, Path::new("F822_2.py"); "F822_2")]
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
---
|
||||
source: crates/ruff/src/rules/pyflakes/mod.rs
|
||||
expression: diagnostics
|
||||
---
|
||||
- kind:
|
||||
name: UndefinedName
|
||||
body: "Undefined name `List`"
|
||||
suggestion: ~
|
||||
fixable: false
|
||||
location:
|
||||
row: 8
|
||||
column: 18
|
||||
end_location:
|
||||
row: 8
|
||||
column: 22
|
||||
fix: ~
|
||||
parent: ~
|
||||
|
|
@ -11,7 +11,6 @@ use crate::str;
|
|||
use crate::types::Range;
|
||||
|
||||
pub enum Callable {
|
||||
ForwardRef,
|
||||
Cast,
|
||||
NewType,
|
||||
TypeVar,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue