fix: recognize annotated and aliased Windmill clients in the python reference scan

This commit is contained in:
Guilhem Lemouel
2026-12-01 05:01:27 -08:00
parent af51b20d37
commit fff2e3e92d
2 changed files with 41 additions and 19 deletions
@@ -902,6 +902,7 @@ def main():
let input = r#"
import wmill
import wmill.client as sdk
from wmill import Windmill as Client
from wmill import get_variable as secret, task_script
def main():
@@ -914,6 +915,9 @@ def main():
client = wmill.Windmill()
client.set_resource("f/ops/other_value", "f/ops/client_var_state", resource_type="any")
sdk.set_resource("f/ops/aliased_module_state", {})
typed: wmill.Windmill = wmill.Windmill()
typed.set_resource("f/ops/v", "f/ops/typed_client_state")
Client().set_resource("f/ops/v", "f/ops/aliased_client_state")
wmill.load_s3_file("s3:///a.csv", s3_resource_path="f/ops/bucket")
wmill.run_script_async("8e5d6c4f1a2b3c4d")
wmill.run_flow_async(path="f/ops/sync")
@@ -924,6 +928,7 @@ def main():
assert_eq!(
parse_assets(input).unwrap().references,
vec![
reference(Resource, "f/ops/aliased_client_state", Write),
reference(Resource, "f/ops/aliased_module_state", Write),
reference(Resource, "f/ops/bucket", Read),
reference(Resource, "f/ops/client_state", Write),
@@ -931,6 +936,7 @@ def main():
reference(Resource, "f/ops/named_state", Write),
reference(Resource, "f/ops/positional_state", Write),
reference(Resource, "f/ops/state", Write),
reference(Resource, "f/ops/typed_client_state", Write),
reference(Variable, "f/ops/api_key", Read),
reference(Script, "f/ops/step", Task),
reference(Flow, "f/ops/sync", Run),
@@ -125,6 +125,7 @@ pub(crate) fn find_references(suite: &[Stmt]) -> Vec<ItemReference> {
let mut finder = ReferencesFinder {
renamed: HashMap::new(),
clients: HashSet::new(),
constructors: HashSet::from(["Windmill".to_string()]),
found: BTreeSet::new(),
};
finder.visit_body(suite);
@@ -136,22 +137,32 @@ struct ReferencesFinder {
renamed: HashMap<String, &'static SdkCall>,
/// Names assigned a `Windmill(...)` client: `client = wmill.Windmill()`.
clients: HashSet<String>,
/// Names the `Windmill` class is called by: itself, and `from wmill import Windmill as C`.
constructors: HashSet<String>,
found: BTreeSet<ItemReference>,
}
impl<'a> Visitor<'a> for ReferencesFinder {
fn visit_stmt(&mut self, stmt: &'a Stmt) {
if let Stmt::Assign(node) = stmt {
if is_client(&node.value) {
for target in &node.targets {
if let Some(name) = target.as_name_expr() {
self.clients.insert(name.id.to_string());
}
}
let (targets, is_client): (Vec<&Expr>, bool) = match stmt {
Stmt::Assign(node) => (node.targets.iter().collect(), self.is_client(&node.value)),
Stmt::AnnAssign(node) => (
vec![&node.target],
self.names_constructor(&node.annotation)
|| node.value.as_deref().is_some_and(|v| self.is_client(v)),
),
_ => (vec![], false),
};
if is_client {
for name in targets.into_iter().filter_map(Expr::as_name_expr) {
self.clients.insert(name.id.to_string());
}
}
if let Stmt::ImportFrom(node) = stmt {
for alias in &node.names {
if let (Some(local), "Windmill") = (&alias.asname, alias.name.as_str()) {
self.constructors.insert(local.to_string());
}
if let (Some(local), Some(call)) = (&alias.asname, sdk_call(alias.name.as_str())) {
self.renamed.insert(local.to_string(), call);
}
@@ -180,7 +191,7 @@ impl ReferencesFinder {
.map(|n| n.id.as_str())
.or_else(|| node.func.as_attribute_expr().map(|a| a.attr.as_str()));
let on_client = node.func.as_attribute_expr().is_some_and(|a| {
is_client(&a.value)
self.is_client(&a.value)
|| a.value
.as_name_expr()
.is_some_and(|n| self.clients.contains(n.id.as_str()))
@@ -224,15 +235,20 @@ impl ReferencesFinder {
}
}
/// `Windmill(...)` or `wmill.Windmill(...)`: a client, whose methods take the value first.
fn is_client(expr: &Expr) -> bool {
let Some(call) = expr.as_call_expr() else {
return false;
};
let name = call
.func
.as_name_expr()
.map(|n| n.id.as_str())
.or_else(|| call.func.as_attribute_expr().map(|a| a.attr.as_str()));
name == Some("Windmill")
impl ReferencesFinder {
/// `Windmill(...)`, `wmill.Windmill(...)` or an alias's call: a client, whose methods take
/// the value first.
fn is_client(&self, expr: &Expr) -> bool {
expr.as_call_expr()
.is_some_and(|call| self.names_constructor(&call.func))
}
/// `Windmill`, `wmill.Windmill` or an alias of it, as called or as an annotation.
fn names_constructor(&self, expr: &Expr) -> bool {
match expr {
Expr::Name(n) => self.constructors.contains(n.id.as_str()),
Expr::Attribute(a) => a.attr.as_str() == "Windmill",
_ => false,
}
}
}