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

Nicer structured-dtype constructor #13701

Open
anntzer opened this issue Jun 3, 2019 · 3 comments
Open

Nicer structured-dtype constructor #13701

anntzer opened this issue Jun 3, 2019 · 3 comments

Comments

@anntzer
Copy link
Contributor

anntzer commented Jun 3, 2019

It would be nice if a structured-stype constructor taking **kwargs was available, ie.

np.dtype(foo=int, bar=(float, 2))

being equivalent to

np.dtype([("foo", int), ("bar", float, 2)])

the latter being quite a mouthful to type (I realize that this can be made into a one-line helper function too..).

It is true that further overloading the (already very overloaded) np.dtype constructor results in an ambiguity, because the following is currently legal:

In [1]: np.dtype(dtype=np.uint16)
Out[1]: dtype('uint16')

(even though the docs say that the first argument is named "obj" -- but it's actually named "dtype"...)

However, I think it would make sense to make the first argument (when used in that semantic) positional only, so that (after some deprecation period) np.dtype(dtype=np.uint16) means np.dtype([("dtype", np.uint16)]).
Obviously, if there are no fields named "dtype", there is no ambiguity even as of now.

Numpy/Python version information:

1.16.3 3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0]

@seberg
Copy link
Member

seberg commented Jun 3, 2019

Sounds like something to think about when/if we split up the dtypes into a true type hierarchy, so that it might be np.structured(...) or similar.

@eric-wieser
Copy link
Member

I would be very against adding more overloads to the dtype constructor, it has too many overloads already. A different helper function or static factory function might be fine though. Elsewhere I show how to declare structured types using class syntax

Note also that your proposed syntax results in a non-deterministic field order until python 3.6

@anntzer
Copy link
Contributor Author

anntzer commented Jun 3, 2019

That's definitely intended for Py3.6+ (I'm fine with waiting until numpy only supports Py3.6+, it's not really urgent...).
Making this a separate function is fine too.

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

No branches or pull requests

3 participants