From 80ba027d798cadd8396074f8158a0abe36898f54 Mon Sep 17 00:00:00 2001 From: Tzu-Wei Sung Date: Mon, 3 Aug 2020 23:27:59 -0700 Subject: [PATCH 1/4] Enable doctest --- pytest.ini | 1 + tensorflow_addons/activations/hardshrink.py | 6 ++++++ tensorflow_addons/conftest.py | 13 +++++++++++++ tensorflow_addons/utils/test_utils.py | 7 ++++++- tools/docker/cpu_tests.Dockerfile | 3 ++- 5 files changed, 28 insertions(+), 2 deletions(-) diff --git a/pytest.ini b/pytest.ini index 3591f3c561..dce9fc2445 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,2 +1,3 @@ [pytest] addopts = -ra +doctest_optionflags = ELLIPSIS NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL DONT_ACCEPT_BLANKLINE \ No newline at end of file diff --git a/tensorflow_addons/activations/hardshrink.py b/tensorflow_addons/activations/hardshrink.py index 1d241de531..99c9d5f65a 100644 --- a/tensorflow_addons/activations/hardshrink.py +++ b/tensorflow_addons/activations/hardshrink.py @@ -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) + + Args: x: A `Tensor`. Must be one of the following types: `float16`, `float32`, `float64`. diff --git a/tensorflow_addons/conftest.py b/tensorflow_addons/conftest.py index 8aa24adfdf..c62e400a69 100644 --- a/tensorflow_addons/conftest.py +++ b/tensorflow_addons/conftest.py @@ -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, @@ -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 \ No newline at end of file diff --git a/tensorflow_addons/utils/test_utils.py b/tensorflow_addons/utils/test_utils.py index a6e67b4202..1c63f648fa 100644 --- a/tensorflow_addons/utils/test_utils.py +++ b/tensorflow_addons/utils/test_utils.py @@ -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: + # 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: diff --git a/tools/docker/cpu_tests.Dockerfile b/tools/docker/cpu_tests.Dockerfile index c25d724a08..cbbd67dd59 100644 --- a/tools/docker/cpu_tests.Dockerfile +++ b/tools/docker/cpu_tests.Dockerfile @@ -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/ RUN bazel build --enable_runfiles build_pip_pkg RUN bazel-bin/build_pip_pkg artifacts From bcb9f12f001abcb103d6e666c6b20a87f9bc8475 Mon Sep 17 00:00:00 2001 From: Tzu-Wei Sung Date: Mon, 3 Aug 2020 23:29:13 -0700 Subject: [PATCH 2/4] Empty line --- pytest.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytest.ini b/pytest.ini index dce9fc2445..d3a746f592 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,3 +1,3 @@ [pytest] addopts = -ra -doctest_optionflags = ELLIPSIS NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL DONT_ACCEPT_BLANKLINE \ No newline at end of file +doctest_optionflags = ELLIPSIS NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL DONT_ACCEPT_BLANKLINE From 54521f6780f21b4297530e24416196f015f5ffac Mon Sep 17 00:00:00 2001 From: Tzu-Wei Sung Date: Mon, 3 Aug 2020 23:32:00 -0700 Subject: [PATCH 3/4] Run flake8 --- tensorflow_addons/conftest.py | 2 +- tensorflow_addons/utils/test_utils.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/conftest.py b/tensorflow_addons/conftest.py index c62e400a69..3290bafaec 100644 --- a/tensorflow_addons/conftest.py +++ b/tensorflow_addons/conftest.py @@ -29,4 +29,4 @@ def add_doctest_namespace(doctest_namespace): doctest_namespace["np"] = np doctest_namespace["tf"] = tf - doctest_namespace["tfa"] = tfa \ No newline at end of file + doctest_namespace["tfa"] = tfa diff --git a/tensorflow_addons/utils/test_utils.py b/tensorflow_addons/utils/test_utils.py index 1c63f648fa..cdb33bcf82 100644 --- a/tensorflow_addons/utils/test_utils.py +++ b/tensorflow_addons/utils/test_utils.py @@ -168,7 +168,7 @@ def pytest_configure(config): def device(request): try: requested_device = request.param - except: + except Exception: # workaround for DocTestItem # https://github.com/pytest-dev/pytest/issues/5070 requested_device = "no_device" From 203a52f7e025af03bf337b5a34d210f137e6a3c0 Mon Sep 17 00:00:00 2001 From: Tzu-Wei Sung Date: Tue, 4 Aug 2020 19:40:41 -0700 Subject: [PATCH 4/4] Fix GeometricMean docstring --- tensorflow_addons/metrics/geometric_mean.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/geometric_mean.py b/tensorflow_addons/metrics/geometric_mean.py index 12dfb95ea2..bce8e15509 100644 --- a/tensorflow_addons/metrics/geometric_mean.py +++ b/tensorflow_addons/metrics/geometric_mean.py @@ -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