OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Type hinting a Hypothesis composite strategy

  • Thread starter Thread starter Andi
  • Start date Start date
A

Andi

Guest
I am using the hypothesis library and I would like to annotate my code with type hints. The docs are mentioning the hypothesis.strategies.SearchStrategy as the type for all search strategies.

Take this example:

Code:
@composite
def int_strategy(draw: DrawFn) -> hypothesis.strategies.SearchStrategy[int]:
    ... # some computation here resulting in ``x`` being an ``int``

    return x

Running mypy will (rightly so) result in an error along those lines: error: Returning Any from function declared to return "SearchStrategy[Any]" [no-any-return]

I mean, I am actually returning an int, not a SearchStrategy.

How am I supposed to type annotate my hypothesis strategies?
<p>I am using the <code>hypothesis</code> library and I would like to annotate my code with type hints. The <a href="https://hypothesis.readthedocs.io/en/latest/details.html#writing-downstream-type-hints" rel="nofollow noreferrer">docs</a> are mentioning the <code>hypothesis.strategies.SearchStrategy</code> as the type for all search strategies.</p>
<p>Take this example:</p>
<pre><code>@composite
def int_strategy(draw: DrawFn) -> hypothesis.strategies.SearchStrategy[int]:
... # some computation here resulting in ``x`` being an ``int``

return x
</code></pre>
<p>Running <code>mypy</code> will (rightly so) result in an error along those lines:
<code>error: Returning Any from function declared to return "SearchStrategy[Any]" [no-any-return]</code></p>
<p>I mean, I am actually returning an <code>int</code>, not a <code>SearchStrategy</code>.</p>
<p>How am I supposed to type annotate my <code>hypothesis</code> strategies?</p>
 
Top