Skip to content

MAINT: Use a StructSequence in place of the typeinfo tuples #10154

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

Merged
merged 4 commits into from
Jan 5, 2018

Conversation

eric-wieser
Copy link
Member

@eric-wieser eric-wieser commented Dec 3, 2017

Part of trying to dissect the problem with #10151.

Replaces the tuples in np.core.multiarray.typeinfo with struct sequences (which in Py3 only, subclass tuple).

This makes this all a little more readable.

Also adds a bunch of error checking while I'm here.

@eric-wieser eric-wieser force-pushed the fixup-numerictypes branch 6 times, most recently from 34b13b4 to ad74ecc Compare December 7, 2017 06:45
types.append(Type(name))
return types

def larger_types(self):
bits = typeinfo[self.NAME][3]
Copy link
Member Author

@eric-wieser eric-wieser Dec 7, 2017

Choose a reason for hiding this comment

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

Hey, the refactor was useful! Seems this has exposed a bug in what was already here... ([3] is .alignment under the new spelling)

@mhvk
Copy link
Contributor

mhvk commented Dec 7, 2017

This looks very nice! Definitely substantially clearer.

self.dtypechar = typeinfo[self.NAME][0]
assert_equal(self.type_num, info.num)
self.dtype = info.type
self.elsize = info.bits / 8
Copy link
Contributor

Choose a reason for hiding this comment

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

I know it was here already, and it is only a test, so perhaps OK, but it still seems strange not to use //.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, I'd argue that all of these are wrong, and should be ceil(bits / 8) or must_be_integral(bits / 8). Would prefer to leave that out of scope, either way

for info, charname, intname, Intname in [
(i_info,'i%d' % (bits//8,), 'int%d' % bits, 'Int%d' % bits),
(u_info,'u%d' % (bits//8,), 'uint%d' % bits, 'UInt%d' % bits)
]:
Copy link
Member

Choose a reason for hiding this comment

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

Might put this at the end of the previous line and indent both lines another 4 spaces.

Copy link
Member Author

Choose a reason for hiding this comment

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

Also reasonable - feel free to edit in the web editor, because I'm not going to get around to checking out this branch today.

Out of curiousity, does PEP8 have anything to say here?

Copy link
Member

Choose a reason for hiding this comment

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

It doesn't mention the for case explicitly, and the if version leaves it pretty open. I'm just thinking C here. It would also be reasonable to just indent the ]:.

};

static PyStructSequence_Desc typeinfo_desc = {
/* name */ "numpy.core.multiarray.typeinfo",
Copy link
Member

Choose a reason for hiding this comment

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

The comment would normally go after the entry.

Copy link
Member

Choose a reason for hiding this comment

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

I think it would also look better, especially as it would be expected.

Copy link
Member Author

Choose a reason for hiding this comment

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

Column alignment is easier to maintain this way, but I think you're right that consistency wins here, and cpython uses what you suggest internally

@charris charris merged commit 10ccfe7 into numpy:master Jan 5, 2018
@charris
Copy link
Member

charris commented Jan 5, 2018

Thanks Eric.

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

Successfully merging this pull request may close these issues.

3 participants