| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import collections.abc |
| 12 |
import dataclasses |
| 13 |
import sys |
| 14 |
import typing |
| 15 |
|
| 16 |
import pytest |
| 17 |
|
| 18 |
from hypothesis import given, strategies as st |
| 19 |
from hypothesis.errors import InvalidArgument |
| 20 |
|
| 21 |
from tests.common.debug import ( |
| 22 |
assert_all_examples, |
| 23 |
assert_simple_property, |
| 24 |
check_can_generate_examples, |
| 25 |
find_any, |
| 26 |
) |
| 27 |
from tests.common.utils import temp_registered |
| 28 |
|
| 29 |
# Union[A, B] is not equivalent to A | B until 3.14. We'll continue to test both |
| 30 |
# until then. |
| 31 |
# ruff: noqa: UP007 |
| 32 |
|
| 33 |
|
| 34 |
@pytest.mark.parametrize( |
| 35 |
"annotated_type,expected_strategy_repr", |
| 36 |
[ |
| 37 |
(typing.Annotated[int, "foo"], "integers()"), |
| 38 |
(typing.Annotated[list[float], "foo"], "lists(floats())"), |
| 39 |
(typing.Annotated[typing.Annotated[str, "foo"], "bar"], "text()"), |
| 40 |
( |
| 41 |
typing.Annotated[typing.Annotated[list[dict[str, bool]], "foo"], "bar"], |
| 42 |
"lists(dictionaries(keys=text(), values=booleans()))", |
| 43 |
), |
| 44 |
], |
| 45 |
) |
| 46 |
def test_typing_Annotated(annotated_type, expected_strategy_repr): |
| 47 |
assert repr(st.from_type(annotated_type)) == expected_strategy_repr |
| 48 |
|
| 49 |
|
| 50 |
PositiveInt = typing.Annotated[int, st.integers(min_value=1)] |
| 51 |
MoreThanTenInt = typing.Annotated[PositiveInt, st.integers(min_value=10 + 1)] |
| 52 |
WithTwoStrategies = typing.Annotated[int, st.integers(), st.none()] |
| 53 |
ExtraAnnotationNoStrategy = typing.Annotated[PositiveInt, "metadata"] |
| 54 |
|
| 55 |
|
| 56 |
def arg_positive(x: PositiveInt): |
| 57 |
assert x > 0 |
| 58 |
|
| 59 |
|
| 60 |
def arg_more_than_ten(x: MoreThanTenInt): |
| 61 |
assert x > 10 |
| 62 |
|
| 63 |
|
| 64 |
@given(st.data()) |
| 65 |
def test_annotated_positive_int(data): |
| 66 |
data.draw(st.builds(arg_positive)) |
| 67 |
|
| 68 |
|
| 69 |
@given(st.data()) |
| 70 |
def test_annotated_more_than_ten(data): |
| 71 |
data.draw(st.builds(arg_more_than_ten)) |
| 72 |
|
| 73 |
|
| 74 |
@given(st.data()) |
| 75 |
def test_annotated_with_two_strategies(data): |
| 76 |
assert data.draw(st.from_type(WithTwoStrategies)) is None |
| 77 |
|
| 78 |
|
| 79 |
@given(st.data()) |
| 80 |
def test_annotated_extra_metadata(data): |
| 81 |
assert data.draw(st.from_type(ExtraAnnotationNoStrategy)) > 0 |
| 82 |
|
| 83 |
|
| 84 |
@dataclasses.dataclass |
| 85 |
class User: |
| 86 |
id: int |
| 87 |
following: list["User"] # works with typing.List |
| 88 |
|
| 89 |
|
| 90 |
@pytest.mark.skipif(sys.version_info[:2] >= (3, 11), reason="works in new Pythons") |
| 91 |
def test_string_forward_ref_message(): |
| 92 |
# See https://github.com/HypothesisWorks/hypothesis/issues/3016 |
| 93 |
s = st.builds(User) |
| 94 |
with pytest.raises(InvalidArgument, match="`from __future__ import annotations`"): |
| 95 |
check_can_generate_examples(s) |
| 96 |
|
| 97 |
|
| 98 |
@pytest.mark.parametrize("typ", (typing.Union[list[int], int], list[int] | int)) |
| 99 |
def test_issue_3080(typ): |
| 100 |
# Check for https://github.com/HypothesisWorks/hypothesis/issues/3080 |
| 101 |
s = st.from_type(typ) |
| 102 |
find_any(s, lambda x: isinstance(x, int)) |
| 103 |
find_any(s, lambda x: isinstance(x, list)) |
| 104 |
|
| 105 |
|
| 106 |
@dataclasses.dataclass |
| 107 |
class TypingTuple: |
| 108 |
a: dict[tuple[int, int], str] |
| 109 |
|
| 110 |
|
| 111 |
@dataclasses.dataclass |
| 112 |
class BuiltinTuple: |
| 113 |
a: dict[tuple[int, int], str] |
| 114 |
|
| 115 |
|
| 116 |
TestDataClass = typing.Union[TypingTuple, BuiltinTuple] |
| 117 |
|
| 118 |
|
| 119 |
@pytest.mark.parametrize("data_class", [TypingTuple, BuiltinTuple]) |
| 120 |
@given(data=st.data()) |
| 121 |
def test_from_type_with_tuple_works(data, data_class: TestDataClass): |
| 122 |
value: TestDataClass = data.draw(st.from_type(data_class)) |
| 123 |
assert len(value.a) >= 0 |
| 124 |
|
| 125 |
|
| 126 |
def _shorter_lists(list_type): |
| 127 |
return st.lists(st.from_type(*typing.get_args(list_type)), max_size=2) |
| 128 |
|
| 129 |
|
| 130 |
def test_can_register_builtin_list(): |
| 131 |
# Regression test for https://github.com/HypothesisWorks/hypothesis/issues/3635 |
| 132 |
with temp_registered(list, _shorter_lists): |
| 133 |
assert_all_examples( |
| 134 |
st.from_type(list[int]), |
| 135 |
lambda ls: len(ls) <= 2 and {type(x) for x in ls}.issubset({int}), |
| 136 |
) |
| 137 |
|
| 138 |
|
| 139 |
T = typing.TypeVar("T") |
| 140 |
|
| 141 |
|
| 142 |
@typing.runtime_checkable |
| 143 |
class Fooable(typing.Protocol[T]): |
| 144 |
def foo(self): ... |
| 145 |
|
| 146 |
|
| 147 |
class FooableConcrete(tuple): |
| 148 |
def foo(self): |
| 149 |
pass |
| 150 |
|
| 151 |
|
| 152 |
def test_only_tuple_subclasses_in_typing_type(): |
| 153 |
# A generic typing type (such as Fooable) whose only concrete |
| 154 |
# instantiations are tuples should still generate tuples. This is in |
| 155 |
# contrast to test_tuple_subclasses_not_generic_sequences, which discards |
| 156 |
# tuples if there are any alternatives. |
| 157 |
with temp_registered(FooableConcrete, st.builds(FooableConcrete)): |
| 158 |
s = st.from_type(Fooable[int]) |
| 159 |
assert_all_examples(s, lambda x: type(x) is FooableConcrete) |
| 160 |
|
| 161 |
|
| 162 |
def test_lookup_registered_tuple(): |
| 163 |
sentinel = object() |
| 164 |
typ = tuple[int] |
| 165 |
with temp_registered(tuple, st.just(sentinel)): |
| 166 |
assert_simple_property(st.from_type(typ), lambda v: v is sentinel) |
| 167 |
assert_simple_property(st.from_type(typ), lambda v: v is not sentinel) |
| 168 |
|
| 169 |
|
| 170 |
sentinel = object() |
| 171 |
|
| 172 |
|
| 173 |
class LazyStrategyAnnotation: |
| 174 |
__is_annotated_types_grouped_metadata__ = True |
| 175 |
|
| 176 |
def __iter__(self): |
| 177 |
return iter([st.just(sentinel)]) |
| 178 |
|
| 179 |
|
| 180 |
@given(...) |
| 181 |
def test_grouped_protocol_strategy(x: typing.Annotated[int, LazyStrategyAnnotation()]): |
| 182 |
assert x is sentinel |
| 183 |
|
| 184 |
|
| 185 |
def test_collections_abc_callable_none(): |
| 186 |
# https://github.com/HypothesisWorks/hypothesis/issues/4192 |
| 187 |
s = st.from_type(collections.abc.Callable[[None], None]) |
| 188 |
assert_all_examples(s, lambda x: callable(x) and x(None) is None) |