Skip to content

Commit d5482b5

Browse files
authored
Fix several typing annotations in the SDK and run mypy on snippets (#9260)
### Related * Some more leftovers from #9209 ### What Snippets, a key aspect of our documentation, were not typed checked by mypy so far... 😱 No longer. This lead to several fixes in user-facing typing annotation in the python SDK. Plus the usual load of fixes in the snippets themselves. Note: snippets that are "not runnable" (as per `snippets.toml`) must now also be mypy-ignore in our `pyproject.toml`. I don't mind this added complexity a single second: make your snippets runnable! :)
1 parent 6e80626 commit d5482b5

17 files changed

+52
-32
lines changed

docs/snippets/INDEX.md

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

docs/snippets/all/__init__.py

Whitespace-only changes.

docs/snippets/all/archetypes/scalar_multiple_plots.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@
2424
rr.log("trig/cos", rr.Scalar(cos(float(t) / 100.0)))
2525

2626
lcg_state = (1140671485 * lcg_state + 128201163) % 16777216 # simple linear congruency generator
27-
rr.log("scatter/lcg", rr.Scalar(lcg_state))
27+
rr.log("scatter/lcg", rr.Scalar(lcg_state.astype(np.float64)))

docs/snippets/all/archetypes/transform3d_hierarchy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Logs a transforms transform hierarchy."""
1+
"""Logs a transform hierarchy."""
22

33
import numpy as np
44
import rerun as rr
@@ -26,7 +26,7 @@
2626
d_planet = 6.0
2727
d_moon = 3.0
2828
angles = np.arange(0.0, 1.01, 0.01) * np.pi * 2
29-
circle = np.array([np.sin(angles), np.cos(angles), angles * 0.0]).transpose()
29+
circle = np.array([np.sin(angles), np.cos(angles), angles * 0.0], dtype=np.float32).transpose()
3030
rr.log("sun/planet_path", rr.LineStrips3D(circle * d_planet))
3131
rr.log("sun/planet/moon_path", rr.LineStrips3D(circle * d_moon))
3232

docs/snippets/all/concepts/different_data_per_timeline.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
# Log a red color on one timeline.
1313
rr.reset_time() # Clears all set timeline info.
1414
rr.set_index("red timeline", timedelta=1.0)
15-
rr.log("points", [rr.components.Color(0xFF0000FF)])
15+
rr.log("points", rr.Points2D.from_fields(colors=[255, 0, 0]))
1616

1717
# And a blue color on the other.
1818
rr.reset_time() # Clears all set timeline info.
1919
rr.set_index("blue timeline", sequence=1)
20-
rr.log("points", [rr.components.Color(0x0000FFFF)])
20+
rr.log("points", rr.Points2D.from_fields(colors=[0, 0, 255]))
2121

2222

2323
# Set view bounds:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
rr.log("skybox", static=True, generate_skybox_mesh())
1+
rr.log("skybox", generate_skybox_mesh(), static=True)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
for _ in range(10):
2-
rr.log("camera/image", static=True, camera.save_current_frame())
2+
rr.log("camera/image", camera.save_current_frame(), static=True)

docs/snippets/compare_snippet_output.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,21 @@
1111
import sys
1212
import time
1313
from pathlib import Path
14+
from typing import Any
1415

1516
import tomlkit
17+
from tomlkit.container import Container
1618

1719
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/../../scripts/")
1820
from roundtrip_utils import roundtrip_env, run, run_comparison # noqa
1921

2022
config_path = Path(__file__).parent / "snippets.toml"
2123
config = tomlkit.loads(config_path.read_text())
22-
OPT_OUT_ENTIRELY = config["opt_out"]["run"]
23-
OPT_OUT_COMPARE = config["opt_out"]["compare"]
24-
EXTRA_ARGS = config["extra_args"]
24+
assert isinstance(config["opt_out"], Container)
25+
26+
OPT_OUT_ENTIRELY: dict[str, Any] = config["opt_out"]["run"].value
27+
OPT_OUT_COMPARE = config["opt_out"]["compare"].value
28+
EXTRA_ARGS = config["extra_args"].value
2529

2630

2731
class Example:
@@ -32,13 +36,13 @@ def __init__(self, subdir: str, name: str) -> None:
3236
def opt_out_entirely(self) -> list[str]:
3337
for key in [self.subdir, self.subdir + "/" + self.name]:
3438
if key in OPT_OUT_ENTIRELY:
35-
return OPT_OUT_ENTIRELY[key]
39+
return list(OPT_OUT_ENTIRELY[key])
3640
return []
3741

3842
def opt_out_compare(self) -> list[str]:
3943
for key in [self.subdir, self.subdir + "/" + self.name]:
4044
if key in OPT_OUT_COMPARE:
41-
return OPT_OUT_COMPARE[key]
45+
return list(OPT_OUT_COMPARE[key])
4246
return []
4347

4448
def extra_args(self) -> list[str]:

docs/snippets/snippets.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ features = [
7070

7171
# These entries won't run at all.
7272
#
73+
# NOTE: Non-runnable python snippets will have to also be added the `rerun_py/pyproject.toml`
74+
# file as mypy excluded.
75+
#
7376
# You should only ever use this if the test isn't implemented and cannot yet be implemented
7477
# for one or more specific SDKs.
7578
[opt_out.run]

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: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,31 @@ norecursedirs = ".* venv* target* build"
208208

209209
[tool.mypy]
210210
files = [
211+
"docs/snippets",
211212
"rerun_py/rerun_sdk/rerun",
212213
"rerun_py/rerun_bindings",
213214
"rerun_py/tests",
214215
"examples/python",
215216
"scripts",
216217
"tests/python",
217218
]
218-
exclude = ["examples/python/objectron", "examples/python/ros_node"]
219+
exclude = [
220+
# snippets that are not runnable
221+
"docs/snippets/all/concepts/how_helix_was_logged",
222+
"docs/snippets/all/concepts/static/log_static",
223+
"docs/snippets/all/concepts/static/log_static_10x",
224+
"docs/snippets/all/concepts/static/log_temporal_10x",
225+
"docs/snippets/all/concepts/static/send_static",
226+
"docs/snippets/all/concepts/static/send_static_10x",
227+
"docs/snippets/all/migration/log_line",
228+
"docs/snippets/all/tutorials/custom-application-id",
229+
"docs/snippets/all/tutorials/custom-recording-id",
230+
"docs/snippets/all/tutorials/timelines_example",
231+
232+
# hopeless examples
233+
"examples/python/objectron",
234+
"examples/python/ros_node",
235+
]
219236
namespace_packages = true
220237
show_error_codes = true
221238
strict = true

rerun_py/rerun_sdk/rerun/_log.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def component_descriptor(self) -> ComponentDescriptor:
5353

5454
@catch_and_log_exceptions()
5555
def log(
56-
entity_path: str | list[str],
56+
entity_path: str | list[object],
5757
entity: AsComponents | Iterable[DescribedComponentBatch],
5858
*extra: AsComponents | Iterable[DescribedComponentBatch],
5959
static: bool = False,
@@ -172,7 +172,7 @@ def log(
172172

173173

174174
def _log_components(
175-
entity_path: str | list[str],
175+
entity_path: str | list[object],
176176
components: list[DescribedComponentBatch],
177177
*,
178178
static: bool = False,

rerun_py/rerun_sdk/rerun/archetypes/boxes3d_ext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(
2323
rotations: datatypes.RotationAxisAngleArrayLike | datatypes.QuaternionArrayLike | None = None,
2424
colors: datatypes.Rgba32ArrayLike | None = None,
2525
radii: datatypes.Float32ArrayLike | None = None,
26-
fill_mode: components.FillMode | None = None,
26+
fill_mode: components.FillModeLike | None = None,
2727
labels: datatypes.Utf8ArrayLike | None = None,
2828
show_labels: datatypes.BoolLike | None = None,
2929
class_ids: datatypes.ClassIdArrayLike | None = None,

rerun_py/rerun_sdk/rerun/archetypes/depth_image_ext.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from rerun.datatypes.range1d import Range1DLike
99

10-
from ..components import Colormap, ImageFormat
10+
from ..components import ColormapLike, ImageFormat
1111
from ..datatypes import ChannelDatatype, Float32Like
1212

1313
if TYPE_CHECKING:
@@ -46,7 +46,7 @@ def __init__(
4646
image: ImageLike,
4747
*,
4848
meter: Float32Like | None = None,
49-
colormap: Colormap | None = None,
49+
colormap: ColormapLike | None = None,
5050
depth_range: Range1DLike | None = None,
5151
point_fill_ratio: Float32Like | None = None,
5252
draw_order: Float32Like | None = None,

rerun_py/rerun_sdk/rerun/archetypes/ellipsoids3d_ext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def __init__(
2121
quaternions: datatypes.QuaternionArrayLike | None = None,
2222
colors: datatypes.Rgba32ArrayLike | None = None,
2323
line_radii: datatypes.Float32ArrayLike | None = None,
24-
fill_mode: components.FillMode | None = None,
24+
fill_mode: components.FillModeLike | None = None,
2525
labels: datatypes.Utf8ArrayLike | None = None,
2626
show_labels: datatypes.BoolLike | None = None,
2727
class_ids: datatypes.ClassIdArrayLike | None = None,

rerun_py/rerun_sdk/rerun/archetypes/transform3d.py

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/rerun_sdk/rerun/recording_stream.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ def disable_timeline(self, timeline: str) -> None:
960960

961961
def log(
962962
self,
963-
entity_path: str | list[str],
963+
entity_path: str | list[object],
964964
entity: AsComponents | Iterable[DescribedComponentBatch],
965965
*extra: AsComponents | Iterable[DescribedComponentBatch],
966966
static: bool = False,
@@ -970,11 +970,7 @@ def log(
970970
Log data to Rerun.
971971
972972
This is the main entry point for logging data to rerun. It can be used to log anything
973-
that implements the [`rerun.AsComponents`][] interface, or a collection of `ComponentBatchLike`
974-
975-
976-
977-
objects.
973+
that implements the [`rerun.AsComponents`][] interface, or a collection of `ComponentBatchLike` objects.
978974
979975
When logging data, you must always provide an [entity_path](https://www.rerun.io/docs/concepts/entity-path)
980976
for identifying the data. Note that the path prefix "rerun/" is considered reserved for use by the Rerun SDK

0 commit comments

Comments
 (0)