use ruff_python_ast::Expr;
use ruff_python_semantic::{BindingKind, Imported, SemanticModel};
#[derive(Debug)]
pub(super) enum Resolution {
IrrelevantExpression,
IrrelevantBinding,
RelevantLocal,
PandasModule,
}
pub(super) fn test_expression(expr: &Expr, semantic: &SemanticModel) -> Resolution {
match expr {
Expr::StringLiteral(_)
| Expr::BytesLiteral(_)
| Expr::NumberLiteral(_)
| Expr::BooleanLiteral(_)
| Expr::NoneLiteral(_)
| Expr::EllipsisLiteral(_)
| Expr::Tuple(_)
| Expr::List(_)
| Expr::Set(_)
| Expr::Dict(_)
| Expr::SetComp(_)
| Expr::ListComp(_)
| Expr::DictComp(_)
| Expr::Generator(_) => Resolution::IrrelevantExpression,
Expr::Name(name) => {
semantic
.resolve_name(name)
.map_or(Resolution::IrrelevantBinding, |id| {
match &semantic.binding(id).kind {
BindingKind::Argument => {
if matches!(name.id.as_str(), "self" | "cls") {
Resolution::IrrelevantBinding
} else {
Resolution::RelevantLocal
}
}
BindingKind::Annotation
| BindingKind::Assignment
| BindingKind::NamedExprAssignment
| BindingKind::LoopVar
| BindingKind::Global(_)
| BindingKind::Nonlocal(_, _) => Resolution::RelevantLocal,
BindingKind::Import(import)
if matches!(import.qualified_name().segments(), ["pandas"]) =>
{
Resolution::PandasModule
}
_ => Resolution::IrrelevantBinding,
}
})
}
_ => Resolution::RelevantLocal,
}
}