summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBas van Beek <b.f.van.beek@vu.nl>2020-11-25 15:27:14 +0100
committerBas van Beek <b.f.van.beek@vu.nl>2020-12-22 17:24:04 +0100
commit92e549fc1c02e86363daf9de5945d2fb5259951c (patch)
treed6cf31f9c98158f4636b473c3d2cc168bd26a2fe
parent8685cf3ad8faa3dc2828be59fa45351456f55aee (diff)
downloadnumpy-92e549fc1c02e86363daf9de5945d2fb5259951c.tar.gz
MAINT: Import `mypy` via a try/except approach
Fixes an issue where the `pypy` tests would fail
-rw-r--r--numpy/typing/mypy_plugin.py38
1 files changed, 23 insertions, 15 deletions
diff --git a/numpy/typing/mypy_plugin.py b/numpy/typing/mypy_plugin.py
index 341870167..505a01a0b 100644
--- a/numpy/typing/mypy_plugin.py
+++ b/numpy/typing/mypy_plugin.py
@@ -4,14 +4,17 @@ import typing as t
import numpy as np
-import mypy.types
-from mypy.types import Type
-from mypy.plugin import Plugin, AnalyzeTypeContext
+try:
+ import mypy.types
+ from mypy.types import Type
+ from mypy.plugin import Plugin, AnalyzeTypeContext
+ _HookFunc = t.Callable[[AnalyzeTypeContext], Type]
+ MYPY_EX: t.Optional[ModuleNotFoundError] = None
+except ModuleNotFoundError as ex:
+ MYPY_EX = ex
__all__: t.List[str] = []
-HookFunc = t.Callable[[AnalyzeTypeContext], Type]
-
def _get_precision_dict() -> t.Dict[str, str]:
names = [
@@ -39,7 +42,7 @@ def _get_precision_dict() -> t.Dict[str, str]:
_PRECISION_DICT = _get_precision_dict()
-def _hook(ctx: AnalyzeTypeContext) -> Type:
+def _hook(ctx: "AnalyzeTypeContext") -> "Type":
"""Replace a type-alias with a concrete ``NBitBase`` subclass."""
typ, _, api = ctx
name = typ.name.split(".")[-1]
@@ -47,15 +50,20 @@ def _hook(ctx: AnalyzeTypeContext) -> Type:
return api.named_type(name_new)
-class _NumpyPlugin(Plugin):
- """A plugin for assigning platform-specific `numpy.number` precisions."""
+if MYPY_EX is None:
+ class _NumpyPlugin(Plugin):
+ """A plugin for assigning platform-specific `numpy.number` precisions."""
- def get_type_analyze_hook(self, fullname: str) -> t.Optional[HookFunc]:
- if fullname in _PRECISION_DICT:
- return _hook
- return None
+ def get_type_analyze_hook(self, fullname: str) -> t.Optional[_HookFunc]:
+ if fullname in _PRECISION_DICT:
+ return _hook
+ return None
+ def plugin(version: str) -> t.Type[_NumpyPlugin]:
+ """An entry-point for mypy."""
+ return _NumpyPlugin
-def plugin(version: str) -> t.Type[_NumpyPlugin]:
- """An entry-point for mypy."""
- return _NumpyPlugin
+else:
+ def plugin(version: str) -> t.Type["_NumpyPlugin"]:
+ """An entry-point for mypy."""
+ raise MYPY_EX