Skip to content

Commit 4b50b94

Browse files
authoredAug 7, 2020
Enable doctest (#2069)
* Enable doctest * Fix GeometricMean docstring
1 parent b072836 commit 4b50b94

File tree

6 files changed

+30
-4
lines changed

6 files changed

+30
-4
lines changed
 

‎pytest.ini

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[pytest]
22
addopts = -ra
3+
doctest_optionflags = ELLIPSIS NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL DONT_ACCEPT_BLANKLINE

‎tensorflow_addons/activations/hardshrink.py

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ def hardshrink(
3333
Computes hard shrink function:
3434
`x if x < lower or x > upper else 0`.
3535
36+
Usage:
37+
38+
>>> x = tf.constant([1.0, 0.0, 1.0])
39+
>>> tfa.activations.hardshrink(x)
40+
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([1., 0., 1.], dtype=float32)>
41+
3642
Args:
3743
x: A `Tensor`. Must be one of the following types:
3844
`float16`, `float32`, `float64`.

‎tensorflow_addons/conftest.py

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import numpy as np
2+
import pytest
3+
import tensorflow as tf
4+
5+
import tensorflow_addons as tfa
6+
17
from tensorflow_addons.utils.test_utils import ( # noqa: F401
28
maybe_run_functions_eagerly,
39
only_run_functions_eagerly,
@@ -17,3 +23,10 @@
1723
# fixtures present in this file will be available
1824
# when running tests and can be referenced with strings
1925
# https://docs.pytest.org/en/latest/fixture.html#conftest-py-sharing-fixture-functions
26+
27+
28+
@pytest.fixture(autouse=True)
29+
def add_doctest_namespace(doctest_namespace):
30+
doctest_namespace["np"] = np
31+
doctest_namespace["tf"] = tf
32+
doctest_namespace["tfa"] = tfa

‎tensorflow_addons/metrics/geometric_mean.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ class GeometricMean(Metric):
3434
Note: `tfa.metrics.GeometricMean` can be used the same as `tf.keras.metrics.Mean`
3535
3636
Usage:
37-
```python
37+
3838
>>> m = tfa.metrics.GeometricMean()
3939
>>> m.update_state([1, 3, 5, 7, 9])
4040
>>> m.result().numpy()
4141
3.9362833
42-
```
42+
4343
"""
4444

4545
@typechecked

‎tensorflow_addons/utils/test_utils.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,12 @@ def pytest_configure(config):
166166

167167
@pytest.fixture(autouse=True, scope="function")
168168
def device(request):
169-
requested_device = request.param
169+
try:
170+
requested_device = request.param
171+
except Exception:
172+
# workaround for DocTestItem
173+
# https://github.com/pytest-dev/pytest/issues/5070
174+
requested_device = "no_device"
170175
if requested_device == "no_device":
171176
yield requested_device
172177
elif requested_device == tf.distribute.MirroredStrategy:

‎tools/docker/cpu_tests.Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ RUN python configure.py
2020
RUN pip install -e ./
2121
RUN --mount=type=cache,id=cache_bazel,target=/root/.cache/bazel \
2222
bash tools/install_so_files.sh
23-
RUN pytest -v -n auto --durations=25 --cov=tensorflow_addons ./tensorflow_addons/
23+
RUN pytest -v -n auto --durations=25 --doctest-modules ./tensorflow_addons \
24+
--cov=tensorflow_addons ./tensorflow_addons/
2425

2526
RUN bazel build --enable_runfiles build_pip_pkg
2627
RUN bazel-bin/build_pip_pkg artifacts

0 commit comments

Comments
 (0)
Please sign in to comment.