Skip to content

MAINT: Adjust typing for readded np.long #25039

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 8 commits into from
Nov 1, 2023

Conversation

mtsokol
Copy link
Member

@mtsokol mtsokol commented Oct 31, 2023

Connected to #24922 and #24224

Hi @seberg,
I just started working on it but I wanted to open a PR for adjusting typing after reintroducing np.long. Let's continue the discussion here!

@mtsokol mtsokol self-assigned this Oct 31, 2023
@mtsokol mtsokol force-pushed the adjust-typing-for-long branch from 57c136a to 28a696b Compare October 31, 2023 10:56
@seberg
Copy link
Member

seberg commented Oct 31, 2023

This is getting very tedious and opaque in the intp as default PR. The test are failing here though, would be great to get some of these fixes out of the way to hopefully get somewhere in that PR.
I thinkt he fix here is just that the numerictypes reveal test also needs changing.

@seberg seberg force-pushed the adjust-typing-for-long branch from e29b21f to 59ed171 Compare November 1, 2023 13:45
@seberg seberg force-pushed the adjust-typing-for-long branch from 59ed171 to 979cd87 Compare November 1, 2023 13:46
@seberg
Copy link
Member

seberg commented Nov 1, 2023

@BvB93 moving this over here for now. For some reason, I have to use pytest.mark.slow = lambda x: x to get things to run, but whatever.

The big churn here, is that randint/integers was typed incorrectly. We return int/bool only if dtype is exactly that (the code is a bit unclear, I fixed that also).

Ideally, this should make it much easier to make progress on the other PR once finished. Locally, the tests were passing, but of course I am not 100% sure that will translate to windows.

@seberg
Copy link
Member

seberg commented Nov 1, 2023

Btw. I am a bit surprised, but the reveal tests don't actually check that the revealed type matches the run-time result? Because the random ones were never actually correct here.

@@ -796,7 +796,7 @@ cdef class RandomState:
else:
raise TypeError('Unsupported dtype %r for randint' % _dtype)

if size is None and dtype in (bool, int):
if size is None and (dtype is bool or dtype is int):
Copy link
Member

Choose a reason for hiding this comment

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

This (same in generator) is the only actual change here, because this is just wrong and including it here clarifies the typing side fixes. We already return NumPy scalars when relevant and dtype(...) doesn't actually work for arbitrary dtype-likes (could be strings, or dtype instances).

@seberg
Copy link
Member

seberg commented Nov 1, 2023

OK, mypy tests are passing. I would like to finally unblock gh-24224, so would appreciate a review.

@BvB93
Copy link
Member

BvB93 commented Nov 1, 2023

Currently looking at #24224 as well; turns out an _ULongCodes and _LongCodes import is missing from ctypeslib.pyi but there are also some issues with getting ctypes.c_long vs ctypes.c_int return types right (including their unsigned version). The latter in particular is more of an annoyance rather than an actual issue, so you might want to disable those tests for the time being.

@seberg
Copy link
Member

seberg commented Nov 1, 2023

issues with getting ctypes.c_long vs ctypes.c_int return types right

Yeah, I thought reordering things might help with that one, but I am not sure it is a reasonable thing and just returning to the old platform dependent version seems OK also.

@BvB93
Copy link
Member

BvB93 commented Nov 1, 2023

Yeah, I thought reordering things might help with that one, but I am not sure it is a reasonable thing and just returning to the old platform dependent version seems OK also.

Either is fine tbh, as long as it just stops blocking your PR as the typing failures have been a hold up for long enough now IMO.

Btw. I am a bit surprised, but the reveal tests don't actually check that the revealed type matches the run-time result? Because the random ones were never actually correct here.

Just a bit more context: the reveal type tests check, using typing.assert_type, that an expressions return type matches the return type as is inferred by a type checker; it does not involve any actual runtime checks (the latter in particular would get very tricky once parametrized types such as NDArray[int64] are involved, as the interpretation of the parameter is entirely up to the parametrized types. It can represent the an element-/data-type, an attribute type, something more abstract, etc.. It's not too difficult to infer its meaning for a single specific type (or set thereof), but doing so in a general way is highly challenging if not impossible).

@seberg
Copy link
Member

seberg commented Nov 1, 2023

@BvB93 do you want to have a closer look at this? Otherwise, I am tempted to push things through and consider it a follow-up review.

Copy link
Member

@BvB93 BvB93 left a comment

Choose a reason for hiding this comment

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

It's fine to push for now; I'm hoping to do a more thorough review of the 2.0-related typing changes soon (including a few of the leftover long-related points; xref #25048), but I don't expect that will be until later this week.

@ngoldbaum
Copy link
Member

Let's pull this in to unblock changing the default integer on windows.

@ngoldbaum ngoldbaum merged commit 20103e7 into numpy:main Nov 1, 2023
@mtsokol mtsokol deleted the adjust-typing-for-long branch November 1, 2023 16:06
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.

4 participants