| 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 inspect |
| 12 |
from inspect import Parameter |
| 13 |
|
| 14 |
import pytest |
| 15 |
|
| 16 |
from hypothesis import strategies as st |
| 17 |
from hypothesis.strategies._internal.utils import _all_strategies |
| 18 |
|
| 19 |
from tests.common.arguments import argument_validation_test, e |
| 20 |
|
| 21 |
BAD_ARGS = [] |
| 22 |
|
| 23 |
|
| 24 |
def adjust(ex, **kwargs): |
| 25 |
f, a, b = ex |
| 26 |
b = dict(b) |
| 27 |
b.update(kwargs) |
| 28 |
BAD_ARGS.append((f, a, b)) |
| 29 |
|
| 30 |
|
| 31 |
for ex in [ |
| 32 |
e(st.lists, st.integers()), |
| 33 |
e(st.sets, st.integers()), |
| 34 |
e(st.frozensets, st.integers()), |
| 35 |
e(st.dictionaries, st.integers(), st.integers()), |
| 36 |
e(st.text), |
| 37 |
e(st.binary), |
| 38 |
]: |
| 39 |
adjust(ex, min_size=-1) |
| 40 |
adjust(ex, max_size=-1) |
| 41 |
adjust(ex, min_size="no") |
| 42 |
adjust(ex, max_size="no") |
| 43 |
|
| 44 |
|
| 45 |
BAD_ARGS.extend([e(st.lists, st.nothing(), unique=True, min_size=1)]) |
| 46 |
|
| 47 |
test_raise_invalid_argument = argument_validation_test(BAD_ARGS) |
| 48 |
|
| 49 |
|
| 50 |
ALL_STRATEGIES_COPY = dict(_all_strategies) # avoid rare weakref issues |
| 51 |
|
| 52 |
|
| 53 |
@pytest.mark.parametrize("name", sorted(ALL_STRATEGIES_COPY)) |
| 54 |
def test_consistent_with_api_guide_on_kwonly_args(name): |
| 55 |
# Enforce our style-guide: if it has a default value, it should be |
| 56 |
# keyword-only, with a few exceptions. |
| 57 |
strategy = ALL_STRATEGIES_COPY[name] |
| 58 |
for arg in inspect.signature(strategy).parameters.values(): |
| 59 |
assert ( |
| 60 |
arg.default == Parameter.empty |
| 61 |
or arg.kind != Parameter.POSITIONAL_OR_KEYWORD |
| 62 |
or arg.name in ("min_value", "max_value", "subtype_strategy", "columns") |
| 63 |
or name in ("text", "range_indexes", "badly_draw_lists", "write_pattern") |
| 64 |
), f"need kwonly args in {name}" |