mirror of https://github.com/astral-sh/ruff
Treat TypeAlias values as annotations (#377)
This commit is contained in:
parent
aafe7c0c39
commit
30b1b1e15a
|
|
@ -67,7 +67,22 @@ c = cast("Vegetable", b)
|
||||||
Field = lambda default=MISSING: field(default=default)
|
Field = lambda default=MISSING: field(default=default)
|
||||||
|
|
||||||
|
|
||||||
|
# Test: access a sub-importation via an alias.
|
||||||
import pyarrow as pa
|
import pyarrow as pa
|
||||||
import pyarrow.csv
|
import pyarrow.csv
|
||||||
|
|
||||||
print(pa.csv.read_csv("test.csv"))
|
print(pa.csv.read_csv("test.csv"))
|
||||||
|
|
||||||
|
|
||||||
|
# Test: referencing an import via TypeAlias.
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
if sys.version_info >= (3, 10):
|
||||||
|
from typing import TypeAlias
|
||||||
|
else:
|
||||||
|
from typing_extensions import TypeAlias
|
||||||
|
|
||||||
|
|
||||||
|
CustomInt: TypeAlias = "np.int8 | np.int16"
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::ast::helpers::match_name_or_attr;
|
||||||
use rustpython_parser::ast::{
|
use rustpython_parser::ast::{
|
||||||
Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, Excepthandler,
|
Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, Excepthandler,
|
||||||
ExcepthandlerKind, Expr, ExprContext, ExprKind, Keyword, MatchCase, Operator, Pattern,
|
ExcepthandlerKind, Expr, ExprContext, ExprKind, Keyword, MatchCase, Operator, Pattern,
|
||||||
|
|
@ -148,7 +149,11 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
||||||
} => {
|
} => {
|
||||||
visitor.visit_annotation(annotation);
|
visitor.visit_annotation(annotation);
|
||||||
if let Some(expr) = value {
|
if let Some(expr) = value {
|
||||||
visitor.visit_expr(expr);
|
if match_name_or_attr(annotation, "TypeAlias") {
|
||||||
|
visitor.visit_annotation(expr);
|
||||||
|
} else {
|
||||||
|
visitor.visit_expr(expr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
visitor.visit_expr(target);
|
visitor.visit_expr(target);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue