mirror of https://github.com/astral-sh/ruff
Allow `typing_extensions.TypeVar` assignments in `.pyi` files (#3951)
This commit is contained in:
parent
71e807b3be
commit
9067ae47d1
|
|
@ -3,6 +3,7 @@ use rustpython_parser::ast::{Arguments, Constant, Expr, ExprKind, Operator, Unar
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::types::Range;
|
use ruff_python_ast::types::Range;
|
||||||
|
use ruff_python_semantic::context::Context;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::registry::AsRule;
|
use crate::registry::AsRule;
|
||||||
|
|
@ -221,20 +222,20 @@ fn is_valid_default_value_with_annotation(
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_type_var_call(checker: &Checker, expr: &Expr) -> bool {
|
/// Returns `true` if an [`Expr`] appears to be `TypeVar`, `TypeVarTuple`, `NewType`, or `ParamSpec`
|
||||||
|
/// call.
|
||||||
|
fn is_type_var_like_call(context: &Context, expr: &Expr) -> bool {
|
||||||
let ExprKind::Call {func, ..} = &expr.node else {
|
let ExprKind::Call {func, ..} = &expr.node else {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
checker
|
context.resolve_call_path(func).map_or(false, |call_path| {
|
||||||
.ctx
|
matches!(
|
||||||
.resolve_call_path(func)
|
call_path.as_slice(),
|
||||||
.map_or(false, |call_path| {
|
[
|
||||||
call_path.as_slice() == ["typing", "TypeVar"]
|
"typing" | "typing_extensions",
|
||||||
|| call_path.as_slice() == ["typing", "TypeVarTuple"]
|
"TypeVar" | "TypeVarTuple" | "NewType" | "ParamSpec"
|
||||||
|| call_path.as_slice() == ["typing_extensions", "TypeVarTuple"]
|
]
|
||||||
|| call_path.as_slice() == ["typing", "NewType"]
|
)
|
||||||
|| call_path.as_slice() == ["typing", "ParamSpec"]
|
|
||||||
|| call_path.as_slice() == ["typing_extensions", "ParamSpec"]
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -359,7 +360,7 @@ pub fn assignment_default_in_stub(checker: &mut Checker, value: &Expr, annotatio
|
||||||
}) {
|
}) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if is_type_var_call(checker, value) {
|
if is_type_var_like_call(&checker.ctx, value) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if !is_valid_default_value_with_annotation(value, checker, true) {
|
if !is_valid_default_value_with_annotation(value, checker, true) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue