Skip to content
This repository was archived by the owner on Jun 10, 2020. It is now read-only.

Commit 9fd098b

Browse files
committed
MAINT: add dtype argument to _SupportsArray protocol
1 parent 2a373a8 commit 9fd098b

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

numpy-stubs/__init__.pyi

+14-3
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,21 @@ _DtypeLike = Union[
9393
_NdArraySubClass = TypeVar("_NdArraySubClass", bound=ndarray)
9494

9595
class _SupportsArray(Protocol):
96-
def __array__(self) -> Union[Sequence, ndarray]: ...
96+
def __array__(
97+
self, dtype: Optional[_DtypeLike] = ...
98+
) -> Union[Sequence, ndarray]: ...
9799

98-
ArrayLike = Union[bool, int, float, complex, _SupportsArray, Sequence]
100+
# Note that generic.__array__ and ndarray.__array__ don't accept
101+
# keyword arguments
102+
#
103+
# >>> np.ndarray.__array__(np.array([1]), dtype=np.float64)
104+
# Traceback (most recent call last):
105+
# File "<stdin>", line 1, in <module>
106+
# TypeError: __array__() takes no keyword arguments
107+
#
108+
# so they don't actually satisfy the protocol we've defined here. For
109+
# that reason include them explicitly in the union.
110+
ArrayLike = Union[bool, int, float, complex, generic, ndarray, _SupportsArray, Sequence]
99111

100112
class dtype:
101113
names: Optional[Tuple[str, ...]]
@@ -220,7 +232,6 @@ class _ArrayOrScalarCommon(
220232
def shape(self) -> _Shape: ...
221233
@property
222234
def strides(self) -> _Shape: ...
223-
def __array__(self) -> ndarray: ...
224235
def __int__(self) -> int: ...
225236
def __float__(self) -> float: ...
226237
def __complex__(self) -> complex: ...

tests/fail/array_like_py3.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import numpy as np
22

3-
class A: pass
43

5-
x1: 'np.ArrayLike' = (i for i in range(10)) # E: Incompatible types in assignment
6-
x2: 'np.ArrayLike' = A() # E: Incompatible types in assignment
7-
x3: 'np.ArrayLike' = {1: 'foo', 2: 'bar'} # E: Incompatible types in assignment
4+
class A:
5+
pass
6+
7+
8+
x1: "np.ArrayLike" = (i for i in range(10)) # E: Incompatible types in assignment
9+
x2: "np.ArrayLike" = A() # E: Incompatible types in assignment
10+
x3: "np.ArrayLike" = {1: "foo", 2: "bar"} # E: Incompatible types in assignment

tests/pass/array_like_py3.py

+19-15
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,33 @@
22

33
import numpy as np
44

5-
x1: 'np.ArrayLike' = True
6-
x2: 'np.ArrayLike' = 5
7-
x3: 'np.ArrayLike' = 1.0
8-
x4: 'np.ArrayLike' = 1 + 1j
9-
x5: 'np.ArrayLike' = np.int8(1)
10-
x6: 'np.ArrayLike' = np.float64(1)
11-
x7: 'np.ArrayLike' = np.complex128(1)
12-
x8: 'np.ArrayLike' = np.array([1, 2, 3])
13-
x9: 'np.ArrayLike' = [1, 2, 3]
14-
x10: 'np.ArrayLike' = (1, 2, 3)
15-
x11: 'np.ArrayLike' = 'foo'
5+
x1: "np.ArrayLike" = True
6+
x2: "np.ArrayLike" = 5
7+
x3: "np.ArrayLike" = 1.0
8+
x4: "np.ArrayLike" = 1 + 1j
9+
x5: "np.ArrayLike" = np.int8(1)
10+
x6: "np.ArrayLike" = np.float64(1)
11+
x7: "np.ArrayLike" = np.complex128(1)
12+
x8: "np.ArrayLike" = np.array([1, 2, 3])
13+
x9: "np.ArrayLike" = [1, 2, 3]
14+
x10: "np.ArrayLike" = (1, 2, 3)
15+
x11: "np.ArrayLike" = "foo"
16+
1617

1718
class A:
18-
def __array__(self):
19+
def __array__(self, dtype=None):
1920
return [1, 2, 3]
2021

21-
x12: 'np.ArrayLike' = A()
22+
23+
x12: "np.ArrayLike" = A()
24+
2225

2326
class B:
24-
def __array__(self):
27+
def __array__(self, dtype=None):
2528
return np.array([1, 2, 3])
2629

27-
x13: 'np.ArrayLike' = B()
30+
31+
x13: "np.ArrayLike" = B()
2832

2933
# Escape hatch for when you mean to make something like an object
3034
# array.

0 commit comments

Comments
 (0)