numpy / numpy-stubs

Experimental typing stubs for NumPy
BSD 3-Clause "New" or "Revised" License
282 stars 32 forks source link

MAINT: ban setting `ndarray.dtype` #47

Closed person142 closed 4 years ago

person142 commented 4 years ago

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.

shoyer commented 4 years ago

Thanks!