Skip to content

Conversation

@pavel-kirienko
Copy link
Member

@pavel-kirienko pavel-kirienko commented May 2, 2023

Implement string and byte array support as described in OpenCyphal/specification#51. The Specification will be updated after this is agreed upon. The grammar has been modified as follows:

  • Add utf8 primitive.
  • Add byte primitive.
  • Remove the unnecessary form saturated bool, which was shorthand for bool, for regularity. From now on, only unqualified bool is allowed. The expected impact is zero since the long form was never seen in the wild. Note that truncated bool was never legal.

At the moment, the following constraints are enforced; they may be lifted in the future:

  • utf8 can only be used as an element type of a variable-length array:
utf8[<=10] name  # valid
utf8[10]   name  # invalid
utf8       name  # invalid
  • byte can only be used as an element type of an array:
byte[<=10] data  # valid
byte[10]   data  # valid
byte       data  # invalid

Both utf8 and byte are concrete instances of UnsignedIntegerType. By virtue of this, existing users of PyDSDL (like Nunavut) will interpret both utf8 and byte as truncated uint8, until explicitly updated to take advantage of the new feature. That is:

utf8[<=10] name  # seen by Nunavut as: truncated uint8[<=10]
utf8[10]   name  # frontend error
utf8       name  # frontend error
byte[<=10] data  # seen by Nunavut as: truncated uint8[<=10]
byte[10]   data  # seen by Nunavut as: truncated uint8[10]
byte       data  # frontend error

Property pydsdl.ArrayType.string_like is now deprecated and should be replaced with an explicit isinstance(array.element_type, pydsdl.UTF8Type).

Some internal refactoring has been done to unify the deprecation consistency checking with the aggregation constraints listed above (e.g., utf8[<=10] is valid but utf8[10] is not).

Closes #96
Relates to OpenCyphal/yakut#65

@pavel-kirienko
Copy link
Member Author

I would like to summon @samcrow for commentary.

@samcrow
Copy link

samcrow commented May 3, 2023

This looks good overall and not very difficult to add to other implementations.

@aentinger
Copy link

May I inquire if there's anything keeping this PR from being merged?

@pavel-kirienko
Copy link
Member Author

pavel-kirienko commented Jul 2, 2023 via email

@pavel-kirienko
Copy link
Member Author

@thirtytwobits I suggest we merge this

assert isinstance(major, _expression.Rational) and isinstance(minor, _expression.Rational)
return _serializable.Version(major=major.as_native_integer(), minor=minor.as_native_integer())

def visit_type_primitive_boolean(self, _n: _Node, _c: _Children) -> _serializable.PrimitiveType:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that these types are not part of the current standard, should we add an "allow experimental" or, inversely, "strict" mode to ensure we can enforce 1.0 compatibility when using pydsdl?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a new strict mode option. In the future we should extract all options into a single Options dataclass. Right now we can't do it properly without breaking API compatibility.

@emrainey
Copy link

emrainey commented May 13, 2025

Would there ever be a case where we'd want to send individual characters as bytes or utf8?

Certainly one could 'byte[1]' but this seems heavy.

@pavel-kirienko
Copy link
Member Author

I couldn't come up with a use case where this would be needed instead of uint8. Note that a single byte of utf8 is meaningless because it is a variable-length encoding, and the length of an encoded code point depends on the value of the first byte.

We can always remove these restrictions in the future if needed. It makes sense to start with a restricted feature set to allow more choices in the future without breaking backward compatibility.

pavel-kirienko added a commit to OpenCyphal/specification that referenced this pull request May 15, 2025
Closes #51

See OpenCyphal/pydsdl#97

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@pavel-kirienko pavel-kirienko requested a review from emrainey May 16, 2025 19:26
@pavel-kirienko pavel-kirienko merged commit a1506ce into master May 20, 2025
15 checks passed
@pavel-kirienko pavel-kirienko deleted the dev branch May 20, 2025 05:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add string support

7 participants