mirror of https://github.com/astral-sh/ruff
Support builtins
This commit is contained in:
parent
e9a3484edf
commit
fff5dd98bc
|
|
@ -12,3 +12,5 @@ def get_name():
|
||||||
|
|
||||||
def get_name(self):
|
def get_name(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
x = list()
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,163 @@
|
||||||
|
pub const BUILTINS: &[&str] = &[
|
||||||
|
"ArithmeticError",
|
||||||
|
"AssertionError",
|
||||||
|
"AttributeError",
|
||||||
|
"BaseException",
|
||||||
|
"BlockingIOError",
|
||||||
|
"BrokenPipeError",
|
||||||
|
"BufferError",
|
||||||
|
"BytesWarning",
|
||||||
|
"ChildProcessError",
|
||||||
|
"ConnectionAbortedError",
|
||||||
|
"ConnectionError",
|
||||||
|
"ConnectionRefusedError",
|
||||||
|
"ConnectionResetError",
|
||||||
|
"DeprecationWarning",
|
||||||
|
"EOFError",
|
||||||
|
"Ellipsis",
|
||||||
|
"EnvironmentError",
|
||||||
|
"Exception",
|
||||||
|
"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",
|
||||||
|
"all",
|
||||||
|
"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",
|
||||||
|
];
|
||||||
|
|
||||||
|
// Globally defined names which are not attributes of the builtins module, or are only present on
|
||||||
|
// some platforms.
|
||||||
|
pub const MAGIC_GLOBALS: &[&str] = &[
|
||||||
|
"__file__",
|
||||||
|
"__builtins__",
|
||||||
|
"__annotations__",
|
||||||
|
"WindowsError",
|
||||||
|
];
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
use std::collections::{BTreeMap, BTreeSet};
|
use std::collections::{BTreeMap, BTreeSet};
|
||||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
|
||||||
|
use crate::builtins::{BUILTINS, MAGIC_GLOBALS};
|
||||||
use rustpython_parser::ast::{
|
use rustpython_parser::ast::{
|
||||||
Arg, Arguments, Constant, Expr, ExprContext, ExprKind, Location, Stmt, StmtKind, Suite,
|
Arg, Arguments, Constant, Expr, ExprContext, ExprKind, Location, Stmt, StmtKind, Suite,
|
||||||
};
|
};
|
||||||
|
|
@ -45,6 +46,7 @@ enum BindingKind {
|
||||||
Assignment,
|
Assignment,
|
||||||
ClassDefinition,
|
ClassDefinition,
|
||||||
Definition,
|
Definition,
|
||||||
|
Builtin,
|
||||||
FutureImportation,
|
FutureImportation,
|
||||||
Importation(String),
|
Importation(String),
|
||||||
StarImportation,
|
StarImportation,
|
||||||
|
|
@ -408,6 +410,25 @@ impl Checker<'_> {
|
||||||
.push(self.scopes.pop().expect("Attempted to pop without scope."));
|
.push(self.scopes.pop().expect("Attempted to pop without scope."));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn bind_builtins(&mut self) {
|
||||||
|
for builtin in BUILTINS {
|
||||||
|
self.add_binding(Binding {
|
||||||
|
kind: BindingKind::Builtin,
|
||||||
|
name: builtin.to_string(),
|
||||||
|
location: Default::default(),
|
||||||
|
used: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
for builtin in MAGIC_GLOBALS {
|
||||||
|
self.add_binding(Binding {
|
||||||
|
kind: BindingKind::Builtin,
|
||||||
|
name: builtin.to_string(),
|
||||||
|
location: Default::default(),
|
||||||
|
used: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn add_binding(&mut self, binding: Binding) {
|
fn add_binding(&mut self, binding: Binding) {
|
||||||
let scope = self.scopes.last_mut().expect("No current scope found.");
|
let scope = self.scopes.last_mut().expect("No current scope found.");
|
||||||
|
|
||||||
|
|
@ -532,6 +553,7 @@ impl Checker<'_> {
|
||||||
pub fn check_ast(python_ast: &Suite, settings: &Settings, path: &str) -> Vec<Check> {
|
pub fn check_ast(python_ast: &Suite, settings: &Settings, path: &str) -> Vec<Check> {
|
||||||
let mut checker = Checker::new(settings);
|
let mut checker = Checker::new(settings);
|
||||||
checker.push_scope(Scope::new(Module));
|
checker.push_scope(Scope::new(Module));
|
||||||
|
checker.bind_builtins();
|
||||||
|
|
||||||
for stmt in python_ast {
|
for stmt in python_ast {
|
||||||
checker.visit_stmt(stmt);
|
checker.visit_stmt(stmt);
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
mod builtins;
|
||||||
mod cache;
|
mod cache;
|
||||||
pub mod check_ast;
|
pub mod check_ast;
|
||||||
mod check_lines;
|
mod check_lines;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue