Skip to content

Commit

Permalink
Avoid erroneous RUF013 violations for quoted annotations (#5234)
Browse files Browse the repository at this point in the history
## 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`
  • Loading branch information
charliermarsh authored Jun 21, 2023
1 parent 621e9ac commit 1db7d9e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
15 changes: 15 additions & 0 deletions crates/ruff/resources/test/fixtures/ruff/RUF013_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 14 additions & 7 deletions crates/ruff/src/rules/ruff/rules/implicit_optional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ enum TypingTarget<'a> {
None,
Any,
Object,
ForwardReference,
Optional,
Union(Vec<&'a Expr>),
Literal(Vec<&'a Expr>),
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -208,22 +213,24 @@ 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),
}
}),
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,
}
}
}
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 |


Original file line number Diff line number Diff line change
Expand Up @@ -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 |


0 comments on commit 1db7d9e

Please sign in to comment.