[Numpy-discussion] Suggestion: prevent silent downcast in np.full_like

A T alt.tblt1 at gmail.com
Mon Feb 24 15:00:47 EST 2020


Hello,

This is my first activity on this mailing list, please let me know if I 
am doing anything improperly.
I recently opened issue #15635 
<https://github.com/numpy/numpy/issues/15635>and it was suggested to me 
that it could be worth discussing here.

Here is a summarized code example of how unsafe downcasting in 
np.full_like() resulted in issues in our scientific toolbox:

t0 = 20.5
# We're trying to make a constant-valued "ufunc"
temperature = lambda x: np.full_like(x, t0)
print(temperature([0.1, 0.7, 2.3]))
# [20.5 20.5 20.5]
print(temperature(0))
# 20

This is consistent with the documentation (which even gives an example 
of this unsafe casting), and was obvious to fix once identified.

But what seems especially problematic to me is the fact that the code 
/looks safe, but isn't/. There is no sketchy `dtype=...` to make you 
think twice about the possibility of downcasting, but it happens all the 
same.

What are your thoughts on this topic? Should a special warning be given? 
Should the casting rule be made more strict?


Alexis THIBAULT
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20200224/6bac1167/attachment.html>


More information about the NumPy-Discussion mailing list