Skip to content

Commit 9577dd6

Browse files
committed
composite/overload compatibility
1 parent 70a72bb commit 9577dd6

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

hypothesis-python/RELEASE.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
RELEASE_TYPE: patch
2+
3+
This patch turns off a warning for functions decorated with
4+
:func:`typing.overload` and then :func:`~hypothesis.strategies.composite`,
5+
although only in that order (:issue:`3970`).

hypothesis-python/src/hypothesis/strategies/_internal/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1810,7 +1810,7 @@ def _composite(f):
18101810
)
18111811
if params[0].default is not sig.empty:
18121812
raise InvalidArgument("A default value for initial argument will never be used")
1813-
if not is_first_param_referenced_in_function(f):
1813+
if not (f is typing._overload_dummy or is_first_param_referenced_in_function(f)):
18141814
note_deprecation(
18151815
"There is no reason to use @st.composite on a function which "
18161816
"does not call the provided draw() function internally.",

hypothesis-python/tests/cover/test_composite.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# obtain one at https://mozilla.org/MPL/2.0/.
1010

1111
import sys
12+
import typing
1213

1314
import pytest
1415

@@ -204,3 +205,18 @@ def my_integers(draw: st.DrawFn) -> st.SearchStrategy[int]:
204205

205206
assert len(w.list) == 1
206207
assert w.list[0].filename == __file__ # check stacklevel points to user code
208+
209+
210+
def test_composite_allows_overload_without_draw():
211+
# See https://github.com/HypothesisWorks/hypothesis/issues/3970
212+
@st.composite
213+
@typing.overload
214+
def overloaded(draw: st.DrawFn, *, x: int) -> typing.Literal[True]: ...
215+
216+
@st.composite
217+
@typing.overload
218+
def overloaded(draw: st.DrawFn, *, x: str) -> typing.Literal[False]: ...
219+
220+
@st.composite
221+
def overloaded(draw: st.DrawFn, *, x: typing.Union[int, str]) -> bool:
222+
return draw(st.just(isinstance(x, int)))

0 commit comments

Comments
 (0)