Repository navigation
gh #7131 Support for astype with literal strings - #7132
Conversation
|
@njriasan thank you for submitting this to the Numba issue tracker. I have added it to the queue for review. Please note that we are currently in a burn-down period towards a release candidate, so the review for this PR may be somewhat delayed. We thank you in advance for your patience and understanding! |
stuartarchibald
left a comment
There was a problem hiding this comment.
Thanks for the patch, couple of things to resolve else looks good.
|
@stuartarchibald I've update this PR with your suggested changes. Thank you. |
stuartarchibald
left a comment
There was a problem hiding this comment.
Thanks for the update, couple of minor things to resolve else looks good.
|
@stuartarchibald Sorry for the delay. I finally got the time to finalize this PR. |
|
@stuartarchibald Just wanted to send you another ping in case the notification got lost in your inbox. No rush on this, but it should be a quick review when you have time. |
stuartarchibald
left a comment
There was a problem hiding this comment.
Thanks for the update, I've left some comments inline, I think given the nature of the corner cases being tested being very explicit in the tests is preferable. Thanks again!
| cres = self.ccache.compile(pyfunc, (typeof(arr), typeof(dtype))) | ||
| return cres.entry_point(arr, dtype) |
There was a problem hiding this comment.
This is not reached in the current tests.
| """ | ||
| expected = arr.astype(dtype).copy(order='A') | ||
| got = run_dtype_arg(arr, dtype) | ||
| self.assertPreciseEqual(got, expected) |
There was a problem hiding this comment.
This is not reached in the current tests.
| # Non-Literal String | ||
| unicode_val = "float32" | ||
| with self.assertTypingError() as raises: | ||
| check_dtype_arg(arr, unicode_val) | ||
| self.assertIn('array.astype if dtype is a string it must be constant', | ||
| str(raises.exception)) |
There was a problem hiding this comment.
Suggest something like this as it's really only this one case that exists?:
diff --git a/numba/tests/test_array_methods.py b/numba/tests/test_array_methods.py
index 64c0eba..c130d88 100644
--- a/numba/tests/test_array_methods.py
+++ b/numba/tests/test_array_methods.py
@@ -469,25 +469,6 @@ class TestArrayMethods(MemoryLeakMixin, TestCase):
got = run(arr, dtype)
self.assertPreciseEqual(got, expected)
- def run_dtype_arg(arr, dtype):
- """
- Equivalent to run but dtype is passed as
- an arg instead of as a lowered constant.
- This is used to check LiteralValue requirements.
- """
- pyfunc = lambda arr, dtype: arr.astype(dtype)
- cres = self.ccache.compile(pyfunc, (typeof(arr), typeof(dtype)))
- return cres.entry_point(arr, dtype)
- def check_dtype_arg(arr, dtype):
- """
- Equivalent to check but dtype is passed as
- an arg instead of as a lowered constant.
- This is used to check LiteralValue requirements.
- """
- expected = arr.astype(dtype).copy(order='A')
- got = run_dtype_arg(arr, dtype)
- self.assertPreciseEqual(got, expected)
-
# C-contiguous
arr = np.arange(24, dtype=np.int8)
check(arr, np.dtype('int16'))
@@ -515,10 +496,14 @@ class TestArrayMethods(MemoryLeakMixin, TestCase):
check(arr, dt)
self.assertIn('cannot convert from int32 to Record',
str(raises.exception))
- # Non-Literal String
+
+ # Check non-Literal string raises
unicode_val = "float32"
with self.assertTypingError() as raises:
- check_dtype_arg(arr, unicode_val)
+ @jit(nopython=True)
+ def foo(dtype):
+ np.array([1]).astype(dtype)
+ foo(unicode_val)
self.assertIn('array.astype if dtype is a string it must be constant',
str(raises.exception))| assert not kws | ||
| dtype, = args | ||
| if dtype == types.unicode_type: | ||
| raise RequireLiteralValue("array.astype if dtype is a string it must be constant") |
There was a problem hiding this comment.
This line needs wrapping to 80 chars.
| from .npydecl import parse_dtype | ||
| assert not kws | ||
| dtype, = args | ||
| if dtype == types.unicode_type: |
There was a problem hiding this comment.
| if dtype == types.unicode_type: | |
| if isinstance(dtype, types.UnicodeType): |
stuartarchibald
left a comment
There was a problem hiding this comment.
This should fix CI.
Fix CI issue. Co-authored-by: stuartarchibald <stuartarchibald@users.noreply.github.com>
Thank you! |
|
@stuartarchibald I made all of your suggested changes. Let me know if this PR needs anything else? |
stuartarchibald
left a comment
There was a problem hiding this comment.
Thanks for the patch and fixes.
|
@njriasan Congratulations on your first contribution to Numba! |
Adds support for Array.astype with string literal type names. This required two changes:
get_call_typewhere the literal option would never be tried if the unliteral version was preferred and returned None.Reference an existing issue
Closes #7131