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

add option to generate annotations for the tests generated by hypothesis #3548

Conversation

ThunderKey
Copy link
Member

This adds a annotation argument to all public ghostwriter functions, as well as a --annotations and --no-annotations flag to the CLI. It searches the function for which the tests are generated for type annotations. If the arguments have type annotations, the same will be used for the generated tests.

The generated annotations are not as good as the detected strategies of st.builds, but it sure is a start for people writing tests that requires type hints.

Closes #3546

@ThunderKey ThunderKey marked this pull request as draft January 19, 2023 08:15
@ThunderKey ThunderKey force-pushed the feature/add-annotations-to-ghostwriter branch from 58d2fbf to 2628087 Compare January 19, 2023 12:31
@ThunderKey ThunderKey marked this pull request as ready for review January 19, 2023 14:15
hypothesis-python/src/hypothesis/extra/ghostwriter.py Outdated Show resolved Hide resolved
hypothesis-python/src/hypothesis/extra/ghostwriter.py Outdated Show resolved Hide resolved
hypothesis-python/src/hypothesis/extra/ghostwriter.py Outdated Show resolved Hide resolved
Comment on lines +49 to +52
try:
from typing import get_origin as get_origin
except ImportError:
# remove at Python 3.7 end-of-life
Copy link
Member

Choose a reason for hiding this comment

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

Note to self: we have a pretty similar helper in strategies/_internal/types.py, which dates back to Python 3.4 or so. We (I) should try porting over to the stdlib version soon and see what breaks.

@Zac-HD
Copy link
Member

Zac-HD commented Jan 19, 2023

This is fantastic work, thanks so much @ThunderKey! I'll want to do another read through after the minor comments above are addressed, probably on the weekend, but look forward to shipping this soon!

@ThunderKey
Copy link
Member Author

You're welcome!

Thanks for the tip with defaultdict and the idea of not using the ghostwriter specific _get_params function. I'll fix those shortly.

About moving the detection of the annotations to the _make_test_body: My idea was, that the annotations should be the same for each generated test in the file. For example in the magic function it could be that some of the functions would be generated with annotations and others without. I see the following 3 options. Which do you prefer?

  1. Let the ghostwriter mix having annotations and not in the same file.
  2. Leave as is, which ensures that it will always add annotations or not add annotations for each file.
  3. Pass the detection to _make_test_body, but in the magic function detect it before.

@Zac-HD
Copy link
Member

Zac-HD commented Jan 19, 2023

I prefer having/not-having annotations on a per-file basis matches the behavior that you can get by passing the argument to specify which you want. I liked (3) for reduced code duplication, but then we'd need a special case so I'm equally happy with (2).

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.

I updated the docs and then after checking CLI help shortened the argument name; let's merge and release!

@Zac-HD Zac-HD enabled auto-merge January 20, 2023 13:22
@Zac-HD Zac-HD merged commit 3f545a6 into HypothesisWorks:master Jan 23, 2023
@ThunderKey ThunderKey deleted the feature/add-annotations-to-ghostwriter branch January 23, 2023 07:35
JensHeinrich pushed a commit to JensHeinrich/hypothesis that referenced this pull request Feb 16, 2023
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.

Add type hints to the tests generated by ghostwriter
2 participants