Skip to content

Commit ee98558

Browse files
BvB93seberg
andcommitted
TST: Remodel the typing-extensions test after test_full_reimport
Co-Authored-By: Sebastian Berg <[email protected]>
1 parent c467310 commit ee98558

File tree

1 file changed

+17
-38
lines changed

1 file changed

+17
-38
lines changed
Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,20 @@
11
"""Tests for the optional typing-extensions dependency."""
22

33
import sys
4-
import types
5-
import inspect
6-
import importlib
4+
import textwrap
5+
import subprocess
76

8-
import typing_extensions
9-
import numpy.typing as npt
7+
CODE = textwrap.dedent(r"""
8+
import sys
9+
import importlib
1010
11+
assert "typing_extensions" not in sys.modules
12+
assert "numpy.typing" not in sys.modules
1113
12-
def _is_sub_module(obj: object) -> bool:
13-
"""Check if `obj` is a `numpy.typing` submodule."""
14-
return inspect.ismodule(obj) and obj.__name__.startswith("numpy.typing")
15-
16-
17-
def _is_dunder(name: str) -> bool:
18-
"""Check whether `name` is a dunder."""
19-
return name.startswith("__") and name.endswith("__")
20-
21-
22-
def _clear_attr(module: types.ModuleType) -> None:
23-
"""Clear all (non-dunder) module-level attributes."""
24-
del_names = [name for name in vars(module) if not _is_dunder(name)]
25-
for name in del_names:
26-
delattr(module, name)
27-
28-
29-
MODULES = {"numpy.typing": npt}
30-
MODULES.update({
31-
f"numpy.typing.{k}": v for k, v in vars(npt).items() if _is_sub_module(v)
32-
})
14+
# Importing `typing_extensions` will now raise an `ImportError`
15+
sys.modules["typing_extensions"] = None
16+
assert importlib.import_module("numpy.typing")
17+
""")
3318

3419

3520
def test_no_typing_extensions() -> None:
@@ -42,15 +27,9 @@ def test_no_typing_extensions() -> None:
4227
impossible as it is an indirect hard dependency of pytest.
4328
4429
"""
45-
assert "typing_extensions" in sys.modules
46-
47-
try:
48-
sys.modules["typing_extensions"] = None
49-
for name, module in MODULES.items():
50-
_clear_attr(module)
51-
assert importlib.reload(module), name
52-
finally:
53-
sys.modules["typing_extensions"] = typing_extensions
54-
for module in MODULES.values():
55-
_clear_attr(module)
56-
importlib.reload(module)
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)