Skip to content

Commit

Permalink
Add a convenience property CompositeType.inner_type to reduce boilerp…
Browse files Browse the repository at this point in the history
…late (OpenCyphal#55)

* Add a convenience property CompositeType.inner_type to reduce boilerplate

* Bump version
  • Loading branch information
pavel-kirienko authored Oct 31, 2020
1 parent 0a2c0b1 commit ff0fb35
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pydsdl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os as _os
import sys as _sys

__version__ = '1.9.0'
__version__ = '1.9.1'
__version_info__ = tuple(map(int, __version__.split('.')))
__license__ = 'MIT'
__author__ = 'UAVCAN Development Team'
Expand Down
18 changes: 18 additions & 0 deletions pydsdl/_serializable/_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,17 @@ def fields_except_padding(self) -> typing.List[Field]:
def constants(self) -> typing.List[Constant]:
return [a for a in self.attributes if isinstance(a, Constant)]

@property
def inner_type(self) -> 'CompositeType':
"""
If the concrete type is a decorator over another Composite (such as :class:`DelimitedType`),
this property provides access to the decorated instance.
Otherwise, returns the current instance reference unchanged.
This is intended for use in scenarios where the decoration is irrelevant and the user needs to know
the concrete type of the decorated instance.
"""
return self

@property
def fixed_port_id(self) -> typing.Optional[int]:
return self._fixed_port_id
Expand Down Expand Up @@ -741,9 +752,12 @@ def try_name(name: str) -> CompositeType:
assert s[''] # Padding fields are not accessible
assert hash(s) == hash(s)
assert not s.has_parent_service
assert s.inner_type is s
assert s.inner_type.inner_type is s

d = DelimitedType(s, 2048)
assert d.inner_type is s
assert d.inner_type.inner_type is s
assert d.attributes == d.inner_type.attributes
with raises(KeyError):
assert d['c']
Expand Down Expand Up @@ -787,6 +801,8 @@ def try_union_fields(field_types: typing.List[SerializableType]) -> UnionType:
UnsignedIntegerType(16, PrimitiveType.CastMode.TRUNCATED),
SignedIntegerType(16, PrimitiveType.CastMode.SATURATED),
])
assert u.inner_type is u
assert u.inner_type.inner_type is u
assert u.bit_length_set == {24}
assert u.extent == 24
assert DelimitedType(u, 40).extent == 40
Expand All @@ -796,6 +812,8 @@ def try_union_fields(field_types: typing.List[SerializableType]) -> UnionType:
assert DelimitedType(u, 32).extent == 32
assert DelimitedType(u, 32).bit_length_set == {32, 40, 48, 56, 64}
assert DelimitedType(u, 800).extent == 800
assert DelimitedType(u, 800).inner_type is u
assert DelimitedType(u, 800).inner_type.inner_type is u

assert try_union_fields(
[
Expand Down

0 comments on commit ff0fb35

Please sign in to comment.