From 1560b225d13fe19a25395061390bbd5a74961397 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sun, 1 Dec 2024 20:21:44 +0300 Subject: [PATCH 1/2] Fix `from_type` failure on `collections.abc.Callable[..., None]` --- hypothesis-python/RELEASE.rst | 4 ++++ .../src/hypothesis/strategies/_internal/types.py | 3 +++ hypothesis-python/tests/cover/test_lookup_py39.py | 7 +++++++ 3 files changed, 14 insertions(+) create mode 100644 hypothesis-python/RELEASE.rst diff --git a/hypothesis-python/RELEASE.rst b/hypothesis-python/RELEASE.rst new file mode 100644 index 0000000000..5fdf4be46f --- /dev/null +++ b/hypothesis-python/RELEASE.rst @@ -0,0 +1,4 @@ +RELEASE_TYPE: patch + +Fix :func:`~hypothesis.strategies.from_type` +on :class:`collections.abc.Callable` returning :const:`None`. diff --git a/hypothesis-python/src/hypothesis/strategies/_internal/types.py b/hypothesis-python/src/hypothesis/strategies/_internal/types.py index 4fd4829136..3c736bf3be 100644 --- a/hypothesis-python/src/hypothesis/strategies/_internal/types.py +++ b/hypothesis-python/src/hypothesis/strategies/_internal/types.py @@ -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), diff --git a/hypothesis-python/tests/cover/test_lookup_py39.py b/hypothesis-python/tests/cover/test_lookup_py39.py index 3181850c8e..6b7ade8dd0 100644 --- a/hypothesis-python/tests/cover/test_lookup_py39.py +++ b/hypothesis-python/tests/cover/test_lookup_py39.py @@ -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 @@ -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) From e4c42481957eed1f272b1e655328100500509170 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sun, 1 Dec 2024 22:13:33 +0300 Subject: [PATCH 2/2] Update hypothesis-python/RELEASE.rst Co-authored-by: Zac Hatfield-Dodds --- hypothesis-python/RELEASE.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hypothesis-python/RELEASE.rst b/hypothesis-python/RELEASE.rst index 5fdf4be46f..59b06a0af7 100644 --- a/hypothesis-python/RELEASE.rst +++ b/hypothesis-python/RELEASE.rst @@ -1,4 +1,4 @@ RELEASE_TYPE: patch Fix :func:`~hypothesis.strategies.from_type` -on :class:`collections.abc.Callable` returning :const:`None`. +on :class:`collections.abc.Callable` returning ``None``.