Minor refactoring of some flake-pyi rules (#14275)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
Simon Brugman 2024-11-11 14:10:48 +01:00 committed by GitHub
parent f8aae9b1d6
commit b3b5c19105
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 37 additions and 30 deletions

View file

@ -35,14 +35,24 @@ 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") {
else {
return;
};
if module_name != "__future__" {
return;
};
if names.iter().all(|alias| &*alias.name != "annotations") {
return;
}
let mut diagnostic = Diagnostic::new(FutureAnnotationsInStub, *range);
if checker.settings.preview.is_enabled() {
@ -64,5 +74,3 @@ pub(crate) fn from_future_import(checker: &mut Checker, target: &StmtImportFrom)
checker.diagnostics.push(diagnostic);
}
}
}

View file

@ -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::*;

View file

@ -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(),

View file

@ -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)]