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

Improve support for @st.composite with @classmethod or @staticmethod #2634

Merged
merged 2 commits into from
Oct 24, 2020

Conversation

Zac-HD
Copy link
Member

@Zac-HD Zac-HD commented Oct 3, 2020

Closes #2578.

@Zac-HD Zac-HD added the enhancement it's not broken, but we want it to be better label Oct 3, 2020
@Zac-HD Zac-HD force-pushed the composite-methods branch 2 times, most recently from 284552c to 115f360 Compare October 3, 2020 06:24
'8z]EIFA06^l`i^': 'LFE{Q',
'8z]EIFA06^li^': 'LFE{Q',
Copy link
Member Author

Choose a reason for hiding this comment

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

This is just because the backtick broke syntax highlighting support in VS code, and I wanted to delay new contributors' first encounter with the Curse of Hypothesis.

@Zac-HD
Copy link
Member Author

Zac-HD commented Oct 20, 2020

Hey @HypothesisWorks/hypothesis-python-contributors, anyone want to review a PR? It's short and simple, promise 😇

@@ -302,6 +302,10 @@ lead to serious performance problems. For example:
result.add(draw(elements.filter(lambda x: x not in result)))
return result

:func:`@composite <hypothesis.strategies.composite>` *can* be used on methods,
classmethods, and staticmethods, but note that the ``draw`` argument must come
before ``self`` or ``cls``. See :issue:`2578` for more details.
Copy link
Member

Choose a reason for hiding this comment

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

This will break several linters. At least pylint and wemake-python-styleguide.
And mypy too.

😢

Copy link
Member Author

Choose a reason for hiding this comment

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

I think my response is "don't use non-static methods as composite strategies then"; personally I think that using a function is cleaner anyway.

The basic problem is that there's no way for us to distinguish between functions and methods - because at runtime methods are functions - if @composite is applied alone or before @classmethod. We are therefore left with a trilemma between this consistent-but-unlintable style, using unsound heuristics like guessing based on the argument name, or having the behaviour vary based on the order of decorators. Deprecating the use of @composite on methods is also out, because as above there's no way to tell at runtime.

Overall, I prefer this PR as an improvement on the status quo ("draw must come first, and @composite must be the inner-most decorator") even though I wouldn't actually use it for anything but staticmethods... and in that case why not use a function?

Copy link
Member Author

Choose a reason for hiding this comment

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

@sobolevn - if you're happy with that explanation, you can approve and merge 😉

Copy link
Member

Choose a reason for hiding this comment

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

Do we need to mention it in the docs, what do you think? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

I've clarified and expanded the docs, though without discussing the specific implementation constraints beyond linking to this PR.

I've also tweaked the test for #2646 as you suggested after merge 🙂

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Awesome work! 👍

@Zac-HD Zac-HD merged commit 01589ad into HypothesisWorks:master Oct 24, 2020
@Zac-HD Zac-HD deleted the composite-methods branch October 24, 2020 11:11
@Zac-HD
Copy link
Member Author

Zac-HD commented Oct 24, 2020

Thanks 🥰

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 this pull request may close these issues.

Improve composability of @classmethod and @st.composite
2 participants