mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 22:31:23 +00:00
Minor refactoring of some flake-pyi rules (#14275)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
f8aae9b1d6
commit
b3b5c19105
4 changed files with 37 additions and 30 deletions
|
@ -35,34 +35,42 @@ impl Violation for FutureAnnotationsInStub {
|
|||
|
||||
/// PYI044
|
||||
pub(crate) fn from_future_import(checker: &mut Checker, target: &StmtImportFrom) {
|
||||
if let StmtImportFrom {
|
||||
let StmtImportFrom {
|
||||
range,
|
||||
module: Some(name),
|
||||
module: Some(module_name),
|
||||
names,
|
||||
..
|
||||
} = target
|
||||
{
|
||||
if name == "__future__" && names.iter().any(|alias| &*alias.name == "annotations") {
|
||||
let mut diagnostic = Diagnostic::new(FutureAnnotationsInStub, *range);
|
||||
else {
|
||||
return;
|
||||
};
|
||||
|
||||
if checker.settings.preview.is_enabled() {
|
||||
let stmt = checker.semantic().current_statement();
|
||||
if module_name != "__future__" {
|
||||
return;
|
||||
};
|
||||
|
||||
diagnostic.try_set_fix(|| {
|
||||
let edit = fix::edits::remove_unused_imports(
|
||||
std::iter::once("annotations"),
|
||||
stmt,
|
||||
None,
|
||||
checker.locator(),
|
||||
checker.stylist(),
|
||||
checker.indexer(),
|
||||
)?;
|
||||
|
||||
Ok(Fix::safe_edit(edit))
|
||||
});
|
||||
}
|
||||
|
||||
checker.diagnostics.push(diagnostic);
|
||||
}
|
||||
if names.iter().all(|alias| &*alias.name != "annotations") {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut diagnostic = Diagnostic::new(FutureAnnotationsInStub, *range);
|
||||
|
||||
if checker.settings.preview.is_enabled() {
|
||||
let stmt = checker.semantic().current_statement();
|
||||
|
||||
diagnostic.try_set_fix(|| {
|
||||
let edit = fix::edits::remove_unused_imports(
|
||||
std::iter::once("annotations"),
|
||||
stmt,
|
||||
None,
|
||||
checker.locator(),
|
||||
checker.stylist(),
|
||||
checker.indexer(),
|
||||
)?;
|
||||
|
||||
Ok(Fix::safe_edit(edit))
|
||||
});
|
||||
}
|
||||
|
||||
checker.diagnostics.push(diagnostic);
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
use std::fmt;
|
||||
|
||||
pub(crate) use any_eq_ne_annotation::*;
|
||||
pub(crate) use bad_generator_return_type::*;
|
||||
pub(crate) use bad_version_info_comparison::*;
|
||||
|
@ -27,7 +29,6 @@ pub(crate) use redundant_final_literal::*;
|
|||
pub(crate) use redundant_literal_union::*;
|
||||
pub(crate) use redundant_numeric_union::*;
|
||||
pub(crate) use simple_defaults::*;
|
||||
use std::fmt;
|
||||
pub(crate) use str_or_repr_defined_in_stub::*;
|
||||
pub(crate) use string_or_bytes_too_long::*;
|
||||
pub(crate) use stub_body_multiple_statements::*;
|
||||
|
|
|
@ -131,8 +131,6 @@ pub(crate) fn suppressible_exception(
|
|||
.has_comments(stmt, checker.source())
|
||||
{
|
||||
diagnostic.try_set_fix(|| {
|
||||
// let range = statement_range(stmt, checker.locator(), checker.indexer());
|
||||
|
||||
let (import_edit, binding) = checker.importer().get_or_import_symbol(
|
||||
&ImportRequest::import("contextlib", "suppress"),
|
||||
stmt.start(),
|
||||
|
|
|
@ -47,7 +47,7 @@ impl AlwaysFixableViolation for NeverUnion {
|
|||
union_like,
|
||||
} = self;
|
||||
match union_like {
|
||||
UnionLike::BinOp => {
|
||||
UnionLike::PEP604 => {
|
||||
format!("`{never_like} | T` is equivalent to `T`")
|
||||
}
|
||||
UnionLike::TypingUnion => {
|
||||
|
@ -77,7 +77,7 @@ pub(crate) fn never_union(checker: &mut Checker, expr: &Expr) {
|
|||
let mut diagnostic = Diagnostic::new(
|
||||
NeverUnion {
|
||||
never_like,
|
||||
union_like: UnionLike::BinOp,
|
||||
union_like: UnionLike::PEP604,
|
||||
},
|
||||
left.range(),
|
||||
);
|
||||
|
@ -93,7 +93,7 @@ pub(crate) fn never_union(checker: &mut Checker, expr: &Expr) {
|
|||
let mut diagnostic = Diagnostic::new(
|
||||
NeverUnion {
|
||||
never_like,
|
||||
union_like: UnionLike::BinOp,
|
||||
union_like: UnionLike::PEP604,
|
||||
},
|
||||
right.range(),
|
||||
);
|
||||
|
@ -174,7 +174,7 @@ enum UnionLike {
|
|||
/// E.g., `typing.Union[int, str]`
|
||||
TypingUnion,
|
||||
/// E.g., `int | str`
|
||||
BinOp,
|
||||
PEP604,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue