Skip to content

Commit b216be6

Browse files
abey79jprochazk
authored andcommitted
Improve annotations on View classes and add tests/python to mypy coverage (#9257)
### Related * Improves on #9209 ### What This PR improves the signatures of the `View` python class (and subclasses), and adds `tests/python` in the coverage of `mypy`.
1 parent 9610ed6 commit b216be6

40 files changed

+150
-130
lines changed

crates/build/re_types_builder/src/codegen/python/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,8 @@ impl PythonCodeGenerator {
393393
"
394394
from __future__ import annotations
395395
396-
from typing import (Any, Dict, Iterable, Optional, Sequence, Set, Tuple, Union,
397-
TYPE_CHECKING, SupportsFloat, Literal)
396+
from collections.abc import Iterable, Mapping, Set, Sequence, Dict
397+
from typing import Any, Optional, Union, TYPE_CHECKING, SupportsFloat, Literal, Tuple
398398
from typing_extensions import deprecated # type: ignore[misc, unused-ignore]
399399
400400
from attrs import define, field

crates/build/re_types_builder/src/codegen/python/views.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ fn init_method(reporter: &Reporter, objects: &Objects, obj: &Object) -> String {
5858
contents: ViewContentsLike = "$origin/**",
5959
name: Utf8Like | None = None,
6060
visible: datatypes.BoolLike | None = None,
61-
defaults: list[AsComponents | Iterable[DescribedComponentBatch]] | None = None,
62-
overrides: dict[EntityPathLike, AsComponents | Iterable[DescribedComponentBatch | AsComponents | Iterable[DescribedComponentBatch]]] | None = None,
61+
defaults: Iterable[AsComponents | Iterable[DescribedComponentBatch]] | None = None,
62+
overrides: Mapping[EntityPathLike, AsComponents | Iterable[DescribedComponentBatch | AsComponents | Iterable[DescribedComponentBatch]]] | None = None,
6363
"#
6464
.to_owned();
6565

pixi.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rerun_py/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ files = [
213213
"rerun_py/tests",
214214
"examples/python",
215215
"scripts",
216+
"tests/python",
216217
]
217218
exclude = ["examples/python/objectron", "examples/python/ros_node"]
218219
namespace_packages = true

rerun_py/rerun_sdk/rerun/_baseclasses.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def as_component_batches(self) -> list[DescribedComponentBatch]:
197197

198198

199199
@define
200-
class Archetype:
200+
class Archetype(AsComponents):
201201
"""Base class for all archetypes."""
202202

203203
def __str__(self) -> str:

rerun_py/rerun_sdk/rerun/blueprint/api.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import uuid
4-
from collections.abc import Iterable
4+
from collections.abc import Iterable, Mapping
55
from typing import Optional, Union
66

77
import rerun_bindings as bindings
@@ -44,8 +44,8 @@ def __init__(
4444
name: Utf8Like | None,
4545
visible: BoolLike | None = None,
4646
properties: dict[str, AsComponents] | None = None,
47-
defaults: list[AsComponents | Iterable[DescribedComponentBatch]] | None = None,
48-
overrides: dict[
47+
defaults: Iterable[AsComponents | Iterable[DescribedComponentBatch]] | None = None,
48+
overrides: Mapping[
4949
EntityPathLike,
5050
AsComponents | Iterable[DescribedComponentBatch | AsComponents | Iterable[DescribedComponentBatch]],
5151
]
@@ -98,8 +98,8 @@ def __init__(
9898
self.contents = contents
9999
self.visible = visible
100100
self.properties = properties if properties is not None else {}
101-
self.defaults = defaults if defaults is not None else []
102-
self.overrides = overrides if overrides is not None else {}
101+
self.defaults = list(defaults) if defaults is not None else []
102+
self.overrides = dict(overrides.items()) if overrides is not None else {}
103103

104104
def blueprint_path(self) -> str:
105105
"""

rerun_py/rerun_sdk/rerun/blueprint/views/bar_chart_view.py

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rerun_py/rerun_sdk/rerun/blueprint/views/dataframe_view.py

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rerun_py/rerun_sdk/rerun/blueprint/views/graph_view.py

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rerun_py/rerun_sdk/rerun/blueprint/views/map_view.py

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rerun_py/rerun_sdk/rerun/blueprint/views/spatial2d_view.py

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rerun_py/rerun_sdk/rerun/blueprint/views/spatial3d_view.py

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rerun_py/rerun_sdk/rerun/blueprint/views/tensor_view.py

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rerun_py/rerun_sdk/rerun/blueprint/views/text_document_view.py

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rerun_py/rerun_sdk/rerun/blueprint/views/text_log_view.py

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rerun_py/rerun_sdk/rerun/blueprint/views/time_series_view.py

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/python/__init__.py

Whitespace-only changes.

tests/python/chunk_zoo/chunk_zoo.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
import numpy as np
1515
import rerun as rr
16-
import rerun.components as rrc
1716

1817

1918
def frame_times(t: int | Sequence[int], *args: int) -> list[rr.IndexColumn]:
@@ -54,7 +53,9 @@ def specimen_archetype_with_clamp_join_semantics() -> None:
5453
"/archetype_with_clamp_join_semantics",
5554
frame_times(0),
5655
[
57-
*rr.Points2D.columns(positions=[(i, i) for i in range(10)], _lengths=[10]),
56+
*rr.Points2D.columns(
57+
positions=[(i, i) for i in range(10)],
58+
).partition([10]),
5859
*rr.Points2D.columns(radii=2),
5960
],
6061
)
@@ -72,15 +73,15 @@ def specimen_archetype_with_latest_at_semantics() -> None:
7273
rr.log("/archetype_chunk_with_latest_at_semantics", [rr.Points2D.indicator()])
7374

7475
set_frame_time(5)
75-
rr.log("/archetype_chunk_with_latest_at_semantics", [rrc.RadiusBatch(2)])
76+
rr.log("/archetype_chunk_with_latest_at_semantics", rr.Points2D.from_fields(radii=2))
7677

7778

7879
def specimen_archetype_with_clamp_join_semantics_two_chunks() -> None:
7980
"""Single row of an archetype with clamp join semantics (Points2D), across two chunks."""
8081
rr.send_columns(
8182
"/archetype_with_clamp_join_semantics_two_batches",
8283
frame_times(0),
83-
rr.Points2D.columns(positions=[(i, i) for i in range(10)], _lengths=[10]),
84+
rr.Points2D.columns(positions=[(i, i) for i in range(10)]).partition([10]),
8485
)
8586

8687
rr.send_columns(
@@ -99,9 +100,8 @@ def specimen_archetype_without_clamp_join_semantics() -> None:
99100
*rr.Mesh3D.columns(
100101
vertex_positions=[(0, 0, 0), (0, 1, 0), (1, 1, 0), (1, 0, 0)],
101102
vertex_colors=[(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0)],
102-
_lengths=[4],
103-
),
104-
*rr.Mesh3D.columns(triangle_indices=[(0, 1, 2), (0, 2, 3)], _lengths=[2]),
103+
).partition([4]),
104+
*rr.Mesh3D.columns(triangle_indices=[(0, 1, 2), (0, 2, 3)]).partition([2]),
105105
],
106106
)
107107

@@ -117,7 +117,7 @@ def specimen_many_rows_with_mismatched_instance_count() -> None:
117117
15,
118118
size=100,
119119
)
120-
batch_size = np.sum(positions_partitions)
120+
batch_size = int(np.sum(positions_partitions))
121121

122122
# Shuffle the color partitions to induce the mismatch
123123
colors_partitions = positions_partitions.copy()
@@ -130,8 +130,8 @@ def specimen_many_rows_with_mismatched_instance_count() -> None:
130130
"/many_rows_with_mismatched_instance_count",
131131
frame_times(range(len(positions_partitions))),
132132
[
133-
*rr.Points2D.columns(positions=positions, _lengths=positions_partitions),
134-
*rr.Points2D.columns(colors=colors, _lengths=colors_partitions),
133+
*rr.Points2D.columns(positions=positions).partition(positions_partitions),
134+
*rr.Points2D.columns(colors=colors).partition(colors_partitions),
135135
],
136136
)
137137
rr.log("/many_rows_with_mismatched_instance_count", [rr.Points2D.indicator()], static=True)
@@ -161,7 +161,7 @@ def specimen_archetype_chunk_with_clear() -> None:
161161
)
162162

163163
set_frame_time(0)
164-
rr.log("/archetype_chunk_with_clear", [rr.Points2D.indicator(), rrc.RadiusBatch(2)])
164+
rr.log("/archetype_chunk_with_clear", [rr.Points2D.indicator()], rr.Points2D.from_fields(radii=2))
165165

166166
set_frame_time(5)
167167
rr.log("/archetype_chunk_with_clear", rr.Clear(recursive=False))
@@ -184,12 +184,7 @@ def main() -> None:
184184
if name.startswith("specimen_") and callable(globals()[name]) and (not args.filter or args.filter in name)
185185
]
186186

187-
# Create an inventory of all the specimens
188-
def strip_prefix(s: str) -> str:
189-
if s.startswith("specimen_"):
190-
return s[len("specimen_") :]
191-
192-
specimen_list = "\n".join([f"| {strip_prefix(s.__name__)} | {s.__doc__} |" for s in specimens])
187+
specimen_list = "\n".join([f"| {s.__name__.removeprefix('specimen_')} | {s.__doc__} |" for s in specimens])
193188
markdown = (
194189
"# Chunk Zoo\n\n"
195190
"This recording contains a variety of chunks of various typologies, for testing purposes.\n\n"

tests/python/log_benchmark/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@
33
import dataclasses
44

55
import numpy as np
6+
import numpy.typing as npt
67

78
MAX_INT64 = 2**63 - 1
89
MAX_INT32 = 2**31 - 1
910

1011

1112
@dataclasses.dataclass
1213
class Point3DInput:
13-
positions: np.ndarray
14-
colors: np.ndarray
15-
radii: np.ndarray
14+
positions: npt.NDArray[np.float32]
15+
colors: npt.NDArray[np.uint32]
16+
radii: npt.NDArray[np.float32]
1617
label: str = "some label"
1718

1819
@classmethod
19-
def prepare(cls, seed: int, num_points: int) -> None:
20+
def prepare(cls, seed: int, num_points: int) -> Point3DInput:
2021
rng = np.random.default_rng(seed=seed)
2122

2223
return cls(

0 commit comments

Comments
 (0)