mirror of https://github.com/astral-sh/ruff
Mark UP043 fix unsafe when the type annotation contains any comments (#14458)
This commit is contained in:
parent
5f09d4a90a
commit
dbbe7a773c
|
|
@ -50,3 +50,10 @@ def func() -> Generator[str, None, None]:
|
||||||
|
|
||||||
async def func() -> AsyncGenerator[str, None]:
|
async def func() -> AsyncGenerator[str, None]:
|
||||||
yield "hello"
|
yield "hello"
|
||||||
|
|
||||||
|
|
||||||
|
async def func() -> AsyncGenerator[ # type: ignore
|
||||||
|
str,
|
||||||
|
None
|
||||||
|
]:
|
||||||
|
yield "hello"
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
|
use ruff_diagnostics::{AlwaysFixableViolation, Applicability, Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::{self as ast, Expr};
|
use ruff_python_ast::{self as ast, Expr};
|
||||||
use ruff_text_size::{Ranged, TextRange};
|
use ruff_text_size::{Ranged, TextRange};
|
||||||
|
|
@ -44,6 +44,9 @@ use crate::checkers::ast::Checker;
|
||||||
/// yield 42
|
/// yield 42
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
/// ## Fix safety
|
||||||
|
/// This rule's fix is marked as safe, unless the type annotation contains comments.
|
||||||
|
///
|
||||||
/// ## References
|
/// ## References
|
||||||
/// - [PEP 696 – Type Defaults for Type Parameters](https://peps.python.org/pep-0696/)
|
/// - [PEP 696 – Type Defaults for Type Parameters](https://peps.python.org/pep-0696/)
|
||||||
/// - [Annotating generators and coroutines](https://docs.python.org/3/library/typing.html#annotating-generators-and-coroutines)
|
/// - [Annotating generators and coroutines](https://docs.python.org/3/library/typing.html#annotating-generators-and-coroutines)
|
||||||
|
|
@ -93,7 +96,18 @@ pub(crate) fn unnecessary_default_type_args(checker: &mut Checker, expr: &Expr)
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut diagnostic = Diagnostic::new(UnnecessaryDefaultTypeArgs, expr.range());
|
let mut diagnostic = Diagnostic::new(UnnecessaryDefaultTypeArgs, expr.range());
|
||||||
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
|
|
||||||
|
let applicability = if checker
|
||||||
|
.comment_ranges()
|
||||||
|
.has_comments(expr, checker.source())
|
||||||
|
{
|
||||||
|
Applicability::Unsafe
|
||||||
|
} else {
|
||||||
|
Applicability::Safe
|
||||||
|
};
|
||||||
|
|
||||||
|
diagnostic.set_fix(Fix::applicable_edit(
|
||||||
|
Edit::range_replacement(
|
||||||
checker
|
checker
|
||||||
.generator()
|
.generator()
|
||||||
.expr(&Expr::Subscript(ast::ExprSubscript {
|
.expr(&Expr::Subscript(ast::ExprSubscript {
|
||||||
|
|
@ -112,7 +126,9 @@ pub(crate) fn unnecessary_default_type_args(checker: &mut Checker, expr: &Expr)
|
||||||
range: TextRange::default(),
|
range: TextRange::default(),
|
||||||
})),
|
})),
|
||||||
expr.range(),
|
expr.range(),
|
||||||
)));
|
),
|
||||||
|
applicability,
|
||||||
|
));
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -108,3 +108,28 @@ UP043.py:51:21: UP043 [*] Unnecessary default type arguments
|
||||||
51 |-async def func() -> AsyncGenerator[str, None]:
|
51 |-async def func() -> AsyncGenerator[str, None]:
|
||||||
51 |+async def func() -> AsyncGenerator[str]:
|
51 |+async def func() -> AsyncGenerator[str]:
|
||||||
52 52 | yield "hello"
|
52 52 | yield "hello"
|
||||||
|
53 53 |
|
||||||
|
54 54 |
|
||||||
|
|
||||||
|
UP043.py:55:21: UP043 [*] Unnecessary default type arguments
|
||||||
|
|
|
||||||
|
55 | async def func() -> AsyncGenerator[ # type: ignore
|
||||||
|
| _____________________^
|
||||||
|
56 | | str,
|
||||||
|
57 | | None
|
||||||
|
58 | | ]:
|
||||||
|
| |_^ UP043
|
||||||
|
59 | yield "hello"
|
||||||
|
|
|
||||||
|
= help: Remove default type arguments
|
||||||
|
|
||||||
|
ℹ Unsafe fix
|
||||||
|
52 52 | yield "hello"
|
||||||
|
53 53 |
|
||||||
|
54 54 |
|
||||||
|
55 |-async def func() -> AsyncGenerator[ # type: ignore
|
||||||
|
56 |- str,
|
||||||
|
57 |- None
|
||||||
|
58 |-]:
|
||||||
|
55 |+async def func() -> AsyncGenerator[str]:
|
||||||
|
59 56 | yield "hello"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue