Skip to content

Commit f69c2fa

Browse files
authored
Merge pull request numpy#19525 from BvB93/optional
TST: Test that `numpy.typing` can be imported in the absence of typing-extensions
2 parents b797a0c + 1ff314d commit f69c2fa

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

numpy/tests/test_reloading.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,8 @@ def test_full_reimport():
5757
with warns(UserWarning):
5858
import numpy as np
5959
""")
60-
p = subprocess.run([sys.executable, '-c', code])
61-
60+
p = subprocess.run([sys.executable, '-c', code], capture_output=True)
61+
if p.returncode:
62+
raise AssertionError(
63+
f"Non-zero return code: {p.returncode!r}\n\n{p.stderr.decode()}"
64+
)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""Tests for the optional typing-extensions dependency."""
2+
3+
import sys
4+
import textwrap
5+
import subprocess
6+
7+
CODE = textwrap.dedent(r"""
8+
import sys
9+
import importlib
10+
11+
assert "typing_extensions" not in sys.modules
12+
assert "numpy.typing" not in sys.modules
13+
14+
# Importing `typing_extensions` will now raise an `ImportError`
15+
sys.modules["typing_extensions"] = None
16+
assert importlib.import_module("numpy.typing")
17+
""")
18+
19+
20+
def test_no_typing_extensions() -> None:
21+
"""Import `numpy.typing` in the absence of typing-extensions.
22+
23+
Notes
24+
-----
25+
Ideally, we'd just run the normal typing tests in an environment where
26+
typing-extensions is not installed, but unfortunatelly this is currently
27+
impossible as it is an indirect hard dependency of pytest.
28+
29+
"""
30+
p = subprocess.run([sys.executable, '-c', CODE], capture_output=True)
31+
if p.returncode:
32+
raise AssertionError(
33+
f"Non-zero return code: {p.returncode!r}\n\n{p.stderr.decode()}"
34+
)
35+

0 commit comments

Comments
 (0)