Check type correctness in all bind paths.
PiperOrigin-RevId: 727877128 Change-Id: I4f4f74a1fb96d3fc5abdea531b78eb54dc53d186
This commit is contained in:
committed by
Copybara-Service
parent
0a4eda5065
commit
d5065f867b
@@ -433,6 +433,12 @@ class BindData(object):
|
|||||||
self.id = ids
|
self.id = ids
|
||||||
|
|
||||||
def __getname(self, name: str):
|
def __getname(self, name: str):
|
||||||
|
"""Get the name of the attribute and check if the type is correct."""
|
||||||
|
if name == 'sensordata':
|
||||||
|
if self.prefix == 'sensor_':
|
||||||
|
return name
|
||||||
|
else:
|
||||||
|
raise AttributeError('sensordata is not available for this type')
|
||||||
if name == 'ctrl':
|
if name == 'ctrl':
|
||||||
if self.prefix == 'actuator_':
|
if self.prefix == 'actuator_':
|
||||||
return name
|
return name
|
||||||
@@ -464,11 +470,11 @@ class BindData(object):
|
|||||||
idx = []
|
idx = []
|
||||||
for a, n in zip(adr, num):
|
for a, n in zip(adr, num):
|
||||||
idx.extend(a + j for j in range(n))
|
idx.extend(a + j for j in range(n))
|
||||||
return getattr(self.data, name)[idx, ...]
|
return getattr(self.data, self.__getname(name))[idx, ...]
|
||||||
elif num > 1:
|
elif num > 1:
|
||||||
return getattr(self.data, name)[adr : adr + num, ...]
|
return getattr(self.data, self.__getname(name))[adr : adr + num, ...]
|
||||||
else:
|
else:
|
||||||
return getattr(self.data, name)[adr, ...]
|
return getattr(self.data, self.__getname(name))[adr, ...]
|
||||||
return getattr(self.data, self.__getname(name))[self.id, ...]
|
return getattr(self.data, self.__getname(name))[self.id, ...]
|
||||||
|
|
||||||
def set(self, name: str, value: jax.Array) -> Data:
|
def set(self, name: str, value: jax.Array) -> Data:
|
||||||
|
|||||||
@@ -282,12 +282,22 @@ class SupportTest(parameterized.TestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# test invalid name
|
# test invalid name
|
||||||
with self.assertRaises(AttributeError):
|
with self.assertRaises(
|
||||||
|
AttributeError, msg='ctrl is not available for this type'
|
||||||
|
):
|
||||||
print(dx.bind(mx, s.geoms).ctrl)
|
print(dx.bind(mx, s.geoms).ctrl)
|
||||||
with self.assertRaises(AttributeError):
|
with self.assertRaises(
|
||||||
|
AttributeError, msg='ctrl is not available for this type'
|
||||||
|
):
|
||||||
print(dx.bind(mx, s.actuators).actuator_ctrl)
|
print(dx.bind(mx, s.actuators).actuator_ctrl)
|
||||||
with self.assertRaises(AttributeError):
|
with self.assertRaises(
|
||||||
|
AttributeError, msg='ctrl is not available for this type'
|
||||||
|
):
|
||||||
print(dx.bind(mx, s.actuators).set('actuator_ctrl', [1, 2, 3]))
|
print(dx.bind(mx, s.actuators).set('actuator_ctrl', [1, 2, 3]))
|
||||||
|
with self.assertRaises(
|
||||||
|
AttributeError, msg='qpos and qvel are not available for this type'
|
||||||
|
):
|
||||||
|
print(dx.bind(mx, s.geoms).qpos)
|
||||||
with self.assertRaises(KeyError, msg='invalid name: invalid_actuator_name'):
|
with self.assertRaises(KeyError, msg='invalid name: invalid_actuator_name'):
|
||||||
s.actuators[0].name = 'invalid_actuator_name'
|
s.actuators[0].name = 'invalid_actuator_name'
|
||||||
print(dx.bind(mx, s.actuators).set('ctrl', [1, 2, 3]))
|
print(dx.bind(mx, s.actuators).set('ctrl', [1, 2, 3]))
|
||||||
|
|||||||
Reference in New Issue
Block a user