-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19533 from BvB93/optional2
BUG: Fix an issue wherein importing `numpy.typing` could raise
- Loading branch information
Showing
3 changed files
with
48 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""Tests for the optional typing-extensions dependency.""" | ||
|
||
import sys | ||
import textwrap | ||
import subprocess | ||
|
||
CODE = textwrap.dedent(r""" | ||
import sys | ||
import importlib | ||
assert "typing_extensions" not in sys.modules | ||
assert "numpy.typing" not in sys.modules | ||
# Importing `typing_extensions` will now raise an `ImportError` | ||
sys.modules["typing_extensions"] = None | ||
assert importlib.import_module("numpy.typing") | ||
""") | ||
|
||
|
||
def test_no_typing_extensions() -> None: | ||
"""Import `numpy.typing` in the absence of typing-extensions. | ||
Notes | ||
----- | ||
Ideally, we'd just run the normal typing tests in an environment where | ||
typing-extensions is not installed, but unfortunatelly this is currently | ||
impossible as it is an indirect hard dependency of pytest. | ||
""" | ||
p = subprocess.run([sys.executable, '-c', CODE], capture_output=True) | ||
if p.returncode: | ||
raise AssertionError( | ||
f"Non-zero return code: {p.returncode!r}\n\n{p.stderr.decode()}" | ||
) | ||
|