-
Notifications
You must be signed in to change notification settings - Fork 587
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
Correctly typing draw
argument in composite strategies
#3005
Comments
Happily, PEP-612 means that we'll be able to annotate this correctly from Python 3.10 onwards. Until then, you might still have some trouble with type-checkers like |
Thanks for the quick response. I had indeed tried something from typing import Callable, Tuple, TypeVar
import hypothesis.strategies as hp_st
T = TypeVar('T')
Draw = TypeVar('Draw', bound=Callable[[hp_st.SearchStrategy[T]], T])
@hp_st.composite
def foo(draw: Draw) -> hp_st.SearchStrategy[Tuple[int, int]]:
return draw(
hp_st.tuples(
hp_st.integers(),
hp_st.integers(),
)
) Unfortunately, that still isn't enough to satisfy mypy, which now complains
In other words, the type(s) of the Otherwise, thanks again. 😄 |
Ah... you've found the same problem as Guido van Rossum! Specifically, we'll need the not-yet-drafted follow-up to PEP-646 to support this "properly". But, we can just add some overloads for lengths 0-5 like we already have for |
Haha awesome, I feel better to know that I wasn't making some obvious error here. And I look forward to trying out whatever you come up with. 😄 Thanks! |
Now released as version 6.13.14 🎉
|
Works for me! Nice one, I appreciate it. |
Just for the record (if someone stumbles across this) use hypothesis.strategies.DrawFn to annotate |
I think this is probably a trivially answered question, but I wasn't able to find an answer in the documentation and poking around in the source code didn't help either.
I'm a big fan of type-hinting, and I also use composite strategies aggressively to add narrative to my property-based testing. It makes sense to put the two together. So consider the following strategy, which is totally useless but illustrative :
My reading of the documentation suggests that this is the correct way to type the return value of the decorated function. And indeed, using
mypy==0.812
withhypothesis==6.13.11
, I see:But wait! All is not well. If we change (for example), one of the
int
annotations in the return type to, say,str
, and rerun mypy, it still reports no issues. That doesn't seem right. In fact, it turns out that the permissive defaults of mypy are providing a false sense of security here -- as can be seen by running (with the original code given above):The issue is, of course, that the
draw
argument in the strategy is untyped, so mypy can't infer the type ofdraw(...)
.So here's my question: how should I then type
draw
? I've tried messing around with type variables etc., and so far I haven't come up with something that seems nice. Are there any recommendations on how to make this work, or indeed a recommendation that this CAN'T work?The text was updated successfully, but these errors were encountered: