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