Skip to content

gh #7131 Support for astype with literal strings - #7132

Merged
sklam merged 9 commits into
numba:masterfrom
njriasan:nick/astype_str_literals
Oct 6, 2021
Merged

sklam merged 9 commits into
numba:masterfrom
njriasan:nick/astype_str_literals

Conversation

@njriasan

Copy link
Copy Markdown
Contributor

Adds support for Array.astype with string literal type names. This required two changes:

  1. Adding a lower_builtin for string literals.
  2. Fixes a bug in get_call_type where the literal option would never be tried if the unliteral version was preferred and returned None.

Reference an existing issue

Closes #7131

@esc

esc commented Jun 22, 2021

Copy link
Copy Markdown
Member

@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 stuartarchibald left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch, couple of things to resolve else looks good.

Comment thread numba/core/types/functions.py
Comment thread numba/core/types/functions.py Outdated
@stuartarchibald stuartarchibald added 4 - Waiting on author Waiting for author to respond to review and removed 3 - Ready for Review labels Jul 7, 2021
@stuartarchibald stuartarchibald added this to the Numba 0.55 RC milestone Jul 7, 2021
@stuartarchibald stuartarchibald added the Effort - short Short size effort needed label Jul 7, 2021
@njriasan

njriasan commented Jul 9, 2021

Copy link
Copy Markdown
Contributor Author

@stuartarchibald I've update this PR with your suggested changes. Thank you.

@njriasan
njriasan requested a review from stuartarchibald July 12, 2021 01:36

@stuartarchibald stuartarchibald left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update, couple of minor things to resolve else looks good.

Comment thread numba/core/typing/arraydecl.py Outdated
Comment thread numba/core/typing/arraydecl.py Outdated
@njriasan

njriasan commented Sep 9, 2021

Copy link
Copy Markdown
Contributor Author

@stuartarchibald Sorry for the delay. I finally got the time to finalize this PR.

@njriasan

Copy link
Copy Markdown
Contributor Author

@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 stuartarchibald left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Comment thread numba/tests/test_array_methods.py Outdated
Comment on lines +475 to +476
cres = self.ccache.compile(pyfunc, (typeof(arr), typeof(dtype)))
return cres.entry_point(arr, dtype)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not reached in the current tests.

Comment thread numba/tests/test_array_methods.py Outdated
"""
expected = arr.astype(dtype).copy(order='A')
got = run_dtype_arg(arr, dtype)
self.assertPreciseEqual(got, expected)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not reached in the current tests.

Comment thread numba/tests/test_array_methods.py Outdated
Comment on lines +514 to +519
# 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))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))

Comment thread numba/core/typing/arraydecl.py Outdated
Comment thread numba/core/typing/arraydecl.py Outdated
assert not kws
dtype, = args
if dtype == types.unicode_type:
raise RequireLiteralValue("array.astype if dtype is a string it must be constant")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line needs wrapping to 80 chars.

Comment thread numba/core/typing/arraydecl.py Outdated
from .npydecl import parse_dtype
assert not kws
dtype, = args
if dtype == types.unicode_type:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if dtype == types.unicode_type:
if isinstance(dtype, types.UnicodeType):

Nicholas J Riasanovsky added 2 commits October 5, 2021 12:36

@stuartarchibald stuartarchibald left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should fix CI.

Comment thread numba/core/typing/arraydecl.py Outdated
Fix CI issue.

Co-authored-by: stuartarchibald <stuartarchibald@users.noreply.github.com>
@njriasan

njriasan commented Oct 5, 2021

Copy link
Copy Markdown
Contributor Author

This should fix CI.

Thank you!

@njriasan

njriasan commented Oct 5, 2021

Copy link
Copy Markdown
Contributor Author

@stuartarchibald I made all of your suggested changes. Let me know if this PR needs anything else?

@stuartarchibald stuartarchibald left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch and fixes.

@stuartarchibald stuartarchibald added 5 - Ready to merge Review and testing done, is ready to merge and removed 4 - Waiting on author Waiting for author to respond to review labels Oct 6, 2021
@stuartarchibald

Copy link
Copy Markdown
Contributor

@njriasan Congratulations on your first contribution to Numba!

@sklam
sklam merged commit efb0288 into numba:master Oct 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

5 - Ready to merge Review and testing done, is ready to merge Effort - short Short size effort needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Array.astype not supported with string literals

4 participants