Repository navigation
[Unicode] support startswith with args, start and end. - #8557
Conversation
guilhermeleobas
left a comment
There was a problem hiding this comment.
Thanks for working on this feature @dlee992. I've made a few comments regarding the exception message raised. Other than that, code looks good.
Co-authored-by: Guilherme Leobas <guilhermeleobas@gmail.com>
Co-authored-by: Guilherme Leobas <guilhermeleobas@gmail.com>
|
I have checked that:
|
|
Actually, the API |
stuartarchibald
left a comment
There was a problem hiding this comment.
Thanks for the patch. Few minor things to look at else looks good.
| types.NoneType))): | ||
| raise TypingError('When specified, the arg "end" must be an Integer') | ||
|
|
||
| if isinstance(prefix, (types.Tuple, types.UniTuple)): |
There was a problem hiding this comment.
Tuple cannot be accepted here unless the contents of the Tuple are all types.StringLiteral. Consider:
@njit
def foo():
print('abcde'.startswith(('a', 1)))
foo()it may be easier to just not accept Tuple to force type inference to try with a non-literal types instead.
There was a problem hiding this comment.
I prefer to accept a UniTuple of UnicodeType, since it's more consistent with cpython. So I want to add a check to ensure the prefix type as expected. BTW, in your case:
@njit
def foo():
print('abcde'.startswith(('a', 1)))
or another case:
prefix = ('a', 1)
@njit
def foo(prefix):
print('abcde'.startswith(prefix))
Both ('a', 1) will be inferred as Tuple(unicode_type, int64), than Tuple containing a StringLiteral.
And in the executable case:
@njit
def foo():
print('abcde'.startswith(('a', 'b')))
('a', 'b') will be inferred as UniTuple(unicode_type x 2), so I just have to ensure prefix is a UniTuple with unicode_type dtype. If I'm wrong, please point out with an example. I haven't found a counterexample...
BTW, I found an interesting thing in cpython (which I think we don't have to implement this short-cut strategy):
>>> 'a'.startswith(('a', 5))
True
>>> 'a'.startswith((1, 'a'))
Traceback (most recent call last):
File "/Users/ld/opt/anaconda3/envs/numbaenv/lib/python3.10/code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
TypeError: tuple for startswith must only contain str, not int
There was a problem hiding this comment.
Revised, please review this issue again. @stuartarchibald
There was a problem hiding this comment.
By default, type inference will try using non-literal types first, and then if that fails, it will try again using literal types. Python source containing tuples of string literals will be inferred as types.Tuple(<types.StringLiteral>, <types.StringLiteral>, ...). e.g.
from numba import njit
import numpy as np
@njit
def foo():
x = ('a', 'b')
foo()
foo.inspect_types()You can see the "try non-literal then literal" types behaviour by doing something like:
from numba import njit, types
from numba.extending import overload
import numpy as np
def bar(tup):
pass
@overload(bar)
def ol_bar(tup):
print(f"bar:: tup type is {tup}")
@njit
def foo():
x = ('a', 'b')
bar(x)
foo()(ignore the compilation error, just look at what gets printed to stdout).
RE: the 'a'.startswith(('a', 5)), there are implementation details like this in the CPython implementation. They don't need replicating unless there's some proven use case!
…fix more carefully.
stuartarchibald
left a comment
There was a problem hiding this comment.
Thanks for the update @dlee992. I've made a few suggestions about restructuring the type checking with respect to the type of prefix (and a few other minor things). I think once resolved this should be ready to merge.
Note: I'll answer your questions on the diff inline now so as to not get them confused with this review.
Co-authored-by: stuartarchibald <stuartarchibald@users.noreply.github.com>
Co-authored-by: stuartarchibald <stuartarchibald@users.noreply.github.com>
Co-authored-by: stuartarchibald <stuartarchibald@users.noreply.github.com>
Co-authored-by: stuartarchibald <stuartarchibald@users.noreply.github.com>
stuartarchibald
left a comment
There was a problem hiding this comment.
Thanks for the patch and fixes!
As it is, support
startandendoptional arguments forstartswith.Most added code comes from the implementation of
endswith, with marginal changes.PS, I also have aother PR about supporting
int(str)based on this one, and I will open that one after merging this.BTW, I have a question about
_nrt=False, I didn't find any document about this, what is it for in the Numba Runtime level? Disable any memory allocation/refct/release?