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

Type annotations used for functions strategy #2425

Closed
lengau opened this issue May 11, 2020 · 3 comments · Fixed by #2446
Closed

Type annotations used for functions strategy #2425

lengau opened this issue May 11, 2020 · 3 comments · Fixed by #2446
Labels
enhancement it's not broken, but we want it to be better

Comments

@lengau
Copy link

lengau commented May 11, 2020

When you accept a function as a parameter to another function, if you're using mypy and typing you may well have that method described already using Callable etc. In this case, it's redundant to create a lambda function for the like attribute of hypothesis.strategies.functions. It would be nice to be able to reuse these objects in hypothesis.

@Zac-HD Zac-HD added the enhancement it's not broken, but we want it to be better label May 11, 2020
@Zac-HD
Copy link
Member

Zac-HD commented May 12, 2020

It's not quite clear to me what you're asking for. If you mean "st.functions(like=Callable[[int, int], int]) should work", you're better off using st.from_type().

If you mean "st.functions() ought to infer a strategy for returns from the return-type annotation on the like function", that sounds pretty good to me! I'd be happy to help out if you'd like to open a PR: hypothesis.internal.compat.get_type_hints(func).get("return", type(None)) and st.from_type() should get you started 😄

@akash-suresh
Copy link
Contributor

Hey @Zac-HD ! I would like to pick this up. But I would need some guidance on how to get this done. 😄

@Zac-HD
Copy link
Member

Zac-HD commented May 20, 2020

Sure! I'll keep the tips a little vague, because figuring out how to do it is part of the fun. Getting stuck isn't fun for long though, so if you're getting frustrated just push what you have up and ask me for more detailed advice.

  1. Find the definition of the functions() strategy.
  2. We're going to change the default value of the returns argument to None (instead of st.none()).
  3. If a strategy is passed, same logic as now.
  4. If returns is None, we want to get the return-type annotation of the like argument using our get_type_hints function.
  5. Use st.from_type() to get the strategy, defaulting to st.none() if there is no return type hint (again, the current behaviour)
  6. Write some tests in tests/cover/test_functions.py to check that it's working.
  7. Write release notes in a new file hypothesis-python/RELEASE.rst for a minor release. Check some of the other PRs for examples of this.
  8. Add your name to the list of contributors in CONTRIBUTING.rst

and that's it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement it's not broken, but we want it to be better
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants