| 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 |
from typing import Generic, TypeVar |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
from hypothesis import given, strategies as st |
| 16 |
from hypothesis.errors import ResolutionFailed |
| 17 |
|
| 18 |
from tests.common.debug import check_can_generate_examples, find_any |
| 19 |
from tests.common.utils import temp_registered |
| 20 |
|
| 21 |
# Primitives: |
| 22 |
# =========== |
| 23 |
|
| 24 |
_InstanceType = TypeVar("_InstanceType", covariant=True) |
| 25 |
_TypeArgType1 = TypeVar("_TypeArgType1", covariant=True) |
| 26 |
_FirstType = TypeVar("_FirstType") |
| 27 |
_LawType = TypeVar("_LawType") |
| 28 |
|
| 29 |
|
| 30 |
class KindN(Generic[_InstanceType, _TypeArgType1]): |
| 31 |
pass |
| 32 |
|
| 33 |
|
| 34 |
class Lawful(Generic[_LawType]): |
| 35 |
"""This type defines law-related operations.""" |
| 36 |
|
| 37 |
|
| 38 |
class MappableN(Generic[_FirstType], Lawful["MappableN[_FirstType]"]): |
| 39 |
"""Behaves like a functor.""" |
| 40 |
|
| 41 |
|
| 42 |
# End definition: |
| 43 |
# =============== |
| 44 |
|
| 45 |
_ValueType = TypeVar("_ValueType") |
| 46 |
|
| 47 |
|
| 48 |
class MyFunctor(KindN["MyFunctor", _ValueType], MappableN[_ValueType]): |
| 49 |
def __init__(self, inner_value: _ValueType) -> None: |
| 50 |
self.inner_value = inner_value |
| 51 |
|
| 52 |
|
| 53 |
# Testing part: |
| 54 |
# ============= |
| 55 |
|
| 56 |
|
| 57 |
def target_func(mappable: "MappableN[_FirstType]") -> bool: |
| 58 |
return isinstance(mappable, MappableN) |
| 59 |
|
| 60 |
|
| 61 |
@given(st.data()) |
| 62 |
def test_my_mappable(source: st.DataObject) -> None: |
| 63 |
""" |
| 64 |
Checks that complex types with multiple inheritance levels and strings are fine. |
| 65 |
|
| 66 |
Regression test for https://github.com/HypothesisWorks/hypothesis/issues/3060 |
| 67 |
""" |
| 68 |
# In `returns` we register all types in `__mro__` |
| 69 |
# to be this exact type at the moment. But here, we only need `Mappable`. |
| 70 |
# Current `__mro__` is `MyFunctor / Kind / Mappable`: |
| 71 |
assert MyFunctor.__mro__[2] is MappableN |
| 72 |
with temp_registered( |
| 73 |
MyFunctor.__mro__[2], |
| 74 |
st.builds(MyFunctor), |
| 75 |
): |
| 76 |
assert source.draw(st.builds(target_func)) is True |
| 77 |
|
| 78 |
|
| 79 |
A = TypeVar("A") |
| 80 |
B = TypeVar("B") |
| 81 |
C = TypeVar("C") |
| 82 |
D = TypeVar("D") |
| 83 |
|
| 84 |
|
| 85 |
class _FirstBase(Generic[A, B]): |
| 86 |
pass |
| 87 |
|
| 88 |
|
| 89 |
class _SecondBase(Generic[C, D]): |
| 90 |
pass |
| 91 |
|
| 92 |
|
| 93 |
# To be tested: |
| 94 |
|
| 95 |
|
| 96 |
class TwoGenericBases1(_FirstBase[A, B], _SecondBase[C, D]): |
| 97 |
pass |
| 98 |
|
| 99 |
|
| 100 |
class TwoGenericBases2(_FirstBase[C, D], _SecondBase[A, B]): |
| 101 |
pass |
| 102 |
|
| 103 |
|
| 104 |
class OneGenericOneConrete1(_FirstBase[int, str], _SecondBase[A, B]): |
| 105 |
pass |
| 106 |
|
| 107 |
|
| 108 |
class OneGenericOneConrete2(_FirstBase[A, B], _SecondBase[float, bool]): |
| 109 |
pass |
| 110 |
|
| 111 |
|
| 112 |
class MixedGenerics1(_FirstBase[int, B], _SecondBase[C, bool]): |
| 113 |
pass |
| 114 |
|
| 115 |
|
| 116 |
class MixedGenerics2(_FirstBase[A, str], _SecondBase[float, D]): |
| 117 |
pass |
| 118 |
|
| 119 |
|
| 120 |
class AllConcrete(_FirstBase[int, str], _SecondBase[float, bool]): |
| 121 |
pass |
| 122 |
|
| 123 |
|
| 124 |
_generic_test_types = ( |
| 125 |
TwoGenericBases1, |
| 126 |
TwoGenericBases2, |
| 127 |
OneGenericOneConrete1, |
| 128 |
OneGenericOneConrete2, |
| 129 |
MixedGenerics1, |
| 130 |
MixedGenerics2, |
| 131 |
AllConcrete, |
| 132 |
) |
| 133 |
|
| 134 |
|
| 135 |
@pytest.mark.parametrize("type_", _generic_test_types) |
| 136 |
def test_several_generic_bases(type_): |
| 137 |
with temp_registered(_FirstBase, st.builds(type_)): |
| 138 |
find_any(st.builds(_FirstBase)) |
| 139 |
|
| 140 |
with temp_registered(_SecondBase, st.builds(type_)): |
| 141 |
find_any(st.builds(_SecondBase)) |
| 142 |
|
| 143 |
|
| 144 |
def var_generic_func1(obj: _FirstBase[A, B]): |
| 145 |
pass |
| 146 |
|
| 147 |
|
| 148 |
def var_generic_func2(obj: _SecondBase[A, B]): |
| 149 |
pass |
| 150 |
|
| 151 |
|
| 152 |
def concrete_generic_func1(obj: _FirstBase[int, str]): |
| 153 |
pass |
| 154 |
|
| 155 |
|
| 156 |
def concrete_generic_func2(obj: _SecondBase[float, bool]): |
| 157 |
pass |
| 158 |
|
| 159 |
|
| 160 |
def mixed_generic_func1(obj: _FirstBase[A, str]): |
| 161 |
pass |
| 162 |
|
| 163 |
|
| 164 |
def mixed_generic_func2(obj: _SecondBase[float, D]): |
| 165 |
pass |
| 166 |
|
| 167 |
|
| 168 |
@pytest.mark.parametrize("type_", _generic_test_types) |
| 169 |
@pytest.mark.parametrize( |
| 170 |
"func", |
| 171 |
[ |
| 172 |
var_generic_func1, |
| 173 |
var_generic_func2, |
| 174 |
concrete_generic_func1, |
| 175 |
concrete_generic_func2, |
| 176 |
mixed_generic_func1, |
| 177 |
mixed_generic_func2, |
| 178 |
], |
| 179 |
) |
| 180 |
def test_several_generic_bases_functions(type_, func): |
| 181 |
with ( |
| 182 |
temp_registered(_FirstBase, st.builds(type_)), |
| 183 |
temp_registered(_SecondBase, st.builds(type_)), |
| 184 |
): |
| 185 |
find_any(st.builds(func)) |
| 186 |
|
| 187 |
with temp_registered(type_, st.builds(type_)): |
| 188 |
find_any(st.builds(func)) |
| 189 |
|
| 190 |
|
| 191 |
def wrong_generic_func1(obj: _FirstBase[A, None]): |
| 192 |
pass |
| 193 |
|
| 194 |
|
| 195 |
def wrong_generic_func2(obj: _SecondBase[None, bool]): |
| 196 |
pass |
| 197 |
|
| 198 |
|
| 199 |
@pytest.mark.parametrize("func", [wrong_generic_func1, wrong_generic_func2]) |
| 200 |
def test_several_generic_bases_wrong_functions(func): |
| 201 |
with ( |
| 202 |
temp_registered(AllConcrete, st.builds(AllConcrete)), |
| 203 |
pytest.raises(ResolutionFailed), |
| 204 |
): |
| 205 |
check_can_generate_examples(st.builds(func)) |