Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable doctest #2069

Merged
merged 5 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[pytest]
addopts = -ra
doctest_optionflags = ELLIPSIS NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL DONT_ACCEPT_BLANKLINE
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6 changes: 6 additions & 0 deletions tensorflow_addons/activations/hardshrink.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ def hardshrink(
Computes hard shrink function:
`x if x < lower or x > upper else 0`.

Usage:

>>> x = tf.constant([1.0, 0.0, 1.0])
>>> tfa.activations.hardshrink(x)
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([1., 0., 1.], dtype=float32)>

Args:
x: A `Tensor`. Must be one of the following types:
`float16`, `float32`, `float64`.
Expand Down
13 changes: 13 additions & 0 deletions tensorflow_addons/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import numpy as np
import pytest
import tensorflow as tf

import tensorflow_addons as tfa

from tensorflow_addons.utils.test_utils import ( # noqa: F401
maybe_run_functions_eagerly,
only_run_functions_eagerly,
Expand All @@ -17,3 +23,10 @@
# fixtures present in this file will be available
# when running tests and can be referenced with strings
# https://docs.pytest.org/en/latest/fixture.html#conftest-py-sharing-fixture-functions


@pytest.fixture(autouse=True)
def add_doctest_namespace(doctest_namespace):
doctest_namespace["np"] = np
doctest_namespace["tf"] = tf
doctest_namespace["tfa"] = tfa
4 changes: 2 additions & 2 deletions tensorflow_addons/metrics/geometric_mean.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ class GeometricMean(Metric):
Note: `tfa.metrics.GeometricMean` can be used the same as `tf.keras.metrics.Mean`

Usage:
```python

>>> m = tfa.metrics.GeometricMean()
>>> m.update_state([1, 3, 5, 7, 9])
>>> m.result().numpy()
3.9362833
```

"""

@typechecked
Expand Down
7 changes: 6 additions & 1 deletion tensorflow_addons/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,12 @@ def pytest_configure(config):

@pytest.fixture(autouse=True, scope="function")
def device(request):
requested_device = request.param
try:
requested_device = request.param
except Exception:
# workaround for DocTestItem
# https://github.com/pytest-dev/pytest/issues/5070
requested_device = "no_device"
if requested_device == "no_device":
yield requested_device
elif requested_device == tf.distribute.MirroredStrategy:
Expand Down
3 changes: 2 additions & 1 deletion tools/docker/cpu_tests.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ RUN python configure.py
RUN pip install -e ./
RUN --mount=type=cache,id=cache_bazel,target=/root/.cache/bazel \
bash tools/install_so_files.sh
RUN pytest -v -n auto --durations=25 --cov=tensorflow_addons ./tensorflow_addons/
RUN pytest -v -n auto --durations=25 --doctest-modules ./tensorflow_addons \
--cov=tensorflow_addons ./tensorflow_addons/
Copy link
Member Author

@WindQAQ WindQAQ Aug 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to also run doctest in cpu_tests as docstring is also a part of codes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree


RUN bazel build --enable_runfiles build_pip_pkg
RUN bazel-bin/build_pip_pkg artifacts
Expand Down