| 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 inspect import Parameter as P, signature |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
from hypothesis import given, strategies as st |
| 16 |
from hypothesis.internal.reflection import ( |
| 17 |
convert_positional_arguments, |
| 18 |
define_function_signature, |
| 19 |
get_pretty_function_description, |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
@given(st.integers()) |
| 24 |
def test_has_an_annotation(i: int): |
| 25 |
pass |
| 26 |
|
| 27 |
|
| 28 |
def universal_acceptor(*args, **kwargs): |
| 29 |
return args, kwargs |
| 30 |
|
| 31 |
|
| 32 |
def has_annotation(a: int, *b, c=2) -> None: |
| 33 |
pass |
| 34 |
|
| 35 |
|
| 36 |
@pytest.mark.parametrize("f", [has_annotation, lambda *, a: a, lambda *, a=1: a]) |
| 37 |
def test_copying_preserves_signature(f): |
| 38 |
af = signature(f) |
| 39 |
t = define_function_signature("foo", "docstring", af)(universal_acceptor) |
| 40 |
at = signature(t) |
| 41 |
assert af.parameters == at.parameters |
| 42 |
|
| 43 |
|
| 44 |
@pytest.mark.parametrize( |
| 45 |
"lam,source", |
| 46 |
[ |
| 47 |
((lambda *z, a: a), "lambda *z, a: a"), |
| 48 |
((lambda *z, a=1: a), "lambda *z, a=1: a"), |
| 49 |
((lambda *, a: a), "lambda *, a: a"), |
| 50 |
((lambda *, a=1: a), "lambda *, a=1: a"), |
| 51 |
((lambda **kw: kw), "lambda **kw: kw"), |
| 52 |
], |
| 53 |
) |
| 54 |
def test_kwonly_lambda_formatting(lam, source): |
| 55 |
# Testing kwonly lambdas, with and without varargs and default values |
| 56 |
assert get_pretty_function_description(lam) == source |
| 57 |
|
| 58 |
|
| 59 |
def test_given_notices_missing_kwonly_args(): |
| 60 |
@given(a=st.none()) |
| 61 |
def reqs_kwonly(*, a, b): |
| 62 |
pass |
| 63 |
|
| 64 |
with pytest.raises(TypeError): |
| 65 |
reqs_kwonly() |
| 66 |
reqs_kwonly(b=None) |
| 67 |
|
| 68 |
|
| 69 |
def test_converter_handles_kwonly_args(): |
| 70 |
def f(*, a, b=2): |
| 71 |
pass |
| 72 |
|
| 73 |
out = convert_positional_arguments(f, (), {"a": 1}) |
| 74 |
assert out == ((), {"a": 1}) |
| 75 |
|
| 76 |
|
| 77 |
def test_converter_notices_missing_kwonly_args(): |
| 78 |
def f(*, a, b=2): |
| 79 |
pass |
| 80 |
|
| 81 |
with pytest.raises(TypeError): |
| 82 |
assert convert_positional_arguments(f, (), {}) |
| 83 |
|
| 84 |
|
| 85 |
def to_wrap_with_composite(draw: None, strat: float, nothing: list) -> int: |
| 86 |
return draw(st.none()) |
| 87 |
|
| 88 |
|
| 89 |
def return_annot() -> int: |
| 90 |
return 4 # per RFC 1149.5 / xckd 221 |
| 91 |
|
| 92 |
|
| 93 |
def first_annot(draw: None): |
| 94 |
pass |
| 95 |
|
| 96 |
|
| 97 |
def test_composite_edits_annotations(): |
| 98 |
sig_comp = signature(st.composite(to_wrap_with_composite)) |
| 99 |
assert sig_comp.return_annotation == st.SearchStrategy[int] |
| 100 |
assert sig_comp.parameters["nothing"].annotation is not P.empty |
| 101 |
assert "draw" not in sig_comp.parameters |
| 102 |
|
| 103 |
|
| 104 |
@pytest.mark.parametrize("nargs", [1, 2, 3]) |
| 105 |
def test_given_edits_annotations(nargs): |
| 106 |
sig_given = signature(given(*(nargs * [st.none()]))(to_wrap_with_composite)) |
| 107 |
assert sig_given.return_annotation is None |
| 108 |
assert len(sig_given.parameters) == 3 - nargs |
| 109 |
assert all(p.annotation is not P.empty for p in sig_given.parameters.values()) |