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

[TYP] return array dtype is always unkown but it's actually specified as parameter #3602

Merged

Conversation

FrancescElies
Copy link

Given the following example

import numpy as np
from hypothesis.extra.numpy import arrays

a = arrays(np.int32, shape=2)
reveal_type(a)

On master running the typechecker the ndarray dtype is unkown

# mytest.py:5: note: Revealed type is "hypothesis.strategies._internal.strategies.SearchStrategy[numpy.ndarray[Any, Any]]"

If my understanding is correct, after this pr the dtype parameter will be taken into account.

# mytest.py:5: note: Revealed type is "hypothesis.strategies._internal.strategies.SearchStrategy[numpy.ndarray[Any, numpy.dtype[def (Union[typing.SupportsInt, builtins.str, builtins.bytes, typing.SupportsIndex] =) -> numpy.signedinteger[numpy._typing._32Bit]]]]"

Copy link
Member

@Zac-HD Zac-HD left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! I'm looking forward to getting this in, though (per comments below) it might be a bit fiddly.

See also CONTRIBUTING.rst, for how to create a RELEASE.rst file with changelog (RELEASE_TYPE: patch) and add your name to the list of authors 😁

@@ -37,6 +37,7 @@
from hypothesis.strategies._internal.numbers import Real
from hypothesis.strategies._internal.strategies import T, check_strategy
from hypothesis.strategies._internal.utils import defines_strategy
from numpy.typing import DTypeLike, NDArray
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 this might increase our minimum supported numpy version. That's fine if we have to, but I think we could probably use an if TYPE_CHECKING: block and string annotations to preserve compatibility?

Copy link
Author

Choose a reason for hiding this comment

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

Right, I only checked for py37 compatibility.

NDArray was introduced in numpy 1.21 and DTypeLike in v1.20, v1.21 is from june 2022, I belive that's old enough,
I would personally introduce a install_requires=['numpy>=1.21'] in the setup.py, but the call is totally yours.

What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

That's as old as NEP-29 suggests supporting, which seems a quite principled way to make the decision. @honno do you have thoughts on this?

Regardless we should set our actual minimum version in extras_require - I doubt we actually still support back to 1.9.0 - and think about setting up a minimum-numpy-version CI job. The CI job should be a separate PR though!

Copy link
Author

Choose a reason for hiding this comment

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

That's as old as NEP-29 suggests supporting, which seems a quite principled way to make the decision. @honno do you have thoughts on this?

I missed that, then we are good to go, no need for if TYPE_CHECKING right?

hypothesis-python/src/hypothesis/extra/numpy.py Outdated Show resolved Hide resolved
hypothesis-python/src/hypothesis/extra/numpy.py Outdated Show resolved Hide resolved
Comment on lines -385 to +389
) -> st.SearchStrategy[np.ndarray]:
) -> st.SearchStrategy[NDArray[D]]:
Copy link
Member

Choose a reason for hiding this comment

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

We could also declare the shape here, for constant shapes... albeit by going from two to four overloads (value or strategy for each of two arguments).

Copy link
Author

Choose a reason for hiding this comment

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

I think we can get away without overloads in this case, by defining another typevar for shape but honestly I would like to leave this point for now until we sort out the rest the comments you made in this PR.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good to me, I'll be very happy to get in whatever improvement we have and write up an issue with the rest. 👍

hypothesis-python/src/hypothesis/extra/numpy.py Outdated Show resolved Hide resolved
Copy link
Member

@Zac-HD Zac-HD left a comment

Choose a reason for hiding this comment

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

Thanks again Francesc! I'm sorry it took a while for me to get back to this, but delighted to be improving support for type-inference in our API 🙂

@Zac-HD Zac-HD enabled auto-merge April 25, 2023 02:27
@Zac-HD Zac-HD disabled auto-merge April 25, 2023 03:39
@Zac-HD Zac-HD merged commit 4590978 into HypothesisWorks:master Apr 25, 2023
49 checks passed
@FrancescElies
Copy link
Author

@Zac-HD sorry for the late reply, I was out of town.

No worries I also left this pr on the side and didn't realize how close we were to get that CI green, thanks for the lint fixes!

Until the next time 👋🙂

@FrancescElies FrancescElies deleted the cesc/fix-typing-array-return-dtype branch May 3, 2023 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants