Skip to content

Commit

Permalink
Tests for attrs inference
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed May 16, 2018
1 parent 2e065e0 commit ddd7254
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
68 changes: 68 additions & 0 deletions hypothesis-python/tests/cover/test_attrs_inference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# coding=utf-8
#
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis-python
#
# Most of this work is copyright (C) 2013-2018 David R. MacIver
# (david@drmaciver.com), but it contains contributions by others. See
# CONTRIBUTING.rst for a full list of people who may hold copyright, and
# consult the git log if you need to determine who owns an individual
# contribution.
#
# 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 http://mozilla.org/MPL/2.0/.
#
# END HEADER

from __future__ import division, print_function, absolute_import

import attr
import pytest

import hypothesis.strategies as st
from hypothesis import given
from hypothesis.errors import ResolutionFailed


@attr.s
class Inferrables(object):
type_ = attr.ib(type=int)
type_converter = attr.ib(converter=bool)
validator_type = attr.ib(validator=attr.validators.instance_of(str))
validator_type_multiple = attr.ib(validator=[
attr.validators.instance_of(str),
attr.validators.instance_of((str, int, bool)),
])
validator_optional = attr.ib(
validator=attr.validators.optional(lambda inst, atrib, val: float(val))
)
validator_in = attr.ib(validator=attr.validators.in_([1, 2, 3]))
validator_in_multiple = attr.ib(validator=[
attr.validators.in_(list(range(100))),
attr.validators.in_([1, -1]),
])
has_default = attr.ib(default=0)
has_default_factory = attr.ib(default=attr.Factory(list))
has_default_factory_takes_self = attr.ib( # uninferrable but has default
default=attr.Factory(lambda _: list(), takes_self=True))


@attr.s
class Required(object):
a = attr.ib()


@given(st.builds(Inferrables))
def test_attrs_inference_builds(c):
pass


@given(st.from_type(Inferrables))
def test_attrs_inference_from_type(c):
pass


def test_cannot_infer_required():
with pytest.raises(ResolutionFailed):
st.builds(Required).example()
20 changes: 20 additions & 0 deletions hypothesis-python/tests/py3/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from __future__ import division, print_function, absolute_import

import attr
import pytest

import hypothesis.strategies as st
Expand Down Expand Up @@ -121,3 +122,22 @@ def test_given_edits_annotations(nargs):
given(*(nargs * [st.none()]))(pointless_composite))
assert spec_given.annotations.pop('return') is None
assert len(spec_given.annotations) == 3 - nargs


def a_converter(x) -> int:
return int(x)


@attr.s
class Inferrables(object):
annot_converter = attr.ib(converter=a_converter)


@given(st.builds(Inferrables))
def test_attrs_inference_builds(c):
pass


@given(st.from_type(Inferrables))
def test_attrs_inference_from_type(c):
pass

0 comments on commit ddd7254

Please sign in to comment.