Avoid parsing ForwardRef contents as type references (#3698)

This commit is contained in:
Charlie Marsh 2023-03-23 18:44:02 -04:00 committed by GitHub
parent e8d17d23cb
commit ba43d6bd0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 10 deletions

View 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")

View file

@ -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() {

View file

@ -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")]

View file

@ -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: ~

View file

@ -11,7 +11,6 @@ use crate::str;
use crate::types::Range;
pub enum Callable {
ForwardRef,
Cast,
NewType,
TypeVar,