| 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 typing |
| 12 |
|
| 13 |
import attr |
| 14 |
import pytest |
| 15 |
|
| 16 |
from hypothesis import given, strategies as st |
| 17 |
from hypothesis.errors import ResolutionFailed |
| 18 |
|
| 19 |
from tests.common.debug import check_can_generate_examples |
| 20 |
|
| 21 |
# Union[A, B] is not equivalent to A | B until 3.14. We'll continue to test both |
| 22 |
# until then. |
| 23 |
# ruff: noqa: UP045, UP007 |
| 24 |
|
| 25 |
|
| 26 |
@attr.s |
| 27 |
class Inferrables: |
| 28 |
type_ = attr.ib(type=int) |
| 29 |
type_converter = attr.ib(converter=bool) |
| 30 |
validator_type = attr.ib(validator=attr.validators.instance_of(str)) |
| 31 |
validator_type_tuple = attr.ib(validator=attr.validators.instance_of((str, int))) |
| 32 |
validator_type_multiple = attr.ib( |
| 33 |
validator=[ |
| 34 |
attr.validators.instance_of(str), |
| 35 |
attr.validators.instance_of((str, int, bool)), |
| 36 |
] |
| 37 |
) |
| 38 |
validator_type_has_overlap = attr.ib( |
| 39 |
validator=[ |
| 40 |
attr.validators.instance_of(str), |
| 41 |
attr.validators.instance_of((str, list)), |
| 42 |
attr.validators.instance_of(object), |
| 43 |
] |
| 44 |
) |
| 45 |
validator_optional = attr.ib( |
| 46 |
validator=attr.validators.optional(lambda inst, atrib, val: float(val)) |
| 47 |
) |
| 48 |
validator_in = attr.ib(validator=attr.validators.in_([1, 2, 3])) |
| 49 |
validator_in_multiple = attr.ib( |
| 50 |
validator=[attr.validators.in_(list(range(100))), attr.validators.in_([1, -1])] |
| 51 |
) |
| 52 |
validator_in_multiple_strings = attr.ib( |
| 53 |
validator=[attr.validators.in_("abcd"), attr.validators.in_(["ab", "cd"])] |
| 54 |
) |
| 55 |
|
| 56 |
typing_list = attr.ib(type=list[int]) |
| 57 |
typing_list_of_list = attr.ib(type=list[list[int]]) |
| 58 |
typing_dict = attr.ib(type=dict[str, int]) |
| 59 |
typing_optional = attr.ib(type=typing.Optional[bool]) |
| 60 |
typing_optional_new = attr.ib(type=bool | None) |
| 61 |
typing_union = attr.ib(type=typing.Union[str, int]) |
| 62 |
typing_union_new = attr.ib(type=str | int) |
| 63 |
|
| 64 |
has_default = attr.ib(default=0) |
| 65 |
has_default_factory = attr.ib(default=attr.Factory(list)) |
| 66 |
has_default_factory_takes_self = attr.ib( # uninferrable but has default |
| 67 |
default=attr.Factory(lambda _: [], takes_self=True) |
| 68 |
) |
| 69 |
|
| 70 |
|
| 71 |
@attr.s |
| 72 |
class Required: |
| 73 |
a = attr.ib() |
| 74 |
|
| 75 |
|
| 76 |
@attr.s |
| 77 |
class UnhelpfulConverter: |
| 78 |
a = attr.ib(converter=lambda x: x) |
| 79 |
|
| 80 |
|
| 81 |
@given(st.builds(Inferrables, has_default=..., has_default_factory=...)) |
| 82 |
def test_attrs_inference_builds(c): |
| 83 |
pass |
| 84 |
|
| 85 |
|
| 86 |
@given(st.from_type(Inferrables)) |
| 87 |
def test_attrs_inference_from_type(c): |
| 88 |
pass |
| 89 |
|
| 90 |
|
| 91 |
@pytest.mark.parametrize("c", [Required, UnhelpfulConverter]) |
| 92 |
def test_cannot_infer(c): |
| 93 |
with pytest.raises(ResolutionFailed): |
| 94 |
check_can_generate_examples(st.builds(c)) |
| 95 |
|
| 96 |
|
| 97 |
def test_cannot_infer_takes_self(): |
| 98 |
with pytest.raises(ResolutionFailed): |
| 99 |
check_can_generate_examples( |
| 100 |
st.builds(Inferrables, has_default_factory_takes_self=...) |
| 101 |
) |
| 102 |
|
| 103 |
|
| 104 |
@attr.s |
| 105 |
class HasPrivateAttribute: |
| 106 |
_x: int = attr.ib() |
| 107 |
|
| 108 |
|
| 109 |
@pytest.mark.parametrize("s", [st.just(42)]) |
| 110 |
def test_private_attribute(s): |
| 111 |
check_can_generate_examples(st.builds(HasPrivateAttribute, x=s)) |
| 112 |
|
| 113 |
|
| 114 |
def test_private_attribute_underscore_fails(): |
| 115 |
with pytest.raises(TypeError, match="unexpected keyword argument '_x'"): |
| 116 |
check_can_generate_examples(st.builds(HasPrivateAttribute, _x=st.just(42))) |
| 117 |
|
| 118 |
|
| 119 |
def test_private_attribute_underscore_infer_fails(): |
| 120 |
# this has a slightly different failure case, because it goes through |
| 121 |
# attrs-specific resolution logic. |
| 122 |
with pytest.raises( |
| 123 |
TypeError, match="Unexpected keyword argument _x for attrs class" |
| 124 |
): |
| 125 |
check_can_generate_examples(st.builds(HasPrivateAttribute, _x=...)) |
| 126 |
|
| 127 |
|
| 128 |
@attr.s |
| 129 |
class HasAliasedAttribute: |
| 130 |
x: int = attr.ib(alias="crazyname") |
| 131 |
|
| 132 |
|
| 133 |
@pytest.mark.parametrize("s", [st.just(42)]) |
| 134 |
def test_aliased_attribute(s): |
| 135 |
check_can_generate_examples(st.builds(HasAliasedAttribute, crazyname=s)) |