mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-29 07:53:52 +00:00
Remove allocation from ruff_python_stdlib::builtins::python_builtins
(#13317)
This commit is contained in:
parent
2ca78721e6
commit
acab1f4fd8
2 changed files with 45 additions and 25 deletions
|
@ -1951,20 +1951,25 @@ impl<'a> Checker<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bind_builtins(&mut self) {
|
fn bind_builtins(&mut self) {
|
||||||
let standard_builtins = python_builtins(
|
let mut bind_builtin = |builtin| {
|
||||||
self.settings.target_version.minor(),
|
|
||||||
self.source_type.is_ipynb(),
|
|
||||||
);
|
|
||||||
for builtin in standard_builtins
|
|
||||||
.iter()
|
|
||||||
.chain(MAGIC_GLOBALS.iter())
|
|
||||||
.copied()
|
|
||||||
.chain(self.settings.builtins.iter().map(String::as_str))
|
|
||||||
{
|
|
||||||
// Add the builtin to the scope.
|
// Add the builtin to the scope.
|
||||||
let binding_id = self.semantic.push_builtin();
|
let binding_id = self.semantic.push_builtin();
|
||||||
let scope = self.semantic.global_scope_mut();
|
let scope = self.semantic.global_scope_mut();
|
||||||
scope.add(builtin, binding_id);
|
scope.add(builtin, binding_id);
|
||||||
|
};
|
||||||
|
|
||||||
|
let standard_builtins = python_builtins(
|
||||||
|
self.settings.target_version.minor(),
|
||||||
|
self.source_type.is_ipynb(),
|
||||||
|
);
|
||||||
|
for builtin in standard_builtins {
|
||||||
|
bind_builtin(builtin);
|
||||||
|
}
|
||||||
|
for builtin in MAGIC_GLOBALS {
|
||||||
|
bind_builtin(builtin);
|
||||||
|
}
|
||||||
|
for builtin in &self.settings.builtins {
|
||||||
|
bind_builtin(builtin);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -184,21 +184,36 @@ static PY313_PLUS_BUILTINS: &[&str] = &["PythonFinalizationError"];
|
||||||
/// Return the list of builtins for the given Python minor version.
|
/// Return the list of builtins for the given Python minor version.
|
||||||
///
|
///
|
||||||
/// Intended to be kept in sync with [`is_python_builtin`].
|
/// Intended to be kept in sync with [`is_python_builtin`].
|
||||||
pub fn python_builtins(minor_version: u8, is_notebook: bool) -> Vec<&'static str> {
|
pub fn python_builtins(minor_version: u8, is_notebook: bool) -> impl Iterator<Item = &'static str> {
|
||||||
let mut builtins = ALWAYS_AVAILABLE_BUILTINS.to_vec();
|
let py310_builtins = if minor_version >= 10 {
|
||||||
if minor_version >= 10 {
|
Some(PY310_PLUS_BUILTINS)
|
||||||
builtins.extend(PY310_PLUS_BUILTINS);
|
} else {
|
||||||
}
|
None
|
||||||
if minor_version >= 11 {
|
};
|
||||||
builtins.extend(PY311_PLUS_BUILTINS);
|
let py311_builtins = if minor_version >= 11 {
|
||||||
}
|
Some(PY311_PLUS_BUILTINS)
|
||||||
if minor_version >= 13 {
|
} else {
|
||||||
builtins.extend(PY313_PLUS_BUILTINS);
|
None
|
||||||
}
|
};
|
||||||
if is_notebook {
|
let py313_builtins = if minor_version >= 13 {
|
||||||
builtins.extend(IPYTHON_BUILTINS);
|
Some(PY313_PLUS_BUILTINS)
|
||||||
}
|
} else {
|
||||||
builtins
|
None
|
||||||
|
};
|
||||||
|
let ipython_builtins = if is_notebook {
|
||||||
|
Some(IPYTHON_BUILTINS)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
py310_builtins
|
||||||
|
.into_iter()
|
||||||
|
.chain(py311_builtins)
|
||||||
|
.chain(py313_builtins)
|
||||||
|
.chain(ipython_builtins)
|
||||||
|
.flatten()
|
||||||
|
.chain(ALWAYS_AVAILABLE_BUILTINS)
|
||||||
|
.copied()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if the given name is that of a Python builtin.
|
/// Returns `true` if the given name is that of a Python builtin.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue