mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-15 08:00:46 +00:00
Flag, but don't fix, unused imports in ModuleNotFoundError
blocks (#3658)
This commit is contained in:
parent
3a8e98341b
commit
1b3e54231c
8 changed files with 163 additions and 89 deletions
|
@ -8,10 +8,10 @@ use smallvec::smallvec;
|
|||
use ruff_python_stdlib::path::is_python_stub_file;
|
||||
use ruff_python_stdlib::typing::TYPING_EXTENSIONS;
|
||||
|
||||
use crate::helpers::{collect_call_path, from_relative_import, Exceptions};
|
||||
use crate::helpers::{collect_call_path, from_relative_import};
|
||||
use crate::scope::{
|
||||
Binding, BindingId, BindingKind, Bindings, ExecutionContext, Scope, ScopeId, ScopeKind,
|
||||
ScopeStack, Scopes,
|
||||
Binding, BindingId, BindingKind, Bindings, Exceptions, ExecutionContext, Scope, ScopeId,
|
||||
ScopeKind, ScopeStack, Scopes,
|
||||
};
|
||||
use crate::types::{CallPath, RefEquality};
|
||||
use crate::typing::AnnotationKind;
|
||||
|
@ -309,6 +309,7 @@ impl<'a> Context<'a> {
|
|||
self.in_exception_handler
|
||||
}
|
||||
|
||||
/// Return the [`ExecutionContext`] of the current scope.
|
||||
pub const fn execution_context(&self) -> ExecutionContext {
|
||||
if self.in_type_checking_block
|
||||
|| self.in_annotation
|
||||
|
@ -319,4 +320,13 @@ impl<'a> Context<'a> {
|
|||
ExecutionContext::Runtime
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the union of all handled exceptions as an [`Exceptions`] bitflag.
|
||||
pub fn exceptions(&self) -> Exceptions {
|
||||
let mut exceptions = Exceptions::empty();
|
||||
for exception in &self.handled_exceptions {
|
||||
exceptions.insert(*exception);
|
||||
}
|
||||
exceptions
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use std::path::Path;
|
||||
|
||||
use bitflags::bitflags;
|
||||
use itertools::Itertools;
|
||||
use log::error;
|
||||
use once_cell::sync::Lazy;
|
||||
|
@ -577,13 +576,6 @@ pub fn has_non_none_keyword(keywords: &[Keyword], keyword: &str) -> bool {
|
|||
})
|
||||
}
|
||||
|
||||
bitflags! {
|
||||
pub struct Exceptions: u32 {
|
||||
const NAME_ERROR = 0b0000_0001;
|
||||
const MODULE_NOT_FOUND_ERROR = 0b0000_0010;
|
||||
}
|
||||
}
|
||||
|
||||
/// Extract the names of all handled exceptions.
|
||||
pub fn extract_handled_exceptions(handlers: &[Excepthandler]) -> Vec<&Expr> {
|
||||
let mut handled_exceptions = Vec::new();
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use crate::types::{Range, RefEquality};
|
||||
use bitflags::bitflags;
|
||||
use rustc_hash::FxHashMap;
|
||||
use rustpython_parser::ast::{Arguments, Expr, Keyword, Stmt};
|
||||
use std::num::TryFromIntError;
|
||||
|
@ -222,6 +223,14 @@ impl Default for ScopeStack {
|
|||
}
|
||||
}
|
||||
|
||||
bitflags! {
|
||||
pub struct Exceptions: u32 {
|
||||
const NAME_ERROR = 0b0000_0001;
|
||||
const MODULE_NOT_FOUND_ERROR = 0b0000_0010;
|
||||
const IMPORT_ERROR = 0b0000_0100;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Binding<'a> {
|
||||
pub kind: BindingKind<'a>,
|
||||
|
@ -241,6 +250,8 @@ pub struct Binding<'a> {
|
|||
/// (e.g.) `__future__` imports, explicit re-exports, and other bindings
|
||||
/// that should be considered used even if they're never referenced.
|
||||
pub synthetic_usage: Option<(ScopeId, Range)>,
|
||||
/// The exceptions that were handled when the binding was defined.
|
||||
pub exceptions: Exceptions,
|
||||
}
|
||||
|
||||
impl<'a> Binding<'a> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue