|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 | 3 | import copy
|
| 4 | +import sys |
4 | 5 | from abc import abstractmethod
|
5 | 6 | from collections.abc import Mapping
|
6 | 7 | from typing import TYPE_CHECKING, Any, Generic, cast, overload
|
@@ -86,7 +87,29 @@ def check_duck_array_typevar(a: duckarray[Any, _DType]) -> duckarray[Any, _DType
|
86 | 87 | if isinstance(b, _arrayfunction_or_api):
|
87 | 88 | return b
|
88 | 89 | else:
|
89 |
| - raise TypeError(f"a ({type(a)}) is not a valid _arrayfunction or _arrayapi") |
| 90 | + missing_attrs = "" |
| 91 | + actual_attrs = set(dir(b)) |
| 92 | + for t in _arrayfunction_or_api: |
| 93 | + if sys.version_info >= (3, 13): |
| 94 | + # https://github.com/python/cpython/issues/104873 |
| 95 | + from typing import get_protocol_members |
| 96 | + |
| 97 | + expected_attrs = get_protocol_members(t) |
| 98 | + elif sys.version_info >= (3, 12): |
| 99 | + expected_attrs = t.__protocol_attrs__ |
| 100 | + else: |
| 101 | + from typing import _get_protocol_attrs # type: ignore[attr-defined] |
| 102 | + |
| 103 | + expected_attrs = _get_protocol_attrs(t) |
| 104 | + |
| 105 | + missing_attrs_ = expected_attrs - actual_attrs |
| 106 | + if missing_attrs_: |
| 107 | + missing_attrs += f"{t.__name__} - {missing_attrs_}\n" |
| 108 | + raise TypeError( |
| 109 | + f"a ({type(a)}) is not a valid _arrayfunction or _arrayapi. " |
| 110 | + "Missing following attrs:\n" |
| 111 | + f"{missing_attrs}" |
| 112 | + ) |
90 | 113 |
|
91 | 114 |
|
92 | 115 | class NamedArraySubclassobjects:
|
|
0 commit comments