Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def validate(obj: pyuavcan.dsdl.CompositeObject, s: str) -> None:
for f in model.fields_except_padding:
field_present = (f'{f.name}=' in s) or (f'{f.name}_=' in s)
if isinstance(model, pydsdl.UnionType):
# In unions only the active field is printed.
# The active field may contain nested fields which may be named similarly to other fields
# in the current union, so we can't easily ensure lack of non-active fields in the output.
field_active = pyuavcan.dsdl.get_attribute(obj, f.name) is not None
if field_active:
assert field_present, f'{f.name}: {s}'
else:
# In structures all fields are printed always.
assert field_present, f'{f.name}: {s}'
assert obj.else_[0].y is not None
assert len(obj.else_[0].y) == 2
assert obj.else_[0].y[0].x is None
assert obj.else_[0].y[0].y == 7
assert obj.else_[0].y[1].x == 5
assert obj.else_[0].y[1].y is None
assert obj.else_[1].x is not None
assert obj.else_[1].y is None
assert obj.else_[1].x[0].x == 8
assert obj.else_[1].x[0].y is None
assert obj.else_[1].x[1].x is None
assert obj.else_[1].x[1].y == 13
assert len(obj.raise_) == 0
with pytest.raises(AttributeError, match='nonexistent'):
pyuavcan.dsdl.get_attribute(obj, 'nonexistent')
with pytest.raises(AttributeError, match='nonexistent'):
pyuavcan.dsdl.set_attribute(obj, 'nonexistent', 123)
def _are_close(data_type: pydsdl.SerializableType, a: typing.Any, b: typing.Any) -> bool:
"""
If you ever decided to copy-paste this test function into a production application,
beware that it evaluates (NaN == NaN) as True. This is what we want when testing,
but this is not what most real systems expect.
"""
if a is None or b is None: # These occur, for example, in unions
return (a is None) == (b is None)
elif isinstance(data_type, pydsdl.CompositeType):
if type(a) != type(b): # pragma: no cover
return False
for f in pyuavcan.dsdl.get_type(a).fields_except_padding: # pragma: no cover
if not _are_close(f.data_type,
pyuavcan.dsdl.get_attribute(a, f.name),
pyuavcan.dsdl.get_attribute(b, f.name)):
return False
return True # Empty objects of same type compare equal
elif isinstance(data_type, pydsdl.ArrayType):
return all(starmap(partial(_are_close, data_type.element_type), zip(a, b))) \
if len(a) == len(b) and a.dtype == b.dtype else False
elif isinstance(data_type, pydsdl.FloatType):
t = {
16: numpy.float16,
32: numpy.float32,
64: numpy.float64,
}[data_type.bit_length]
return numpy.allclose(t(a), t(b), equal_nan=True)
def _unittest_slow_constants(generated_packages: typing.List[pyuavcan.dsdl.GeneratedPackageInfo]) -> None:
for info in generated_packages:
for model in _util.expand_service_types(info.models, keep_services=True):
dtype = pyuavcan.dsdl.get_class(model)
for c in model.constants:
if isinstance(c.data_type, pydsdl.PrimitiveType): # pragma: no branch
reference = c.value
generated = pyuavcan.dsdl.get_attribute(dtype, c.name)
assert isinstance(reference, pydsdl.Primitive)
assert reference.native_value == pytest.approx(generated), \
'The generated constant does not compare equal against the DSDL source'
if issubclass(dtype, pyuavcan.dsdl.FixedPortObject):
assert issubclass(dtype, pyuavcan.dsdl.CompositeObject) \
and issubclass(dtype, pyuavcan.dsdl.FixedPortObject)
assert pyuavcan.dsdl.get_fixed_port_id(dtype) == pyuavcan.dsdl.get_model(dtype).fixed_port_id
def _test_textual_representations(model: pydsdl.CompositeType, obj: pyuavcan.dsdl.CompositeObject) -> None:
for fn in [str, repr]:
assert callable(fn)
s = fn(obj)
for f in model.fields_except_padding:
field_present = (f'{f.name}=' in s) or (f'{f.name}_=' in s)
if isinstance(model, pydsdl.UnionType):
# In unions only the active field is printed. The active field may contain nested fields which
# may be named similarly to other fields in the current union, so we can't easily ensure lack of
# non-active fields in the output.
field_active = pyuavcan.dsdl.get_attribute(obj, f.name) is not None
if field_active:
assert field_present, f'{f.name}: {s}'
else:
# In structures all fields are printed always.
assert field_present, f'{f.name}: {s}'
def are_close(model: pydsdl.SerializableType, a: typing.Any, b: typing.Any) -> bool:
"""
If you ever decided to copy-paste this test function into a production application,
beware that it evaluates (NaN == NaN) as True. This is what we want when testing,
but this is not what most real systems expect.
"""
if a is None or b is None: # These occur, for example, in unions
return (a is None) == (b is None)
elif isinstance(model, pydsdl.CompositeType):
if type(a) != type(b): # pragma: no cover
return False
for f in pyuavcan.dsdl.get_model(a).fields_except_padding: # pragma: no cover
if not are_close(f.data_type,
pyuavcan.dsdl.get_attribute(a, f.name),
pyuavcan.dsdl.get_attribute(b, f.name)):
return False
return True # Empty objects of same type compare equal
elif isinstance(model, pydsdl.ArrayType):
if len(a) != len(b) or a.dtype != b.dtype: # pragma: no cover
return False
if isinstance(model.element_type, pydsdl.PrimitiveType):
return bool(numpy.allclose(a, b, equal_nan=True)) # Speedup for large arrays like images or point clouds
else:
return all(itertools.starmap(functools.partial(are_close, model.element_type), zip(a, b)))
elif isinstance(model, pydsdl.FloatType):
t = {
16: numpy.float16,
32: numpy.float32,
64: numpy.float64,
def are_close(model: pydsdl.SerializableType, a: typing.Any, b: typing.Any) -> bool:
"""
If you ever decided to copy-paste this test function into a production application,
beware that it evaluates (NaN == NaN) as True. This is what we want when testing,
but this is not what most real systems expect.
"""
if a is None or b is None: # These occur, for example, in unions
return (a is None) == (b is None)
elif isinstance(model, pydsdl.CompositeType):
if type(a) != type(b): # pragma: no cover
return False
for f in pyuavcan.dsdl.get_model(a).fields_except_padding: # pragma: no cover
if not are_close(f.data_type,
pyuavcan.dsdl.get_attribute(a, f.name),
pyuavcan.dsdl.get_attribute(b, f.name)):
return False
return True # Empty objects of same type compare equal
elif isinstance(model, pydsdl.ArrayType):
if len(a) != len(b) or a.dtype != b.dtype: # pragma: no cover
return False
if isinstance(model.element_type, pydsdl.PrimitiveType):
return bool(numpy.allclose(a, b, equal_nan=True)) # Speedup for large arrays like images or point clouds
else:
return all(itertools.starmap(functools.partial(are_close, model.element_type), zip(a, b)))
elif isinstance(model, pydsdl.FloatType):
t = {
16: numpy.float16,
32: numpy.float32,