Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix for issue 6061: unsafe casting of int/bool to str #6067

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

Closed
wants to merge 2 commits into from

Conversation

vestuto
Copy link

@vestuto vestuto commented Jul 11, 2015

Made changes and added tests necessary to ensure that casting from int and bool to string is not treated as safe.

Note: even with these changes, there is still an open question about promotion, for example:

 In [2]: np.concatenate( (['a'], [1,2,3]) )
Out[2]: 
array(['a', '1', '2', '3'], 
      dtype='|S21')

I did nothing to address this.

Link to issue: gh-6061

vestuto added 2 commits July 11, 2015 16:20
- Fixes issue numpy#6061
- Updated _npy_can_cast_safely_table to define such casts as not safe.
- Updated existing python tests to be in agreement with this change.
- Added new test case to test_regression.py to check safe cast example cited in issue numpy#6061
- Updated _npy_can_cast_safely_table to define cast from bool to string as unsafe
- Deleted code block that tested string size to make sure int could fit in it when cast, for purposes of establishing whether the cast was safe.
- Added assert tests to test_numeric.py to check np.can_cast to ensure that int and bools really are not safe to cast to strings.
@mattip
Copy link
Member

mattip commented Aug 15, 2019

Revisit casting after the new dtypes go in

@charris
Copy link
Member

charris commented Dec 17, 2020

@mattip @seberg Any changes relevant to this?

@seberg
Copy link
Member

seberg commented Dec 17, 2020

Oh wow, I remember this at SciPy :). It was a nice excercise, the code looks quite different now so closing.

My current angle is a bit that while I don't like this "unsafe" cast logic too much, I also see it as less of a problem. The problem is rather that type promotion/common-dtype in NumPy used to be tied strictly to "safe" casting.

So while this can be discussed, the far more important step is the start on deprecating number -> string promotion (I had a PR for that, which is now a bit outdated, and unfortunately a bit more complicated with respect to giving a FutureWarning for np.array([1, "1"]))

@seberg seberg closed this Dec 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants