Skip to content

Fix the string representation of archetypes #9297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions rerun_py/rerun_sdk/rerun/_baseclasses.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import re
from collections.abc import Iterable, Iterator
from typing import Generic, Protocol, TypeVar, runtime_checkable

Expand Down Expand Up @@ -201,18 +202,35 @@ class Archetype(AsComponents):
"""Base class for all archetypes."""

def __str__(self) -> str:
cls = type(self)
from pprint import pformat

s = f"rr.{cls.__name__}(\n"
for fld in fields(cls):
if "component" in fld.metadata:
comp = getattr(self, fld.name)
datatype = getattr(comp, "type", None)
if datatype:
s += f" {datatype.extension_name}<{datatype}>(\n {comp.to_pylist()}\n )\n"
s += ")"
cls = type(self)

return s
def fields_repr() -> Iterable[str]:
for fld in fields(cls):
if "component" in fld.metadata:
comp = getattr(self, fld.name)
if comp is None:
continue

as_arrow_array = getattr(comp, "as_arrow_array", None)

if as_arrow_array is None:
comp_contents = "<unknown>"
else:
# Note: the regex here is necessary because for some reason pformat add spurious spaces when
# indent > 1.
comp_contents = re.sub(
r"\[\s+\[", "[[", pformat(as_arrow_array().to_pylist(), compact=True, indent=4)
)

yield f" {fld.name}={comp_contents}"

args = ",\n".join(fields_repr())
if args:
return f"rr.{cls.__name__}(\n{args}\n)"
else:
return f"rr.{cls.__name__}()"

@classmethod
def archetype_name(cls) -> str:
Expand Down
100 changes: 100 additions & 0 deletions rerun_py/tests/unit/test_archetype_str.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from __future__ import annotations

import pytest
import rerun as rr


@pytest.mark.parametrize(
["archetype", "expected"],
[
[
rr.Transform3D(),
(
"rr.Transform3D(\n"
" translation=[],\n"
" rotation_axis_angle=[],\n"
" quaternion=[],\n"
" scale=[],\n"
" mat3x3=[],\n"
" relation=[],\n"
" axis_length=[]\n"
")"
),
],
[
rr.Transform3D(translation=[10, 10, 10]),
(
"rr.Transform3D(\n"
" translation=[[10.0, 10.0, 10.0]],\n"
" rotation_axis_angle=[],\n"
" quaternion=[],\n"
" scale=[],\n"
" mat3x3=[],\n"
" relation=[],\n"
" axis_length=[]\n"
")"
),
],
[
rr.Points2D(positions=[[0, 0], [1, 1], [2, 2]]),
"rr.Points2D(\n positions=[[0.0, 0.0], [1.0, 1.0], [2.0, 2.0]]\n)",
],
[
rr.Points2D(positions=[0, 0, 1, 1, 2, 2], radii=[4, 5, 6]),
"rr.Points2D(\n positions=[[0.0, 0.0], [1.0, 1.0], [2.0, 2.0]],\n radii=[4.0, 5.0, 6.0]\n)",
],
[rr.Points2D.from_fields(), "rr.Points2D()"],
[
rr.Points3D(
[
11,
2,
3,
2,
3,
2,
3,
2,
3,
2,
3,
2,
3,
2,
3,
2,
3,
2,
3,
2,
3,
2,
3,
2,
3,
2,
3,
2,
3,
2,
3,
2,
3,
2,
3,
3,
],
radii=[1, 2, 3],
),
"""\
rr.Points3D(
positions=[[11.0, 2.0, 3.0], [2.0, 3.0, 2.0], [3.0, 2.0, 3.0], [2.0, 3.0, 2.0],
[3.0, 2.0, 3.0], [2.0, 3.0, 2.0], [3.0, 2.0, 3.0], [2.0, 3.0, 2.0],
[3.0, 2.0, 3.0], [2.0, 3.0, 2.0], [3.0, 2.0, 3.0], [2.0, 3.0, 3.0]],
radii=[1.0, 2.0, 3.0]
)""",
],
],
)
def test_archetype_str(archetype: rr._baseclasses.Archetype, expected: str) -> None:
assert str(archetype) == expected