Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn about string unsafe casting (assigning long string into shorter) #13418

Open
jiayi797 opened this issue Apr 28, 2019 · 2 comments
Open

Warn about string unsafe casting (assigning long string into shorter) #13418

jiayi797 opened this issue Apr 28, 2019 · 2 comments

Comments

@jiayi797
Copy link

Code

import numpy as np

a = np.array(['11','22']) 
print(a) # output ['11' '22']
a[0] = '121'
print(a) # output ['12' '22'] !!!!

when I want to reset a[0] as a longer string, it will cut off my new value. it cause a bug in my system and it takes my whole week. >_<

I rewrite it to :

a = np.array(['11','22'],dtype = 'U10') 
print(a) # output ['11' '22']
a[0] = '121'
print(a) # output ['121' '22'] 

it seems work well. While I think it might cause confused again in future.

@seberg
Copy link
Member

seberg commented Apr 28, 2019

I tend to agree, similar to what Complex does. Although, we do currently use unsafe casting for assignments (i.e. also floats assigned to integer arrays get truncated). That is a bit less nasty though, so may be worth to specialize this a bit.

@jiayi797
Copy link
Author

I tend to agree, similar to what Complex does. Although, we do currently use unsafe casting for assignments (i.e. also floats assigned to integer arrays get truncated). That is a bit less nasty though, so may be worth to specialize this a bit.

unsafe casting from float to integer is common, such as java/c/c++/c#.
while I think string unsafe casting is a dangerous operator. Support that sting is some key identification, and most developer don't think string have unsafe casting. I think it will cause much confused .

@seberg seberg changed the title bug during reset value of a string array Warn about unsafe casting (assigning long string into shorter array) Sep 22, 2019
@seberg seberg changed the title Warn about unsafe casting (assigning long string into shorter array) Warn about string unsafe casting (assigning long string into shorter) Sep 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants