Skip to content

Commit cad8ca5

Browse files
ctruedenclaude
andcommitted
Improve the numpy test/warning
The Pythonic way to see if something has been imported is to test whether it is in sys.modules. And add tests to validate that the numpy warning gets issued or not depending on the state of the environment. We use the new source_override() function, since these new tests aim to test the python_worker behavior as it evolves. Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent fb4331b commit cad8ca5

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

src/appose/python_worker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def main() -> None:
290290
numpy_installed = any(
291291
dist.metadata["Name"] == "numpy" for dist in distributions()
292292
)
293-
if numpy_installed and "numpy" not in globals():
293+
if numpy_installed and "numpy" not in sys.modules:
294294
print(
295295
"[WARNING] This environment includes numpy, but numpy was not imported via a service init script.",
296296
file=sys.stderr,

tests/test_python_worker.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Appose: multi-language interprocess cooperation with shared memory.
2+
# Copyright (C) 2023 - 2026 Appose developers.
3+
# SPDX-License-Identifier: BSD-2-Clause
4+
5+
6+
from pathlib import Path
7+
8+
9+
import appose
10+
11+
from tests.test_base import source_override
12+
13+
14+
def _numpy_env():
15+
return (
16+
appose.uv()
17+
.include("numpy")
18+
.env(**source_override())
19+
.base("target/envs/pyworker-numpy")
20+
.log_debug()
21+
.build()
22+
)
23+
24+
25+
def test_numpy_warning():
26+
"""Tests that a warning is issued when numpy is installed but not imported via init."""
27+
env = _numpy_env()
28+
with env.python() as service:
29+
service.task("pass").wait_for()
30+
error_lines = service.error_lines()
31+
assert any("[WARNING]" in line and "numpy" in line for line in error_lines)
32+
33+
34+
def test_numpy_no_warning():
35+
"""Tests that no warning is issued when numpy is imported via the init script."""
36+
env = _numpy_env()
37+
with env.python().init("import numpy") as service:
38+
service.task("pass").wait_for()
39+
error_lines = service.error_lines()
40+
assert not any("[WARNING]" in line and "numpy" in line for line in error_lines)
41+
42+
43+
def test_no_warnings():
44+
"""Tests that no warning is issued in numpy-free environments."""
45+
env = appose.system()
46+
with env.python() as service:
47+
service.start()
48+
error_lines = service.error_lines()
49+
assert not any("[WARNING]" in line for line in error_lines)

0 commit comments

Comments
 (0)