mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-03 07:04:53 +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,14 +35,24 @@ impl Violation for FutureAnnotationsInStub {
|
||||||
|
|
||||||
/// PYI044
|
/// PYI044
|
||||||
pub(crate) fn from_future_import(checker: &mut Checker, target: &StmtImportFrom) {
|
pub(crate) fn from_future_import(checker: &mut Checker, target: &StmtImportFrom) {
|
||||||
if let StmtImportFrom {
|
let StmtImportFrom {
|
||||||
range,
|
range,
|
||||||
module: Some(name),
|
module: Some(module_name),
|
||||||
names,
|
names,
|
||||||
..
|
..
|
||||||
} = target
|
} = target
|
||||||
{
|
else {
|
||||||
if name == "__future__" && names.iter().any(|alias| &*alias.name == "annotations") {
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
if module_name != "__future__" {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
if names.iter().all(|alias| &*alias.name != "annotations") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let mut diagnostic = Diagnostic::new(FutureAnnotationsInStub, *range);
|
let mut diagnostic = Diagnostic::new(FutureAnnotationsInStub, *range);
|
||||||
|
|
||||||
if checker.settings.preview.is_enabled() {
|
if checker.settings.preview.is_enabled() {
|
||||||
|
@ -63,6 +73,4 @@ pub(crate) fn from_future_import(checker: &mut Checker, target: &StmtImportFrom)
|
||||||
}
|
}
|
||||||
|
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
pub(crate) use any_eq_ne_annotation::*;
|
pub(crate) use any_eq_ne_annotation::*;
|
||||||
pub(crate) use bad_generator_return_type::*;
|
pub(crate) use bad_generator_return_type::*;
|
||||||
pub(crate) use bad_version_info_comparison::*;
|
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_literal_union::*;
|
||||||
pub(crate) use redundant_numeric_union::*;
|
pub(crate) use redundant_numeric_union::*;
|
||||||
pub(crate) use simple_defaults::*;
|
pub(crate) use simple_defaults::*;
|
||||||
use std::fmt;
|
|
||||||
pub(crate) use str_or_repr_defined_in_stub::*;
|
pub(crate) use str_or_repr_defined_in_stub::*;
|
||||||
pub(crate) use string_or_bytes_too_long::*;
|
pub(crate) use string_or_bytes_too_long::*;
|
||||||
pub(crate) use stub_body_multiple_statements::*;
|
pub(crate) use stub_body_multiple_statements::*;
|
||||||
|
|
|
@ -131,8 +131,6 @@ pub(crate) fn suppressible_exception(
|
||||||
.has_comments(stmt, checker.source())
|
.has_comments(stmt, checker.source())
|
||||||
{
|
{
|
||||||
diagnostic.try_set_fix(|| {
|
diagnostic.try_set_fix(|| {
|
||||||
// let range = statement_range(stmt, checker.locator(), checker.indexer());
|
|
||||||
|
|
||||||
let (import_edit, binding) = checker.importer().get_or_import_symbol(
|
let (import_edit, binding) = checker.importer().get_or_import_symbol(
|
||||||
&ImportRequest::import("contextlib", "suppress"),
|
&ImportRequest::import("contextlib", "suppress"),
|
||||||
stmt.start(),
|
stmt.start(),
|
||||||
|
|
|
@ -47,7 +47,7 @@ impl AlwaysFixableViolation for NeverUnion {
|
||||||
union_like,
|
union_like,
|
||||||
} = self;
|
} = self;
|
||||||
match union_like {
|
match union_like {
|
||||||
UnionLike::BinOp => {
|
UnionLike::PEP604 => {
|
||||||
format!("`{never_like} | T` is equivalent to `T`")
|
format!("`{never_like} | T` is equivalent to `T`")
|
||||||
}
|
}
|
||||||
UnionLike::TypingUnion => {
|
UnionLike::TypingUnion => {
|
||||||
|
@ -77,7 +77,7 @@ pub(crate) fn never_union(checker: &mut Checker, expr: &Expr) {
|
||||||
let mut diagnostic = Diagnostic::new(
|
let mut diagnostic = Diagnostic::new(
|
||||||
NeverUnion {
|
NeverUnion {
|
||||||
never_like,
|
never_like,
|
||||||
union_like: UnionLike::BinOp,
|
union_like: UnionLike::PEP604,
|
||||||
},
|
},
|
||||||
left.range(),
|
left.range(),
|
||||||
);
|
);
|
||||||
|
@ -93,7 +93,7 @@ pub(crate) fn never_union(checker: &mut Checker, expr: &Expr) {
|
||||||
let mut diagnostic = Diagnostic::new(
|
let mut diagnostic = Diagnostic::new(
|
||||||
NeverUnion {
|
NeverUnion {
|
||||||
never_like,
|
never_like,
|
||||||
union_like: UnionLike::BinOp,
|
union_like: UnionLike::PEP604,
|
||||||
},
|
},
|
||||||
right.range(),
|
right.range(),
|
||||||
);
|
);
|
||||||
|
@ -174,7 +174,7 @@ enum UnionLike {
|
||||||
/// E.g., `typing.Union[int, str]`
|
/// E.g., `typing.Union[int, str]`
|
||||||
TypingUnion,
|
TypingUnion,
|
||||||
/// E.g., `int | str`
|
/// E.g., `int | str`
|
||||||
BinOp,
|
PEP604,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue