mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-30 08:23:53 +00:00
Improve detection of whether a symbol refers to a builtin exception (#13215)
This commit is contained in:
parent
9d517061f2
commit
387af831f9
6 changed files with 150 additions and 95 deletions
|
@ -11,7 +11,7 @@
|
|||
/// ```
|
||||
///
|
||||
/// Intended to be kept in sync with [`is_ipython_builtin`].
|
||||
pub const IPYTHON_BUILTINS: &[&str] = &["__IPYTHON__", "display", "get_ipython"];
|
||||
const IPYTHON_BUILTINS: &[&str] = &["__IPYTHON__", "display", "get_ipython"];
|
||||
|
||||
/// Globally defined names which are not attributes of the builtins module, or
|
||||
/// are only present on some platforms.
|
||||
|
@ -26,7 +26,7 @@ pub const MAGIC_GLOBALS: &[&str] = &[
|
|||
/// Return the list of builtins for the given Python minor version.
|
||||
///
|
||||
/// Intended to be kept in sync with [`is_python_builtin`].
|
||||
pub fn python_builtins(minor: u8) -> Vec<&'static str> {
|
||||
pub fn python_builtins(minor_version: u8, is_notebook: bool) -> Vec<&'static str> {
|
||||
let mut builtins = vec![
|
||||
"ArithmeticError",
|
||||
"AssertionError",
|
||||
|
@ -182,16 +182,20 @@ pub fn python_builtins(minor: u8) -> Vec<&'static str> {
|
|||
"zip",
|
||||
];
|
||||
|
||||
if minor >= 10 {
|
||||
builtins.extend(vec!["EncodingWarning", "aiter", "anext"]);
|
||||
if minor_version >= 10 {
|
||||
builtins.extend(&["EncodingWarning", "aiter", "anext"]);
|
||||
}
|
||||
|
||||
if minor >= 11 {
|
||||
builtins.extend(vec!["BaseExceptionGroup", "ExceptionGroup"]);
|
||||
if minor_version >= 11 {
|
||||
builtins.extend(&["BaseExceptionGroup", "ExceptionGroup"]);
|
||||
}
|
||||
|
||||
if minor >= 13 {
|
||||
builtins.extend(vec!["PythonFinalizationError"]);
|
||||
if minor_version >= 13 {
|
||||
builtins.push("PythonFinalizationError");
|
||||
}
|
||||
|
||||
if is_notebook {
|
||||
builtins.extend(IPYTHON_BUILTINS);
|
||||
}
|
||||
|
||||
builtins
|
||||
|
@ -200,7 +204,10 @@ pub fn python_builtins(minor: u8) -> Vec<&'static str> {
|
|||
/// Returns `true` if the given name is that of a Python builtin.
|
||||
///
|
||||
/// Intended to be kept in sync with [`python_builtins`].
|
||||
pub fn is_python_builtin(name: &str, minor_version: u8) -> bool {
|
||||
pub fn is_python_builtin(name: &str, minor_version: u8, is_notebook: bool) -> bool {
|
||||
if is_notebook && is_ipython_builtin(name) {
|
||||
return true;
|
||||
}
|
||||
matches!(
|
||||
(minor_version, name),
|
||||
(
|
||||
|
@ -374,7 +381,7 @@ pub fn is_iterator(name: &str) -> bool {
|
|||
/// Returns `true` if the given name is that of an IPython builtin.
|
||||
///
|
||||
/// Intended to be kept in sync with [`IPYTHON_BUILTINS`].
|
||||
pub fn is_ipython_builtin(name: &str) -> bool {
|
||||
fn is_ipython_builtin(name: &str) -> bool {
|
||||
// Constructed by converting the `IPYTHON_BUILTINS` slice to a `match` expression.
|
||||
matches!(name, "__IPYTHON__" | "display" | "get_ipython")
|
||||
}
|
||||
|
@ -382,75 +389,77 @@ pub fn is_ipython_builtin(name: &str) -> bool {
|
|||
/// Returns `true` if the given name is that of a builtin exception.
|
||||
///
|
||||
/// See: <https://docs.python.org/3/library/exceptions.html#exception-hierarchy>
|
||||
pub fn is_exception(name: &str) -> bool {
|
||||
pub fn is_exception(name: &str, minor_version: u8) -> bool {
|
||||
matches!(
|
||||
name,
|
||||
"BaseException"
|
||||
| "BaseExceptionGroup"
|
||||
| "GeneratorExit"
|
||||
| "KeyboardInterrupt"
|
||||
| "SystemExit"
|
||||
| "Exception"
|
||||
| "ArithmeticError"
|
||||
| "FloatingPointError"
|
||||
| "OverflowError"
|
||||
| "ZeroDivisionError"
|
||||
| "AssertionError"
|
||||
| "AttributeError"
|
||||
| "BufferError"
|
||||
| "EOFError"
|
||||
| "ExceptionGroup"
|
||||
| "ImportError"
|
||||
| "ModuleNotFoundError"
|
||||
| "LookupError"
|
||||
| "IndexError"
|
||||
| "KeyError"
|
||||
| "MemoryError"
|
||||
| "NameError"
|
||||
| "UnboundLocalError"
|
||||
| "OSError"
|
||||
| "BlockingIOError"
|
||||
| "ChildProcessError"
|
||||
| "ConnectionError"
|
||||
| "BrokenPipeError"
|
||||
| "ConnectionAbortedError"
|
||||
| "ConnectionRefusedError"
|
||||
| "ConnectionResetError"
|
||||
| "FileExistsError"
|
||||
| "FileNotFoundError"
|
||||
| "InterruptedError"
|
||||
| "IsADirectoryError"
|
||||
| "NotADirectoryError"
|
||||
| "PermissionError"
|
||||
| "ProcessLookupError"
|
||||
| "TimeoutError"
|
||||
| "ReferenceError"
|
||||
| "RuntimeError"
|
||||
| "NotImplementedError"
|
||||
| "RecursionError"
|
||||
| "StopAsyncIteration"
|
||||
| "StopIteration"
|
||||
| "SyntaxError"
|
||||
| "IndentationError"
|
||||
| "TabError"
|
||||
| "SystemError"
|
||||
| "TypeError"
|
||||
| "ValueError"
|
||||
| "UnicodeError"
|
||||
| "UnicodeDecodeError"
|
||||
| "UnicodeEncodeError"
|
||||
| "UnicodeTranslateError"
|
||||
| "Warning"
|
||||
| "BytesWarning"
|
||||
| "DeprecationWarning"
|
||||
| "EncodingWarning"
|
||||
| "FutureWarning"
|
||||
| "ImportWarning"
|
||||
| "PendingDeprecationWarning"
|
||||
| "ResourceWarning"
|
||||
| "RuntimeWarning"
|
||||
| "SyntaxWarning"
|
||||
| "UnicodeWarning"
|
||||
| "UserWarning"
|
||||
(minor_version, name),
|
||||
(
|
||||
_,
|
||||
"BaseException"
|
||||
| "GeneratorExit"
|
||||
| "KeyboardInterrupt"
|
||||
| "SystemExit"
|
||||
| "Exception"
|
||||
| "ArithmeticError"
|
||||
| "FloatingPointError"
|
||||
| "OverflowError"
|
||||
| "ZeroDivisionError"
|
||||
| "AssertionError"
|
||||
| "AttributeError"
|
||||
| "BufferError"
|
||||
| "EOFError"
|
||||
| "ImportError"
|
||||
| "ModuleNotFoundError"
|
||||
| "LookupError"
|
||||
| "IndexError"
|
||||
| "KeyError"
|
||||
| "MemoryError"
|
||||
| "NameError"
|
||||
| "UnboundLocalError"
|
||||
| "OSError"
|
||||
| "BlockingIOError"
|
||||
| "ChildProcessError"
|
||||
| "ConnectionError"
|
||||
| "BrokenPipeError"
|
||||
| "ConnectionAbortedError"
|
||||
| "ConnectionRefusedError"
|
||||
| "ConnectionResetError"
|
||||
| "FileExistsError"
|
||||
| "FileNotFoundError"
|
||||
| "InterruptedError"
|
||||
| "IsADirectoryError"
|
||||
| "NotADirectoryError"
|
||||
| "PermissionError"
|
||||
| "ProcessLookupError"
|
||||
| "TimeoutError"
|
||||
| "ReferenceError"
|
||||
| "RuntimeError"
|
||||
| "NotImplementedError"
|
||||
| "RecursionError"
|
||||
| "StopAsyncIteration"
|
||||
| "StopIteration"
|
||||
| "SyntaxError"
|
||||
| "IndentationError"
|
||||
| "TabError"
|
||||
| "SystemError"
|
||||
| "TypeError"
|
||||
| "ValueError"
|
||||
| "UnicodeError"
|
||||
| "UnicodeDecodeError"
|
||||
| "UnicodeEncodeError"
|
||||
| "UnicodeTranslateError"
|
||||
| "Warning"
|
||||
| "BytesWarning"
|
||||
| "DeprecationWarning"
|
||||
| "FutureWarning"
|
||||
| "ImportWarning"
|
||||
| "PendingDeprecationWarning"
|
||||
| "ResourceWarning"
|
||||
| "RuntimeWarning"
|
||||
| "SyntaxWarning"
|
||||
| "UnicodeWarning"
|
||||
| "UserWarning"
|
||||
) | (10..=13, "EncodingWarning")
|
||||
| (11..=13, "BaseExceptionGroup" | "ExceptionGroup")
|
||||
| (13, "PythonFinalizationError")
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue