Avoid erroneous RUF013 violations for quoted annotations (#5234)
## Summary Temporary fix for #5231: if we can't flag and fix these properly, just disabling them for now. \cc @dhruvmanila ## Test Plan `cargo test`
This commit is contained in:
@@ -185,3 +185,18 @@ def f(arg: Union[Annotated[int, ...], Annotated[Optional[float], ...]] = None):
|
||||
|
||||
def f(arg: Union[Annotated[int, ...], Union[str, bytes]] = None): # RUF011
|
||||
pass
|
||||
|
||||
|
||||
# Quoted
|
||||
|
||||
|
||||
def f(arg: "int" = None):
|
||||
pass
|
||||
|
||||
|
||||
def f(arg: "str" = None):
|
||||
pass
|
||||
|
||||
|
||||
def f(arg: "Optional[int]" = None):
|
||||
pass
|
||||
|
||||
@@ -142,6 +142,7 @@ enum TypingTarget<'a> {
|
||||
None,
|
||||
Any,
|
||||
Object,
|
||||
ForwardReference,
|
||||
Optional,
|
||||
Union(Vec<&'a Expr>),
|
||||
Literal(Vec<&'a Expr>),
|
||||
@@ -175,6 +176,10 @@ impl<'a> TypingTarget<'a> {
|
||||
value: Constant::None,
|
||||
..
|
||||
}) => Some(TypingTarget::None),
|
||||
Expr::Constant(ast::ExprConstant {
|
||||
value: Constant::Str(_),
|
||||
..
|
||||
}) => Some(TypingTarget::ForwardReference),
|
||||
_ => semantic.resolve_call_path(expr).and_then(|call_path| {
|
||||
if semantic.match_typing_call_path(&call_path, "Any") {
|
||||
Some(TypingTarget::Any)
|
||||
@@ -196,8 +201,8 @@ impl<'a> TypingTarget<'a> {
|
||||
| TypingTarget::Object => true,
|
||||
TypingTarget::Literal(elements) => elements.iter().any(|element| {
|
||||
let Some(new_target) = TypingTarget::try_from_expr(element, semantic) else {
|
||||
return false;
|
||||
};
|
||||
return false;
|
||||
};
|
||||
// Literal can only contain `None`, a literal value, other `Literal`
|
||||
// or an enum value.
|
||||
match new_target {
|
||||
@@ -208,8 +213,8 @@ impl<'a> TypingTarget<'a> {
|
||||
}),
|
||||
TypingTarget::Union(elements) => elements.iter().any(|element| {
|
||||
let Some(new_target) = TypingTarget::try_from_expr(element, semantic) else {
|
||||
return false;
|
||||
};
|
||||
return false;
|
||||
};
|
||||
match new_target {
|
||||
TypingTarget::None => true,
|
||||
_ => new_target.contains_none(semantic),
|
||||
@@ -217,13 +222,15 @@ impl<'a> TypingTarget<'a> {
|
||||
}),
|
||||
TypingTarget::Annotated(element) => {
|
||||
let Some(new_target) = TypingTarget::try_from_expr(element, semantic) else {
|
||||
return false;
|
||||
};
|
||||
return false;
|
||||
};
|
||||
match new_target {
|
||||
TypingTarget::None => true,
|
||||
_ => new_target.contains_none(semantic),
|
||||
}
|
||||
}
|
||||
// TODO(charlie): Add support for forward references (quoted annotations).
|
||||
TypingTarget::ForwardReference => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -305,7 +312,7 @@ fn generate_fix(checker: &Checker, conversion_type: ConversionType, expr: &Expr)
|
||||
}
|
||||
}
|
||||
|
||||
/// RUF011
|
||||
/// RUF013
|
||||
pub(crate) fn implicit_optional(checker: &mut Checker, arguments: &Arguments) {
|
||||
for ArgWithDefault {
|
||||
def,
|
||||
|
||||
@@ -312,5 +312,7 @@ RUF013_0.py:186:12: RUF013 [*] PEP 484 prohibits implicit `Optional`
|
||||
186 |-def f(arg: Union[Annotated[int, ...], Union[str, bytes]] = None): # RUF011
|
||||
186 |+def f(arg: Optional[Union[Annotated[int, ...], Union[str, bytes]]] = None): # RUF011
|
||||
187 187 | pass
|
||||
188 188 |
|
||||
189 189 |
|
||||
|
||||
|
||||
|
||||
@@ -312,5 +312,7 @@ RUF013_0.py:186:12: RUF013 [*] PEP 484 prohibits implicit `Optional`
|
||||
186 |-def f(arg: Union[Annotated[int, ...], Union[str, bytes]] = None): # RUF011
|
||||
186 |+def f(arg: Union[Annotated[int, ...], Union[str, bytes]] | None = None): # RUF011
|
||||
187 187 | pass
|
||||
188 188 |
|
||||
189 189 |
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user