Skip to content

Commit d883bd1

Browse files
kenjitoyamacopybara-github
authored andcommitted
Use Protocol instead of Callable in rate_limit_wrapper_test.
Unfortunately Python's type checking doesn't currently support functions with attributes (python/mypy#2087) and it fails with a message like `No attribute 'timestamp' on Callable[[Any], Any] [attribute-error]`. This change adds a `Protocol` to the unit test such that the function can be recognized as having the required attributes. PiperOrigin-RevId: 477439369
1 parent e5b2c1c commit d883bd1

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

android_env/wrappers/rate_limit_wrapper_test.py

+16
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""Tests for rate_limit_wrapper."""
1717

1818
import time
19+
from typing import Any, List, Protocol
1920
from unittest import mock
2021

2122
from absl.testing import absltest
@@ -46,6 +47,17 @@ def _get_base_env():
4647
return env
4748

4849

50+
class _FnWithTimestamps(Protocol):
51+
"""A function with `timestamp` and `timestamps` attributes."""
52+
53+
timestamp: float
54+
timestamps: List[float]
55+
56+
57+
def _with_timestamp(fn: Any) -> _FnWithTimestamps:
58+
return fn
59+
60+
4961
class RateLimitWrapperTest(parameterized.TestCase):
5062

5163
@parameterized.named_parameters(
@@ -107,6 +119,7 @@ def test_enabled_sleep_type_before(self, mock_sleep):
107119
_ = wrapper.reset()
108120
mock_sleep.assert_not_called() # It should never sleep during reset().
109121

122+
@_with_timestamp
110123
def _sleep_fn(sleep_time):
111124
_sleep_fn.timestamp = time.time()
112125
self.assertBetween(sleep_time, 0.0, 33.33)
@@ -145,6 +158,7 @@ def test_enabled_sleep_type_after(self, mock_sleep):
145158
_ = wrapper.reset()
146159
mock_sleep.assert_not_called() # It should never sleep during reset().
147160

161+
@_with_timestamp
148162
def _sleep_fn(sleep_time):
149163
_sleep_fn.timestamp = time.time()
150164
self.assertBetween(sleep_time, 0.0, 33.33)
@@ -185,12 +199,14 @@ def test_enabled_sleep_type_after_with_repeat(self, mock_sleep):
185199
_ = wrapper.reset()
186200
mock_sleep.assert_not_called() # It should never sleep during reset().
187201

202+
@_with_timestamp
188203
def _sleep_fn(sleep_time):
189204
_sleep_fn.timestamp = time.time()
190205
self.assertBetween(sleep_time, 0.0, 33.33)
191206

192207
mock_sleep.side_effect = _sleep_fn
193208

209+
@_with_timestamp
194210
def _step_fn(action):
195211
# On even calls the action should be the actual agent action, but on odd
196212
# calls they should be REPEATs.

0 commit comments

Comments
 (0)