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

Migrating to keyword only arguments #2130

Closed
DRMacIver opened this issue Oct 14, 2019 · 3 comments · Fixed by #2317
Closed

Migrating to keyword only arguments #2130

DRMacIver opened this issue Oct 14, 2019 · 3 comments · Fixed by #2317
Assignees
Labels
legibility make errors helpful and Hypothesis grokable

Comments

@DRMacIver
Copy link
Member

Keyword only arguments are lovely and with the upcoming dropping of Python 2 support (😻 😻 😻 ) we can start actually using them in our APIs.

My proposed plan:

  1. All strategies should move to keyword only arguments for everything that currently takes a default argument.
  2. We should have a deprecation path where all of the existing arguments can be passed positionally with the name realname_deprecated. This emits a deprecation warning but otherwise does the right thing. We can handle this essentially transparently with a decorator.
  3. We update the guide to strongly encourage the use of keyword only arguments for most strategies, and that strategies should have at most one or two positional arguments.

Because we do all of this backwards compatibly we don't need to (and shouldn't) do this in Hypothesis 4.0, which can be a pure deprecation release, but it would be nice to have the details of the plan fully fleshes out by the time 4.0 lands so we can do it soon afterwards.

It would be particularly good to note any exceptions where we shouldn't have arguments as keyword only.

@alexwlchan
Copy link
Contributor

I had a quick skim through https://hypothesis.readthedocs.io/en/latest/data.html

All look good to go with keyword only arguments, save hypothesis.strategies.runner(default=not_set), which I think would be less bad to allow positional but is still basically fine.

@Zac-HD Zac-HD added the legibility make errors helpful and Hypothesis grokable label Oct 14, 2019
@Zac-HD
Copy link
Member

Zac-HD commented Oct 14, 2019

Sounds great! I'd apply the "defaults = kwonly" principle to everything, not just strategies, and use *__realname_deprecated so that tab-completion engines can tell they shouldn't suggest it.

I've also been looking at refactoring tools that would let us ship an upgrade script to automatically fix any uses. Nothing specific yet, but the principle would be great ("automatically upgrade any automatically fixable deprecation"). We'll see.

As an exception to the "has default = kwonly" principle, I'd argue that for numeric strategies min_value and max_value should be positional-or-keyword; the ergonomics of integers(1, 10) are too good to lose and it's perfectly clear what that means... though the four boolean flags to floats() can all go kwonly.

@Zac-HD
Copy link
Member

Zac-HD commented Dec 11, 2019

scikit-learn is doing something similar, and reading their proposal made me realise that it's easier to define the function with real kwonly arguments and have the decorator create the compat form than vice-versa.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legibility make errors helpful and Hypothesis grokable
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants