From e27620ebeb2c7737444072348fc73e478aeaf862 Mon Sep 17 00:00:00 2001 From: Santiago Soler Date: Wed, 8 Sep 2021 15:23:59 -0300 Subject: [PATCH 1/3] Transform require_numba decorator into a variable The require_numba as a function wrappers makes it hard to use pytest.mark.parametrize on test functions. Following pytest documentation, it's better to define custom decorators as global variables. --- harmonica/tests/utils.py | 45 ++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/harmonica/tests/utils.py b/harmonica/tests/utils.py index e34c18f38..addbad63c 100644 --- a/harmonica/tests/utils.py +++ b/harmonica/tests/utils.py @@ -10,31 +10,22 @@ import os import pytest +# Check if Numba is disabled +# (if NUMBA_DISABLE_JIT is not defined, we assume Numba jit is enabled) +NUMBA_IS_DISABLED = bool(os.environ.get("NUMBA_DISABLE_JIT", default="0") != "0") -def require_numba(function): # pylint: disable=unused-argument - """ - Function decorator for pytest: run if Numba jit is enabled - - Functions decorator to tell pytest to run the test function only if Numba - jit is enabled. To disable Numba jit the environmental variable - ```NUMBA_DISABLE_JIT``` must be set to a value different than 0. - - Use this decorator on test functions that involve great computational load - and don't want to run if Numba jit is disabled. The decorated test - functions will be run and checked if pass or fail, but won't be taken into - account for meassuring coverage. If the test function will run Numba code, - but doesn't involve great computational load, we reccomend using the - ``@pytest.mark.use_numba`` instead. Therefore the test function will be run - twice: one with Numba jit enabled, and another one with Numba jit disable - to check coverage. - """ - # Check if Numba is disabled - # (if NUMBA_DISABLE_JIT is not defined, we assume Numba jit is enabled) - numba_is_disabled = bool(os.environ.get("NUMBA_DISABLE_JIT", default="0") != "0") - - @pytest.mark.use_numba - @pytest.mark.skipif(numba_is_disabled, reason="Numba jit is disabled") - def function_wrapper(): - function() - - return function_wrapper +# Decorator for pytest: run if Numba jit is enabled +# +# Tell pytest to run the test function only if Numba jit is enabled. To disable +# Numba jit the environmental variable ```NUMBA_DISABLE_JIT``` must be set to +# a value different than 0. +# +# Use this decorator on test functions that involve great computational load +# and don't want to run if Numba jit is disabled. The decorated test functions +# will be run and checked if pass or fail, but won't be taken into account for +# meassuring coverage. If the test function will run Numba code, but doesn't +# involve great computational load, we reccomend using the +# ``@pytest.mark.use_numba`` instead. Therefore the test function will be run +# twice: one with Numba jit enabled, and another one with Numba jit disable to +# check coverage. +require_numba = pytest.mark.skipif(NUMBA_IS_DISABLED, reason="Numba jit is disabled") From c298eaa099c4b19abfa1514927e2e26fd6a6d109 Mon Sep 17 00:00:00 2001 From: Santiago Soler Date: Wed, 8 Sep 2021 15:30:57 -0300 Subject: [PATCH 2/3] Disable invalid-name on test/utils.py --- harmonica/tests/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/harmonica/tests/utils.py b/harmonica/tests/utils.py index addbad63c..899c45eac 100644 --- a/harmonica/tests/utils.py +++ b/harmonica/tests/utils.py @@ -4,6 +4,7 @@ # # This code is part of the Fatiando a Terra project (https://www.fatiando.org) # +# pylint disable: invalid-name """ Decorators and useful functions for running tests """ From 08fbbbc1cc5c783a6377b28544dd11e5238b61c5 Mon Sep 17 00:00:00 2001 From: Santiago Soler Date: Wed, 8 Sep 2021 15:34:58 -0300 Subject: [PATCH 3/3] Fix syntax of pylint disable line --- harmonica/tests/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/harmonica/tests/utils.py b/harmonica/tests/utils.py index 899c45eac..f9d48c588 100644 --- a/harmonica/tests/utils.py +++ b/harmonica/tests/utils.py @@ -4,7 +4,7 @@ # # This code is part of the Fatiando a Terra project (https://www.fatiando.org) # -# pylint disable: invalid-name +# pylint: disable=invalid-name """ Decorators and useful functions for running tests """