summaryrefslogtreecommitdiff
path: root/numpy/typing
diff options
context:
space:
mode:
authorBvB93 <43369155+BvB93@users.noreply.github.com>2022-09-30 16:38:19 +0200
committerBvB93 <43369155+BvB93@users.noreply.github.com>2022-09-30 17:09:51 +0200
commit51c9aa8e35c8200ca8694fc62da1837cdc1cbbff (patch)
tree53ec1219f2f4cd36d4f7cc8029bafe020ec93e81 /numpy/typing
parent17051caef30ad3b99291077c95f5ca9e55f0e082 (diff)
downloadnumpy-51c9aa8e35c8200ca8694fc62da1837cdc1cbbff.tar.gz
TYP,ENH: Mark `numpy.typing` protocols as runtime checkable
Diffstat (limited to 'numpy/typing')
-rw-r--r--numpy/typing/tests/test_runtime.py33
1 files changed, 32 insertions, 1 deletions
diff --git a/numpy/typing/tests/test_runtime.py b/numpy/typing/tests/test_runtime.py
index 5b5df49dc..44d069006 100644
--- a/numpy/typing/tests/test_runtime.py
+++ b/numpy/typing/tests/test_runtime.py
@@ -3,11 +3,19 @@
from __future__ import annotations
import sys
-from typing import get_type_hints, Union, NamedTuple, get_args, get_origin
+from typing import (
+ get_type_hints,
+ Union,
+ NamedTuple,
+ get_args,
+ get_origin,
+ Any,
+)
import pytest
import numpy as np
import numpy.typing as npt
+import numpy._typing as _npt
class TypeTup(NamedTuple):
@@ -80,3 +88,26 @@ def test_keys() -> None:
keys = TYPES.keys()
ref = set(npt.__all__)
assert keys == ref
+
+
+PROTOCOLS: dict[str, tuple[type[Any], object]] = {
+ "_SupportsDType": (_npt._SupportsDType, np.int64(1)),
+ "_SupportsArray": (_npt._SupportsArray, np.arange(10)),
+ "_SupportsArrayFunc": (_npt._SupportsArrayFunc, np.arange(10)),
+ "_NestedSequence": (_npt._NestedSequence, [1]),
+}
+
+
+@pytest.mark.parametrize("cls,obj", PROTOCOLS.values(), ids=PROTOCOLS.keys())
+class TestRuntimeProtocol:
+ def test_isinstance(self, cls: type[Any], obj: object) -> None:
+ assert isinstance(obj, cls)
+ assert not isinstance(None, cls)
+
+ def test_issubclass(self, cls: type[Any], obj: object) -> None:
+ if cls is _npt._SupportsDType:
+ pytest.xfail(
+ "Protocols with non-method members don't support issubclass()"
+ )
+ assert issubclass(type(obj), cls)
+ assert not issubclass(type(None), cls)