Skip to content

Commit

Permalink
Merge pull request #4193 from HypothesisWorks/issue-4192
Browse files Browse the repository at this point in the history
Fix `from_type` failure on `collections.abc.Callable[..., None]`
  • Loading branch information
Zac-HD authored Dec 1, 2024
2 parents fa0d158 + e4c4248 commit 40d943c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RELEASE_TYPE: patch

Fix :func:`~hypothesis.strategies.from_type`
on :class:`collections.abc.Callable` returning ``None``.
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,9 @@ def resolve_Callable(thing):
"Consider using an explicit strategy, or opening an issue."
)

if get_origin(thing) is collections.abc.Callable and return_type is None:
return_type = type(None)

return st.functions(
like=(lambda *a, **k: None) if args_types else (lambda: None),
returns=st.from_type(return_type),
Expand Down
7 changes: 7 additions & 0 deletions hypothesis-python/tests/cover/test_lookup_py39.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.

import collections.abc
import dataclasses
import sys
import typing
Expand Down Expand Up @@ -174,3 +175,9 @@ def __iter__(self):
@given(...)
def test_grouped_protocol_strategy(x: typing.Annotated[int, LazyStrategyAnnotation()]):
assert x is sentinel


def test_collections_abc_callable_none():
# https://github.com/HypothesisWorks/hypothesis/issues/4192
s = st.from_type(collections.abc.Callable[[None], None])
assert_all_examples(s, lambda x: callable(x) and x(None) is None)

0 comments on commit 40d943c

Please sign in to comment.