Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ruff_python_semantic::all::extract_all_names() #11335

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import builtins
a = 1

__all__ = builtins.list(["a", "b"])
3 changes: 1 addition & 2 deletions crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1882,8 +1882,7 @@ impl<'a> Checker<'a> {
_ => false,
}
{
let (all_names, all_flags) =
extract_all_names(parent, |name| self.semantic.has_builtin_binding(name));
let (all_names, all_flags) = extract_all_names(parent, &self.semantic);

if all_flags.intersects(DunderAllFlags::INVALID_OBJECT) {
flags |= BindingFlags::INVALID_ALL_OBJECT;
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_linter/src/rules/pyflakes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ mod tests {
#[test_case(Rule::UndefinedExport, Path::new("F822_0.py"))]
#[test_case(Rule::UndefinedExport, Path::new("F822_0.pyi"))]
#[test_case(Rule::UndefinedExport, Path::new("F822_1.py"))]
#[test_case(Rule::UndefinedExport, Path::new("F822_1b.py"))]
#[test_case(Rule::UndefinedExport, Path::new("F822_2.py"))]
#[test_case(Rule::UndefinedExport, Path::new("F822_3.py"))]
#[test_case(Rule::UndefinedLocal, Path::new("F823.py"))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F822_1b.py:4:31: F822 Undefined name `b` in `__all__`
|
2 | a = 1
3 |
4 | __all__ = builtins.list(["a", "b"])
| ^^^ F822
|
60 changes: 31 additions & 29 deletions crates/ruff_python_semantic/src/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use bitflags::bitflags;
use ruff_python_ast::{self as ast, helpers::map_subscript, Expr, Stmt};
use ruff_text_size::{Ranged, TextRange};

use crate::SemanticModel;

bitflags! {
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct DunderAllFlags: u8 {
Expand Down Expand Up @@ -69,10 +71,10 @@ impl Ranged for DunderAllDefinition<'_> {
/// Extract the names bound to a given __all__ assignment.
///
/// Accepts a closure that determines whether a given name (e.g., `"list"`) is a Python builtin.
pub fn extract_all_names<F>(stmt: &Stmt, is_builtin: F) -> (Vec<DunderAllName>, DunderAllFlags)
where
F: Fn(&str) -> bool,
{
pub fn extract_all_names<'a>(
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved
stmt: &'a Stmt,
semantic: &SemanticModel,
) -> (Vec<DunderAllName<'a>>, DunderAllFlags) {
fn add_to_names<'a>(
elts: &'a [Expr],
names: &mut Vec<DunderAllName<'a>>,
Expand All @@ -90,10 +92,10 @@ where
}
}

fn extract_elts<F>(expr: &Expr, is_builtin: F) -> (Option<&[Expr]>, DunderAllFlags)
where
F: Fn(&str) -> bool,
{
fn extract_elts<'a>(
expr: &'a Expr,
semantic: &SemanticModel,
) -> (Option<&'a [Expr]>, DunderAllFlags) {
match expr {
Expr::List(ast::ExprList { elts, .. }) => {
return (Some(elts), DunderAllFlags::empty());
Expand Down Expand Up @@ -122,32 +124,32 @@ where
}) => {
// Allow `tuple()`, `list()`, and their generic forms, like `list[int]()`.
if arguments.keywords.is_empty() && arguments.args.len() <= 1 {
if let Expr::Name(ast::ExprName { id, .. }) = map_subscript(func) {
let id = id.as_str();
if matches!(id, "tuple" | "list") && is_builtin(id) {
let [arg] = arguments.args.as_ref() else {
if semantic
.resolve_builtin_symbol(map_subscript(func))
.is_some_and(|symbol| matches!(symbol, "tuple" | "list"))
{
let [arg] = arguments.args.as_ref() else {
return (None, DunderAllFlags::empty());
};
match arg {
Expr::List(ast::ExprList { elts, .. })
| Expr::Set(ast::ExprSet { elts, .. })
| Expr::Tuple(ast::ExprTuple { elts, .. }) => {
return (Some(elts), DunderAllFlags::empty());
}
_ => {
// We can't analyze other expressions, but they must be
// valid, since the `list` or `tuple` call will ultimately
// evaluate to a list or tuple.
return (None, DunderAllFlags::empty());
};
match arg {
Expr::List(ast::ExprList { elts, .. })
| Expr::Set(ast::ExprSet { elts, .. })
| Expr::Tuple(ast::ExprTuple { elts, .. }) => {
return (Some(elts), DunderAllFlags::empty());
}
_ => {
// We can't analyze other expressions, but they must be
// valid, since the `list` or `tuple` call will ultimately
// evaluate to a list or tuple.
return (None, DunderAllFlags::empty());
}
}
}
}
}
}
Expr::Named(ast::ExprNamed { value, .. }) => {
// Allow, e.g., `__all__ += (value := ["A", "B"])`.
return extract_elts(value, is_builtin);
return extract_elts(value, semantic);
}
_ => {}
}
Expand All @@ -168,7 +170,7 @@ where
let mut current_right = right;
loop {
// Process the right side, which should be a "real" value.
let (elts, new_flags) = extract_elts(current_right, |expr| is_builtin(expr));
let (elts, new_flags) = extract_elts(current_right, semantic);
flags |= new_flags;
if let Some(elts) = elts {
add_to_names(elts, &mut names, &mut flags);
Expand All @@ -180,7 +182,7 @@ where
current_left = left;
current_right = right;
} else {
let (elts, new_flags) = extract_elts(current_left, |expr| is_builtin(expr));
let (elts, new_flags) = extract_elts(current_left, semantic);
flags |= new_flags;
if let Some(elts) = elts {
add_to_names(elts, &mut names, &mut flags);
Expand All @@ -189,7 +191,7 @@ where
}
}
} else {
let (elts, new_flags) = extract_elts(value, |expr| is_builtin(expr));
let (elts, new_flags) = extract_elts(value, semantic);
flags |= new_flags;
if let Some(elts) = elts {
add_to_names(elts, &mut names, &mut flags);
Expand Down
Loading