-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_namespace.py
More file actions
45 lines (28 loc) · 1.41 KB
/
test_namespace.py
File metadata and controls
45 lines (28 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from types import SimpleNamespace
from typing import Protocol, runtime_checkable
import array_api_typing as xpt
@runtime_checkable
class CheckableHasArrayNamespace(xpt.HasArrayNamespace, Protocol):
"""A runtime-checkable version of the HasArrayNamespace protocol."""
# This class is used to ensure that the protocol can be checked at runtime.
# It inherits from xpt.HasArrayNamespace and is marked as runtime_checkable.
class GoodArray:
"""Example class that implements the HasArrayNamespace protocol."""
def __array_namespace__(self) -> object: # noqa: PLW3201
return SimpleNamespace()
class BadArray:
"""Example class that does not implement the HasArrayNamespace protocol."""
def test_has_namespace_class():
"""Test that GoodArray is a subclass of HasArrayNamespace."""
assert issubclass(GoodArray, CheckableHasArrayNamespace)
def test_has_namespace_instance():
"""Test that an instance of GoodArray is recognized as HasArrayNamespace."""
x = GoodArray()
assert isinstance(x, CheckableHasArrayNamespace)
def test_not_has_namespace_class():
"""Test that BadArray is not a subclass of HasArrayNamespace."""
assert not issubclass(BadArray, CheckableHasArrayNamespace)
def test_not_has_namespace_instance():
"""Test that an instance of BadArray is not recognized as HasArrayNamespace."""
y = BadArray()
assert not isinstance(y, CheckableHasArrayNamespace)