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

Commit ba67281

Browse files
authored
MAINT: ban setting ndarray.dtype (#47)
See discussion in https://github.com/numpy/numpy-stubs/issues/7. Though this is valid NumPy, users should be using `ndarray.view` instead of setting the dtype, and setting the dtype prevents sensibly making `ndarray` generic over dtype because of situations like this: ``` x = np.ones((2,), dtype=np.int64) # type is ndarray[np.int64] x.dtype = np.bool_ # What is the type now? ``` If someone really wants to do this, they will now have add an ignore, which should make it clear that type safety is going out the window.
1 parent 7d44d93 commit ba67281

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
__pycache__
44
numpy_stubs.egg-info/
55
venv
6-
.idea
6+
.idea
7+
**~

numpy-stubs/__init__.pyi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,6 @@ class ndarray(_ArrayOrScalarCommon, Iterable, Sized, Container):
297297
) -> ndarray: ...
298298
@property
299299
def dtype(self) -> _Dtype: ...
300-
@dtype.setter
301-
def dtype(self, value: _DtypeLike): ...
302300
@property
303301
def ctypes(self) -> _ctypes: ...
304302
@property

tests/fail/ndarray.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import numpy as np
2+
3+
# Ban setting dtype since mutating the type of the array in place
4+
# makes having ndarray be generic over dtype impossible. Generally
5+
# users should use `ndarray.view` in this situation anyway. See
6+
#
7+
# https://github.com/numpy/numpy-stubs/issues/7
8+
#
9+
# for more context.
10+
float_array = np.array([1.0])
11+
float_array.dtype = np.bool_ # E: Property "dtype" defined in "ndarray" is read-only

0 commit comments

Comments
 (0)