fix: tell a Windmill client from the wmill module by its constructor

This commit is contained in:
Guilhem Lemouel
2026-12-01 04:52:56 -08:00
parent 09967faa53
commit af51b20d37
2 changed files with 45 additions and 15 deletions
@@ -901,6 +901,7 @@ def main():
use windmill_parser::item_reference::{ItemKind::*, ItemReference, ReferenceAccess::*};
let input = r#"
import wmill
import wmill.client as sdk
from wmill import get_variable as secret, task_script
def main():
@@ -910,6 +911,9 @@ def main():
wmill.Windmill().set_resource({"a": 1}, "f/ops/client_state")
wmill.Windmill().set_resource("f/ops/value", path="f/ops/named_state", resource_type="any")
wmill.Windmill().set_resource("f/ops/value", "f/ops/positional_state", resource_type="any")
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", {})
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")
@@ -920,8 +924,10 @@ def main():
assert_eq!(
parse_assets(input).unwrap().references,
vec![
reference(Resource, "f/ops/aliased_module_state", Write),
reference(Resource, "f/ops/bucket", Read),
reference(Resource, "f/ops/client_state", Write),
reference(Resource, "f/ops/client_var_state", Write),
reference(Resource, "f/ops/named_state", Write),
reference(Resource, "f/ops/positional_state", Write),
reference(Resource, "f/ops/state", Write),
@@ -124,7 +124,7 @@ fn sdk_call(name: &str) -> Option<&'static SdkCall> {
pub(crate) fn find_references(suite: &[Stmt]) -> Vec<ItemReference> {
let mut finder = ReferencesFinder {
renamed: HashMap::new(),
modules: HashSet::from(["wmill".to_string()]),
clients: HashSet::new(),
found: BTreeSet::new(),
};
finder.visit_body(suite);
@@ -134,17 +134,19 @@ pub(crate) fn find_references(suite: &[Stmt]) -> Vec<ItemReference> {
struct ReferencesFinder {
/// Local names bound by `from wmill import get_variable as gv`.
renamed: HashMap<String, &'static SdkCall>,
/// Local names of the `wmill` module: itself, and `import wmill as w`.
modules: HashSet<String>,
/// Names assigned a `Windmill(...)` client: `client = wmill.Windmill()`.
clients: HashSet<String>,
found: BTreeSet<ItemReference>,
}
impl<'a> Visitor<'a> for ReferencesFinder {
fn visit_stmt(&mut self, stmt: &'a Stmt) {
if let Stmt::Import(node) = stmt {
for alias in node.names.iter().filter(|a| a.name.as_str() == "wmill") {
if let Some(local) = &alias.asname {
self.modules.insert(local.to_string());
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());
}
}
}
}
@@ -177,11 +179,11 @@ impl ReferencesFinder {
.as_name_expr()
.map(|n| n.id.as_str())
.or_else(|| node.func.as_attribute_expr().map(|a| a.attr.as_str()));
// A method on anything but the module (`Windmill()`, a client variable) is a client's.
let on_client = node.func.as_attribute_expr().is_some_and(|a| {
a.value
.as_name_expr()
.is_none_or(|n| !self.modules.contains(n.id.as_str()))
is_client(&a.value)
|| a.value
.as_name_expr()
.is_some_and(|n| self.clients.contains(n.id.as_str()))
});
let Some(call) = name.and_then(|n| self.renamed.get(n).copied().or_else(|| sdk_call(n)))
else {
@@ -196,10 +198,19 @@ impl ReferencesFinder {
// the value in `set_resource(value, path="…")`.
let candidates: Vec<&Expr> = match keyword {
Some(k) => vec![&k.value],
None => (if on_client { call.client_args } else { call.args })
.iter()
.filter_map(|i| node.arguments.args.get(*i))
.collect(),
// The other order is tried next, for a receiver not recognized as what it is.
None => {
let (first, then) = if on_client {
(call.client_args, call.args)
} else {
(call.args, call.client_args)
};
first
.iter()
.chain(then)
.filter_map(|i| node.arguments.args.get(*i))
.collect()
}
};
for candidate in candidates {
let Expr::StringLiteral(s) = candidate else {
@@ -212,3 +223,16 @@ 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")
}