Skip to content

Commit 3318b97

Browse files
gselzerclaude
andcommitted
Import header modules under private aliases
The header imported numpy and appose.python_worker under their own names, binding them for the script: a script using numpy without importing it got no diagnostic, and one importing it as np was offered numpy all the same. Import them as private aliases instead, hidden from completion. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2682684 commit 3318b97

2 files changed

Lines changed: 28 additions & 17 deletions

File tree

‎src/main/java/org/scijava/plugins/scripting/appose/python/completion/ApposePythonDialect.java‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,13 @@ public class ApposePythonDialect implements ScriptDialect {
6060
/** The type of images, as Appose passes them. */
6161
private static final String NDARRAY = "numpy.ndarray";
6262

63-
/** The type of the {@code task} variable scripts can use. */
64-
private static final String TASK_TYPE = "appose.python_worker.Task";
63+
/**
64+
* The names the header imports modules as: private, so that the header
65+
* binds no name a script might use itself (e.g. {@code numpy}, which a
66+
* script that does not import it cannot use).
67+
*/
68+
private static final String NUMPY = "_scijava_numpy",
69+
WORKER = "_scijava_python_worker";
6570

6671
@Parameter(required = false)
6772
private ResidentWorkerService workers;
@@ -90,26 +95,28 @@ public Header header(final Map<String, Variable> variables) {
9095
final List<String> code = new ArrayList<>();
9196
final List<String> hidden = new ArrayList<>();
9297
// Each Appose-Python script has access to its task
93-
code.add("import appose.python_worker");
94-
hidden.add("appose");
98+
code.add("import appose.python_worker as " + WORKER);
99+
hidden.add(WORKER);
95100
// And numpy is imported for the server whenever an image parameter is
96101
// provided (the script itself does not get it)
97102
if (variables.values().stream().map(v -> parameterType(v.type()))
98103
.anyMatch(NDARRAY::equals))
99104
{
100-
code.add("import numpy");
101-
hidden.add("numpy");
105+
code.add("import numpy as " + NUMPY);
106+
hidden.add(NUMPY);
102107
}
103108
// Declare the types of each parameter for type reasoning. NB: Assigned
104109
// (to ...), as well as annotated: some servers (e.g. ty) take a name that
105110
// is only annotated to be unbound, and complete none of its members.
106111
variables.forEach((name, v) -> {
107112
final String t = parameterType(v.type());
108113
// NB: Values Appose cannot pass are not declared.
109-
if (t != null) code.add(name + ": " + t + " = ...");
114+
if (t == null) return;
115+
final String declared = NDARRAY.equals(t) ? NUMPY + ".ndarray" : t;
116+
code.add(name + ": " + declared + " = ...");
110117
});
111118
// Declare the type of task for type reasoning
112-
code.add("task: " + TASK_TYPE + " = ...");
119+
code.add("task: " + WORKER + ".Task = ...");
113120
// Combine into a Header
114121
return new Header(String.join("; ", code), hidden);
115122
}

‎src/test/java/org/scijava/plugins/scripting/appose/python/completion/ApposePythonDialectTest.java‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,22 @@ public void testHeader() {
117117
parameters.put("sigma", Variable.ofType(double.class));
118118
parameters.put("context", Variable.ofType(Context.class));
119119
Header header = dialect.header(parameters);
120-
assertEquals("import appose.python_worker; import numpy; " +
121-
"image: numpy.ndarray = ...; sigma: float = ...; " +
122-
"task: appose.python_worker.Task = ...", header.code());
123-
// The imports' names are the header's alone
124-
assertEquals(Arrays.asList("appose", "numpy"), Arrays.asList(header
125-
.hiddenNames().toArray()));
120+
assertEquals("import appose.python_worker as _scijava_python_worker; " +
121+
"import numpy as _scijava_numpy; " +
122+
"image: _scijava_numpy.ndarray = ...; sigma: float = ...; " +
123+
"task: _scijava_python_worker.Task = ...", header.code());
124+
// The imports' names are the header's alone: private aliases, binding
125+
// no name (e.g. numpy) a script might use itself
126+
assertEquals(Arrays.asList("_scijava_python_worker", "_scijava_numpy"),
127+
Arrays.asList(header.hiddenNames().toArray()));
126128
// numpy is imported only when a parameter needs it
127129
header = dialect.header(Collections.singletonMap("sigma", Variable
128130
.ofType(double.class)));
129-
assertEquals("import appose.python_worker; sigma: float = ...; " +
130-
"task: appose.python_worker.Task = ...", header.code());
131-
assertEquals(Collections.singleton("appose"), header.hiddenNames());
131+
assertEquals("import appose.python_worker as _scijava_python_worker; " +
132+
"sigma: float = ...; task: _scijava_python_worker.Task = ...", header
133+
.code());
134+
assertEquals(Collections.singleton("_scijava_python_worker"), header
135+
.hiddenNames());
132136
}
133137

134138
@Test

0 commit comments

Comments
 (0)