-
Notifications
You must be signed in to change notification settings - Fork 590
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
Annotate decorators with ParamSpec
#3212
Changes from all commits
0441629
ebf158c
baa19bf
fe48210
3a19cc9
4536365
75472b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
RELEASE_TYPE: minor | ||
|
||
This preserves the type annotations of functions passed to :func:`hypothesis.strategies.composite` and :func:`hypothesis.strategies.functions` by using :obj:`python:typing.ParamSpec`. | ||
|
||
This improves the ability of static type-checkers to check test code that uses Hypothesis, and improves auto-completion in IDEs. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ | |
from uuid import UUID | ||
|
||
import attr | ||
from typing_extensions import Concatenate | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not a requirement, and I'd like to keep it optional - in which case I think the best option is to try importing ParamSpec (from typing, then typing_extensions); and then have two function definitions behind an |
||
|
||
from hypothesis.control import cleanup, note | ||
from hypothesis.errors import InvalidArgument, ResolutionFailed | ||
|
@@ -94,6 +95,7 @@ | |
from hypothesis.strategies._internal.shared import SharedStrategy | ||
from hypothesis.strategies._internal.strategies import ( | ||
Ex, | ||
P, | ||
SampledFromStrategy, | ||
T, | ||
one_of, | ||
|
@@ -1451,7 +1453,9 @@ def __call__(self, strategy: SearchStrategy[Ex], label: object = None) -> Ex: | |
|
||
|
||
@cacheable | ||
def composite(f: Callable[..., Ex]) -> Callable[..., SearchStrategy[Ex]]: | ||
def composite( | ||
f: Callable[Concatenate[DrawFn, P], Ex] | ||
) -> Callable[P, SearchStrategy[Ex]]: | ||
"""Defines a strategy that is built out of potentially arbitrarily many | ||
other strategies. | ||
|
||
|
@@ -1850,10 +1854,10 @@ def emails() -> SearchStrategy[str]: | |
@defines_strategy() | ||
def functions( | ||
*, | ||
like: Callable[..., Any] = lambda: None, | ||
like: Callable[P, Any] = lambda: None, | ||
returns: Optional[SearchStrategy[Any]] = None, | ||
pure: bool = False, | ||
) -> SearchStrategy[Callable[..., Any]]: | ||
) -> SearchStrategy[Callable[P, Any]]: | ||
# The proper type signature of `functions()` would have T instead of Any, but mypy | ||
# disallows default args for generics: https://github.com/python/mypy/issues/3737 | ||
"""functions(*, like=lambda: None, returns=none(), pure=False) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wasn't sure whether to consider this minor or patch. On one hand, it only touches annotations and has no effect at runtime, so should probably be considered patch. However, if anyone is running mypy on their code which uses Hypothesis, the increased strictness of the annotations could cause mypy to fail code that passed before, which could be considered a breaking change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's honestly a borderline case, but in borderline cases I prefer to err on the side of
minor
- we make no compatibility guarantees about type annotations, but it's still polite to make version constraints easy if we think users might want them.