diff --git a/.gitignore b/.gitignore index b87e1ed5..157bfb33 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,7 @@ pip-log.txt .nox .cache .pytest_cache +pytype_output # Mac @@ -57,4 +58,4 @@ system_tests/local_test_setup # Make sure a generated file isn't accidentally committed. pylintrc -pylintrc.test \ No newline at end of file +pylintrc.test diff --git a/google/api_core/gapic_v1/method.py b/google/api_core/gapic_v1/method.py index 49982c03..8bf82569 100644 --- a/google/api_core/gapic_v1/method.py +++ b/google/api_core/gapic_v1/method.py @@ -61,6 +61,13 @@ def _determine_timeout(default_timeout, specified_timeout, retry): Returns: Optional[Timeout]: The timeout to apply to the method or ``None``. """ + # If timeout is specified as a number instead of a Timeout instance, + # convert it to a ConstantTimeout. + if isinstance(specified_timeout, (int, float)): + specified_timeout = timeout.ConstantTimeout(specified_timeout) + if isinstance(default_timeout, (int, float)): + default_timeout = timeout.ConstantTimeout(default_timeout) + if specified_timeout is DEFAULT: specified_timeout = default_timeout @@ -78,12 +85,7 @@ def _determine_timeout(default_timeout, specified_timeout, retry): else: return default_timeout - # If timeout is specified as a number instead of a Timeout instance, - # convert it to a ConstantTimeout. - if isinstance(specified_timeout, (int, float)): - return timeout.ConstantTimeout(specified_timeout) - else: - return specified_timeout + return specified_timeout class _GapicCallable(object): diff --git a/tests/unit/gapic/test_method.py b/tests/unit/gapic/test_method.py index 0f9bee93..1ae27de0 100644 --- a/tests/unit/gapic/test_method.py +++ b/tests/unit/gapic/test_method.py @@ -32,6 +32,27 @@ def _utcnow_monotonic(): curr_value += delta +def test__determine_timeout(): + # Check _determine_timeout always returns a Timeout object. + timeout_type_timeout = timeout.ConstantTimeout(600.0) + returned_timeout = google.api_core.gapic_v1.method._determine_timeout( + 600.0, 600.0, None + ) + assert isinstance(returned_timeout, timeout.ConstantTimeout) + returned_timeout = google.api_core.gapic_v1.method._determine_timeout( + 600.0, timeout_type_timeout, None + ) + assert isinstance(returned_timeout, timeout.ConstantTimeout) + returned_timeout = google.api_core.gapic_v1.method._determine_timeout( + timeout_type_timeout, 600.0, None + ) + assert isinstance(returned_timeout, timeout.ConstantTimeout) + returned_timeout = google.api_core.gapic_v1.method._determine_timeout( + timeout_type_timeout, timeout_type_timeout, None + ) + assert isinstance(returned_timeout, timeout.ConstantTimeout) + + def test_wrap_method_basic(): method = mock.Mock(spec=["__call__"], return_value=42) @@ -154,7 +175,8 @@ def test_wrap_method_with_default_retry_and_timeout_using_sentinel(unusued_sleep @mock.patch("time.sleep") def test_wrap_method_with_overriding_retry_and_timeout(unusued_sleep): - method = mock.Mock(spec=["__call__"], side_effect=[exceptions.NotFound(None), 42]) + method = mock.Mock(spec=["__call__"], side_effect=[ + exceptions.NotFound(None), 42]) default_retry = retry.Retry() default_timeout = timeout.ConstantTimeout(60) wrapped_method = google.api_core.gapic_v1.method.wrap_method(