Skip to content

Commit ffaa2e3

Browse files
kenjitoyamacopybara-github
authored andcommitted
Fix type safety and typing issues in AndroidEnv components.
- Annotate log_stream filters to ensure strict typing. - Refactor specs action_spec construction to use clean, standard loops and assignments, resolving type invariance issues. PiperOrigin-RevId: 917119228
1 parent 817586d commit ffaa2e3

2 files changed

Lines changed: 27 additions & 26 deletions

File tree

‎android_env/components/log_stream.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class LogStream(metaclass=abc.ABCMeta):
2626

2727
def __init__(self, verbose: bool = False):
2828
self._verbose = verbose
29-
self._filters = []
29+
self._filters: list[str] = []
3030
self._should_stream = threading.Event()
3131

3232
def get_stream_output(self) -> Generator[str, None, None]:

‎android_env/components/specs.py‎

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,36 +65,37 @@ def base_action_spec(
6565

6666
num_actions = len(action_type.ActionType) if enable_key_events else 3
6767

68-
action_spec = {
69-
'action_type':
70-
specs.DiscreteArray(num_values=num_actions, name='action_type'),
71-
'touch_position':
72-
specs.BoundedArray(
73-
shape=(2,),
74-
dtype=np.float32,
75-
minimum=[0.0, 0.0],
76-
maximum=[1.0, 1.0],
77-
name='touch_position'),
68+
action_spec: dict[str, specs.Array] = {
69+
'action_type': specs.DiscreteArray(
70+
num_values=num_actions, name='action_type'
71+
),
72+
'touch_position': specs.BoundedArray(
73+
shape=(2,),
74+
dtype=np.float32,
75+
minimum=[0.0, 0.0],
76+
maximum=[1.0, 1.0],
77+
name='touch_position',
78+
),
7879
}
7980

81+
# Add specs for additional fingers.
8082
for i in range(2, num_fingers + 1):
81-
action_spec.update({
82-
f'action_type_{i}':
83-
specs.DiscreteArray(
84-
num_values=len(action_type.ActionType),
85-
name=f'action_type_{i}'),
86-
f'touch_position_{i}':
87-
specs.BoundedArray(
88-
shape=(2,),
89-
dtype=np.float32,
90-
minimum=[0.0, 0.0],
91-
maximum=[1.0, 1.0],
92-
name=f'touch_position_{i}'),
93-
})
94-
83+
action_spec[f'action_type_{i}'] = specs.DiscreteArray(
84+
num_values=len(action_type.ActionType), name=f'action_type_{i}'
85+
)
86+
action_spec[f'touch_position_{i}'] = specs.BoundedArray(
87+
shape=(2,),
88+
dtype=np.float32,
89+
minimum=[0.0, 0.0],
90+
maximum=[1.0, 1.0],
91+
name=f'touch_position_{i}',
92+
)
93+
94+
# Add keycode spec if enabled.
9595
if enable_key_events:
9696
action_spec['keycode'] = specs.DiscreteArray(
97-
num_values=(1 << 16) - 1, name='keycode')
97+
num_values=(1 << 16) - 1, name='keycode'
98+
)
9899

99100
return action_spec
100101

0 commit comments

Comments
 (0)