Skip to content
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

ENH: Allow creating structured void scalars by passing dtype #22316

Merged
merged 2 commits into from Nov 21, 2022

Conversation

seberg
Copy link
Member

@seberg seberg commented Sep 20, 2022

Adds an optional dtype= kwarg to np.void. If given (and not None), this kwarg effectively turns it into:

 res = np.array(data, dtype=dtype)[()]

The new dtype argument is keyword-only.

The whole idea behind is that if NEP 51 (scalar repr changes) flies, it would be nice if a new void representation is copy-pastable. This achieves that. So it is a small enhancement (I think), that is a stepping stone for fixing scalar representations.

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

Looks good! Only some small points. A more general one is whether keyword-only is needed here. By analogy with np.array, I think having the dtype as the second positional argument is OK.

numpy/core/src/multiarray/scalartypes.c.src Outdated Show resolved Hide resolved
numpy/core/tests/test_scalar_ctors.py Show resolved Hide resolved
match="void: descr must be a `void.*int64"):
np.void(4, dtype="i8")

dtype = np.dtype("4i")
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this defined? I would use '4i' for clarity in the actual test.

Copy link
Member Author

Choose a reason for hiding this comment

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

whoops, wanted to avoid a TypeError due to getting it wrong in the test, and then did it wrong :). Will just match the error string instead.

@seberg
Copy link
Member Author

seberg commented Sep 21, 2022

OK, updated the typing stubs and test (not sure that it is correct) and fixed up those things.

Also allowed it to be passed positionally, it really seems unnecessary to enforce keyword here. (And added a brief release note.) I think it should be OK now modulo the typing/typing tests.

@seberg seberg force-pushed the void-strctured-new branch 2 times, most recently from dbcd09f to dca9d09 Compare September 21, 2022 08:22
@seberg
Copy link
Member Author

seberg commented Sep 21, 2022

@BvB93 I guess it doesn't work easily to reject certain objects only if dtype is not given (or None) with the current typing semantics? It is a bit of a strange overload admittedly...

numpy/__init__.pyi Outdated Show resolved Hide resolved
@seberg seberg force-pushed the void-strctured-new branch 3 times, most recently from 7f953e7 to 44863c9 Compare September 21, 2022 11:37
Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

Looks all good except possible the typing error.

Adds an optional `dtype=` kwarg to `np.void`.  If given (and not None),
this kwarg effectively turns it into:

     res = np.array(data, dtype=dtype)[()]

Thanks for Marten's review and Bas' help with the typing.

Reviewed-by: Marten van Kerkwijk <mhvk@astro.utoronto.ca>
Co-authored-by: Bas van Beek <43369155+BvB93@users.noreply.github.com>
@seberg seberg added the 62 - Python API Changes or additions to the Python API. Mailing list should usually be notified. label Nov 21, 2022
@seberg
Copy link
Member Author

seberg commented Nov 21, 2022

I think this is good to go, unless anyone thinks the API addition needs to be announced wider.

be returned.
dtype : dtype, optional
If provided the dtype of the new scalar. This dtype must
be "void" dtype (i.e. a structured or unstructured
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Linking to https://numpy.org/devdocs/user/basics.rec.html#structured-datatypes (lets see if that works), that seems useful. This is that documentation, so that would be circular ;).

has three calling conventions:

1. ``np.void(5)`` creates a ``dtype="V5"`` scalar filled with
``\0`` bytes. The 5 can be a Python or NumPy integer.
Copy link
Member

Choose a reason for hiding this comment

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

How many \0 bytes?

Copy link
Member

@mattip mattip left a comment

Choose a reason for hiding this comment

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

Just some documentation nits, feel free to mark them as rejected if they don't make sense.

Co-authored-by: Matti Picus <matti.picus@gmail.com>
@seberg
Copy link
Member Author

seberg commented Nov 21, 2022

Thanks Matti. Tweaked the docs according to those notes and tests are passing now. So just going ahead and merging myself.

@seberg seberg merged commit 5d32b8d into numpy:main Nov 21, 2022
@seberg seberg deleted the void-strctured-new branch November 21, 2022 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
01 - Enhancement 62 - Python API Changes or additions to the Python API. Mailing list should usually be notified.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants