Skip to content

BUG: Ignore whitespaces while parsing gufunc signatures#19627

Merged
charris merged 6 commits intonumpy:mainfrom
ankitdwivedi23:ankitd/vectorize-ignore-whitespace
Aug 16, 2021
Merged

BUG: Ignore whitespaces while parsing gufunc signatures#19627
charris merged 6 commits intonumpy:mainfrom
ankitdwivedi23:ankitd/vectorize-ignore-whitespace

Conversation

@ankitdwivedi23
Copy link
Copy Markdown
Contributor

Resolves #19597

Based on the description of gufunc signatures, whitespaces in the signature should be ignored, which is not the current behavior for the signature argument to np.vectorize. This pull request attempts to address that.

@ankitdwivedi23 ankitdwivedi23 marked this pull request as draft August 8, 2021 05:03
@mattip
Copy link
Copy Markdown
Member

mattip commented Aug 8, 2021

The parsing is actually implemented twice: once in numpy/lib/function_base.py for np.vectorize in python (which you fixed and test), and again in '_parse_signature from 'numpy/core/src/umath/ufunc_object.c for ufuncs. The latter is tested in numpy/core/test/test_ufunc.py via various test_signature functions, but I don't see any that test white space. Could you add some tests there too?

@ankitdwivedi23
Copy link
Copy Markdown
Contributor Author

ankitdwivedi23 commented Aug 8, 2021

The parsing is actually implemented twice: once in numpy/lib/function_base.py for np.vectorize in python (which you fixed and test), and again in '_parse_signature from 'numpy/core/src/umath/ufunc_object.c for ufuncs. The latter is tested in numpy/core/test/test_ufunc.py via various test_signature functions, but I don't see any that test white space. Could you add some tests there too?

Ah, thanks for pointing that out! Sure, I will take a look and update the PR.

Update:

There is already one signature parsing test in numpy/core/test/test_ufunc.py that tests for whitespace. I have added a couple more.

@ankitdwivedi23 ankitdwivedi23 marked this pull request as ready for review August 8, 2021 20:29
@seberg
Copy link
Copy Markdown
Member

seberg commented Aug 10, 2021

The C-code apparently accepts both \t and space, maybe we should do the same? Also I am wondering if a sig = sig.replace(" ", "") is not just as well, regex is always a bit tricky.

@ankitdwivedi23
Copy link
Copy Markdown
Contributor Author

The C-code apparently accepts both \t and space, maybe we should do the same? Also I am wondering if a sig = sig.replace(" ", "") is not just as well, regex is always a bit tricky.

The regex I have used should take care of all whitespace characters, not just space (unless I'm missing something).

Replacing all whitespaces in the input signature would also involve using regex: sig = re.sub(r'\s+', "", sig). But I agree, even that is more clear and concise than the current change.

@charris charris merged commit db1a343 into numpy:main Aug 16, 2021
@charris
Copy link
Copy Markdown
Member

charris commented Aug 16, 2021

Thanks @ankitdwivedi23 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

np.vectorize doesn't ignore white space in signature

4 participants