Debian Codegraph @

hypothesis-python/tests/nocover/test_type_lookup.py

raw
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 collections.abc import Callable, Collection, Sequence
12 from typing import Concatenate, ParamSpec
13
14 import pytest
15
16 from hypothesis import strategies as st
17 from hypothesis.errors import InvalidArgument
18 from hypothesis.strategies._internal.types import NON_RUNTIME_TYPES
19
20 from tests.common.debug import (
21 assert_simple_property,
22 check_can_generate_examples,
23 find_any,
24 )
25
26 try:
27 from typing import TypeGuard # new in 3.10
28 except ImportError:
29 TypeGuard = None
30
31 try:
32 from typing import TypeIs # new in 3.13
33 except ImportError:
34 TypeIs = None
35
36
37 @pytest.mark.parametrize("non_runtime_type", NON_RUNTIME_TYPES)
38 def test_non_runtime_type_cannot_be_resolved(non_runtime_type):
39 strategy = st.from_type(non_runtime_type)
40 with pytest.raises(
41 InvalidArgument, match="there is no such thing as a runtime instance"
42 ):
43 check_can_generate_examples(strategy)
44
45
46 @pytest.mark.parametrize("non_runtime_type", NON_RUNTIME_TYPES)
47 def test_non_runtime_type_cannot_be_registered(non_runtime_type):
48 with pytest.raises(
49 InvalidArgument, match="there is no such thing as a runtime instance"
50 ):
51 st.register_type_strategy(non_runtime_type, st.none())
52
53
54 @pytest.mark.skipif(Concatenate is None, reason="requires python3.10 or higher")
55 def test_callable_with_concatenate():
56 P = ParamSpec("P")
57 func_type = Callable[Concatenate[int, P], None]
58 strategy = st.from_type(func_type)
59 with pytest.raises(
60 InvalidArgument,
61 match="Hypothesis can't yet construct a strategy for instances of a Callable type",
62 ):
63 check_can_generate_examples(strategy)
64
65 with pytest.raises(InvalidArgument, match="Cannot register generic type"):
66 st.register_type_strategy(func_type, st.none())
67
68
69 @pytest.mark.skipif(ParamSpec is None, reason="requires python3.10 or higher")
70 def test_callable_with_paramspec():
71 P = ParamSpec("P")
72 func_type = Callable[P, None]
73 strategy = st.from_type(func_type)
74 with pytest.raises(
75 InvalidArgument,
76 match="Hypothesis can't yet construct a strategy for instances of a Callable type",
77 ):
78 check_can_generate_examples(strategy)
79
80 with pytest.raises(InvalidArgument, match="Cannot register generic type"):
81 st.register_type_strategy(func_type, st.none())
82
83
84 @pytest.mark.parametrize("typ", [TypeGuard, TypeIs])
85 def test_callable_return_typegard_type(typ):
86 if typ is None:
87 pytest.skip("Requires modern typing")
88
89 strategy = st.from_type(Callable[[], typ[int]])
90 with pytest.raises(
91 InvalidArgument,
92 match="Hypothesis cannot yet construct a strategy for callables "
93 "which are PEP-647 TypeGuards or PEP-742 TypeIs",
94 ):
95 check_can_generate_examples(strategy)
96
97 with pytest.raises(InvalidArgument, match="Cannot register generic type"):
98 st.register_type_strategy(Callable[[], typ[int]], st.none())
99
100
101 def test_binary_type_resolution():
102 # we have special logic for this, see
103 # https://github.com/HypothesisWorks/hypothesis/pull/4490
104 find_any(st.from_type(Collection[int]), lambda v: isinstance(v, bytes))
105 find_any(st.from_type(Sequence[object]), lambda v: isinstance(v, bytes))
106 find_any(st.from_type(Sequence[int]), lambda v: isinstance(v, bytes))
107
108 assert_simple_property(st.from_type(type[int]), lambda v: not isinstance(v, bytes))
109 assert_simple_property(st.from_type(set[int]), lambda v: not isinstance(v, bytes))