Skip to content

Commit

Permalink
Avoid using attr non-public API (#129)
Browse files Browse the repository at this point in the history
* avoid using attr non-public API

* update release notes

* black
  • Loading branch information
benbovy committed Apr 12, 2020
1 parent 763b31d commit a09c661
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
1 change: 1 addition & 0 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Bug fixes

- Fix running batches of simulations using ``dask.distributed`` (:issue:`124`).
- Fix rendering of auto-generated docstrings of process classes (:issue:`128`).
- Fix tests with ``attr`` v20.1.0 (:issue:`129`).

v0.4.0 (7 April 2020)
---------------------
Expand Down
39 changes: 17 additions & 22 deletions xsimlab/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,17 @@
from xsimlab.validators import in_bounds, is_subdtype


def simple_attr(name):
@pytest.fixture
def simple_attr():
"""
Return an attribute with a name just for testing purpose.
"""
return attr.Attribute(
name=name,
default=attr.NOTHING,
validator=None,
repr=True,
eq=True,
cmp=None,
hash=None,
init=True,
converter=None,
kw_only=False,
)

@attr.attrs
class C:
test = attr.attrib()

return attr.fields_dict(C)["test"]


def test_in_bounds_init():
Expand All @@ -40,11 +35,11 @@ def test_in_bounds_init():
((None, None), 1000),
],
)
def test_in_bounds_success(bounds, value):
def test_in_bounds_success(simple_attr, bounds, value):
v = in_bounds(bounds)

# nothing happens
v(None, simple_attr("test"), value)
v(None, simple_attr, value)


@pytest.mark.parametrize(
Expand All @@ -56,11 +51,11 @@ def test_in_bounds_success(bounds, value):
((0, 5), (False, False), np.array([0, 1, 2, 3, 4, 5])),
],
)
def test_in_bounds_fail(bounds, closed, value):
def test_in_bounds_fail(simple_attr, bounds, closed, value):
v = in_bounds(bounds, closed=closed)

with pytest.raises(ValueError, match=r".*out of bounds.*"):
v(None, simple_attr("test"), value)
v(None, simple_attr, value)


@pytest.mark.parametrize(
Expand All @@ -79,19 +74,19 @@ def test_in_bounds_repr(closed, interval_str):
assert repr(v) == expected


def test_is_subdtype_success():
def test_is_subdtype_success(simple_attr):
v = is_subdtype(np.number)

# nothing happends
v(None, simple_attr("test"), np.array([1, 2, 3]))
v(None, simple_attr("test"), np.array([1.0, 2.0, 3.0]))
v(None, simple_attr, np.array([1, 2, 3]))
v(None, simple_attr, np.array([1.0, 2.0, 3.0]))


def test_is_subdtype_fail():
def test_is_subdtype_fail(simple_attr):
v = is_subdtype(np.number)

with pytest.raises(TypeError, match=r".*not a sub-dtype of.*"):
v(None, simple_attr("test"), np.array(["1", "2", "3"]))
v(None, simple_attr, np.array(["1", "2", "3"]))


def test_is_subdtype_repr():
Expand Down

0 comments on commit a09c661

Please sign in to comment.