mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-25 14:24:10 +00:00
Use a match statement for builtin detection (#5798)
## Summary We've seen speed-ups in the past by converting from slice iteration to match statements; this just does the same for built-in checks.
This commit is contained in:
parent
01b05fe247
commit
d692ed0896
3 changed files with 174 additions and 5 deletions
|
|
@ -3756,7 +3756,6 @@ where
|
||||||
// If we're in a type definition, we need to treat the arguments to any
|
// If we're in a type definition, we need to treat the arguments to any
|
||||||
// other callables as non-type definitions (i.e., we don't want to treat
|
// other callables as non-type definitions (i.e., we don't want to treat
|
||||||
// any strings as deferred type definitions).
|
// any strings as deferred type definitions).
|
||||||
|
|
||||||
for arg in args {
|
for arg in args {
|
||||||
self.visit_non_type_definition(arg);
|
self.visit_non_type_definition(arg);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use ruff_python_stdlib::builtins::BUILTINS;
|
use ruff_python_stdlib::builtins::is_builtin;
|
||||||
|
|
||||||
pub(super) fn shadows_builtin(name: &str, ignorelist: &[String]) -> bool {
|
pub(super) fn shadows_builtin(name: &str, ignorelist: &[String]) -> bool {
|
||||||
BUILTINS.contains(&name) && ignorelist.iter().all(|ignore| ignore != name)
|
is_builtin(name) && ignorelist.iter().all(|ignore| ignore != name)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,6 @@
|
||||||
|
/// A list of all Python builtins.
|
||||||
|
///
|
||||||
|
/// Intended to be kept in sync with [`is_builtin`].
|
||||||
pub const BUILTINS: &[&str] = &[
|
pub const BUILTINS: &[&str] = &[
|
||||||
"ArithmeticError",
|
"ArithmeticError",
|
||||||
"AssertionError",
|
"AssertionError",
|
||||||
|
|
@ -158,11 +161,178 @@ pub const BUILTINS: &[&str] = &[
|
||||||
"zip",
|
"zip",
|
||||||
];
|
];
|
||||||
|
|
||||||
// Globally defined names which are not attributes of the builtins module, or
|
/// Globally defined names which are not attributes of the builtins module, or
|
||||||
// are only present on some platforms.
|
/// are only present on some platforms.
|
||||||
pub const MAGIC_GLOBALS: &[&str] = &[
|
pub const MAGIC_GLOBALS: &[&str] = &[
|
||||||
"WindowsError",
|
"WindowsError",
|
||||||
"__annotations__",
|
"__annotations__",
|
||||||
"__builtins__",
|
"__builtins__",
|
||||||
"__file__",
|
"__file__",
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/// Returns `true` if the given name is that of a Python builtin.
|
||||||
|
///
|
||||||
|
/// Intended to be kept in sync with [`BUILTINS`].
|
||||||
|
pub fn is_builtin(name: &str) -> bool {
|
||||||
|
// Constructed by converting the `BUILTINS` slice to a `match` expression.
|
||||||
|
matches!(
|
||||||
|
name,
|
||||||
|
"ArithmeticError"
|
||||||
|
| "AssertionError"
|
||||||
|
| "AttributeError"
|
||||||
|
| "BaseException"
|
||||||
|
| "BaseExceptionGroup"
|
||||||
|
| "BlockingIOError"
|
||||||
|
| "BrokenPipeError"
|
||||||
|
| "BufferError"
|
||||||
|
| "BytesWarning"
|
||||||
|
| "ChildProcessError"
|
||||||
|
| "ConnectionAbortedError"
|
||||||
|
| "ConnectionError"
|
||||||
|
| "ConnectionRefusedError"
|
||||||
|
| "ConnectionResetError"
|
||||||
|
| "DeprecationWarning"
|
||||||
|
| "EOFError"
|
||||||
|
| "Ellipsis"
|
||||||
|
| "EncodingWarning"
|
||||||
|
| "EnvironmentError"
|
||||||
|
| "Exception"
|
||||||
|
| "ExceptionGroup"
|
||||||
|
| "False"
|
||||||
|
| "FileExistsError"
|
||||||
|
| "FileNotFoundError"
|
||||||
|
| "FloatingPointError"
|
||||||
|
| "FutureWarning"
|
||||||
|
| "GeneratorExit"
|
||||||
|
| "IOError"
|
||||||
|
| "ImportError"
|
||||||
|
| "ImportWarning"
|
||||||
|
| "IndentationError"
|
||||||
|
| "IndexError"
|
||||||
|
| "InterruptedError"
|
||||||
|
| "IsADirectoryError"
|
||||||
|
| "KeyError"
|
||||||
|
| "KeyboardInterrupt"
|
||||||
|
| "LookupError"
|
||||||
|
| "MemoryError"
|
||||||
|
| "ModuleNotFoundError"
|
||||||
|
| "NameError"
|
||||||
|
| "None"
|
||||||
|
| "NotADirectoryError"
|
||||||
|
| "NotImplemented"
|
||||||
|
| "NotImplementedError"
|
||||||
|
| "OSError"
|
||||||
|
| "OverflowError"
|
||||||
|
| "PendingDeprecationWarning"
|
||||||
|
| "PermissionError"
|
||||||
|
| "ProcessLookupError"
|
||||||
|
| "RecursionError"
|
||||||
|
| "ReferenceError"
|
||||||
|
| "ResourceWarning"
|
||||||
|
| "RuntimeError"
|
||||||
|
| "RuntimeWarning"
|
||||||
|
| "StopAsyncIteration"
|
||||||
|
| "StopIteration"
|
||||||
|
| "SyntaxError"
|
||||||
|
| "SyntaxWarning"
|
||||||
|
| "SystemError"
|
||||||
|
| "SystemExit"
|
||||||
|
| "TabError"
|
||||||
|
| "TimeoutError"
|
||||||
|
| "True"
|
||||||
|
| "TypeError"
|
||||||
|
| "UnboundLocalError"
|
||||||
|
| "UnicodeDecodeError"
|
||||||
|
| "UnicodeEncodeError"
|
||||||
|
| "UnicodeError"
|
||||||
|
| "UnicodeTranslateError"
|
||||||
|
| "UnicodeWarning"
|
||||||
|
| "UserWarning"
|
||||||
|
| "ValueError"
|
||||||
|
| "Warning"
|
||||||
|
| "ZeroDivisionError"
|
||||||
|
| "__build_class__"
|
||||||
|
| "__debug__"
|
||||||
|
| "__doc__"
|
||||||
|
| "__import__"
|
||||||
|
| "__loader__"
|
||||||
|
| "__name__"
|
||||||
|
| "__package__"
|
||||||
|
| "__spec__"
|
||||||
|
| "abs"
|
||||||
|
| "aiter"
|
||||||
|
| "all"
|
||||||
|
| "anext"
|
||||||
|
| "any"
|
||||||
|
| "ascii"
|
||||||
|
| "bin"
|
||||||
|
| "bool"
|
||||||
|
| "breakpoint"
|
||||||
|
| "bytearray"
|
||||||
|
| "bytes"
|
||||||
|
| "callable"
|
||||||
|
| "chr"
|
||||||
|
| "classmethod"
|
||||||
|
| "compile"
|
||||||
|
| "complex"
|
||||||
|
| "copyright"
|
||||||
|
| "credits"
|
||||||
|
| "delattr"
|
||||||
|
| "dict"
|
||||||
|
| "dir"
|
||||||
|
| "divmod"
|
||||||
|
| "enumerate"
|
||||||
|
| "eval"
|
||||||
|
| "exec"
|
||||||
|
| "exit"
|
||||||
|
| "filter"
|
||||||
|
| "float"
|
||||||
|
| "format"
|
||||||
|
| "frozenset"
|
||||||
|
| "getattr"
|
||||||
|
| "globals"
|
||||||
|
| "hasattr"
|
||||||
|
| "hash"
|
||||||
|
| "help"
|
||||||
|
| "hex"
|
||||||
|
| "id"
|
||||||
|
| "input"
|
||||||
|
| "int"
|
||||||
|
| "isinstance"
|
||||||
|
| "issubclass"
|
||||||
|
| "iter"
|
||||||
|
| "len"
|
||||||
|
| "license"
|
||||||
|
| "list"
|
||||||
|
| "locals"
|
||||||
|
| "map"
|
||||||
|
| "max"
|
||||||
|
| "memoryview"
|
||||||
|
| "min"
|
||||||
|
| "next"
|
||||||
|
| "object"
|
||||||
|
| "oct"
|
||||||
|
| "open"
|
||||||
|
| "ord"
|
||||||
|
| "pow"
|
||||||
|
| "print"
|
||||||
|
| "property"
|
||||||
|
| "quit"
|
||||||
|
| "range"
|
||||||
|
| "repr"
|
||||||
|
| "reversed"
|
||||||
|
| "round"
|
||||||
|
| "set"
|
||||||
|
| "setattr"
|
||||||
|
| "slice"
|
||||||
|
| "sorted"
|
||||||
|
| "staticmethod"
|
||||||
|
| "str"
|
||||||
|
| "sum"
|
||||||
|
| "super"
|
||||||
|
| "tuple"
|
||||||
|
| "type"
|
||||||
|
| "vars"
|
||||||
|
| "zip"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue