Skip to content

Python SDK spring cleaning: 3.9, no more monkey patching, more lints #9182

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 11 commits into from
Mar 5, 2025
2 changes: 2 additions & 0 deletions pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,8 @@ pygithub = "==1.59.0" # Among others for `sync_release_assets.py`.
requests = ">=2.31,<3" # For `thumbnails.py` & `upload_image.py`
types-Deprecated = "==1.2.9.2" # Type hint stubs
types-requests = ">=2.31,<3" # Type hint stubs
types-decorator = "*" # Type hint stubs
types-pytz = "*" # Type hint stubs
parso = ">=0.8.4, <0.9"

[feature.wheel-build.dependencies]
Expand Down
41 changes: 1 addition & 40 deletions rerun_py/rerun_sdk/rerun/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,51 +203,12 @@
"""


def _init_recording_stream() -> None:
# Inject all relevant methods into the `RecordingStream` class.
# We need to do this from here to avoid circular import issues.

import sys
from inspect import getmembers, isfunction

from rerun.recording_stream import _patch as recording_stream_patch

recording_stream_patch(
[
binary_stream,
connect,
connect_grpc,
save,
stdout,
disconnect,
memory_recording,
serve,
spawn,
send_blueprint,
notebook_show,
]
+ [
set_index,
set_time_sequence,
set_time_seconds,
set_time_nanos,
disable_timeline,
reset_time,
log,
]
+ [fn for name, fn in getmembers(sys.modules[__name__], isfunction) if name.startswith("log_")]
)


_init_recording_stream()


# TODO(#3793): defaulting recording_id to authkey should be opt-in
def init(
application_id: str,
*,
recording_id: str | UUID | None = None,
spawn: bool = False,
spawn: bool = False, # noqa: F811
init_logging: bool = True,
default_enabled: bool = True,
strict: bool | None = None,
Expand Down
24 changes: 12 additions & 12 deletions rerun_py/rerun_sdk/rerun/blueprint/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def __init__(
contents: ViewContentsLike,
name: Utf8Like | None,
visible: BoolLike | None = None,
properties: dict[str, AsComponents] = {},
defaults: list[Union[AsComponents, ComponentBatchLike]] = [],
overrides: dict[EntityPathLike, list[ComponentBatchLike]] = {},
properties: dict[str, AsComponents] | None = None,
defaults: list[Union[AsComponents, ComponentBatchLike]] | None = None,
overrides: dict[EntityPathLike, list[ComponentBatchLike]] | None = None,
):
"""
Construct a blueprint for a new view.
Expand Down Expand Up @@ -88,9 +88,9 @@ def __init__(
self.origin = origin
self.contents = contents
self.visible = visible
self.properties = properties
self.defaults = defaults
self.overrides = overrides
self.properties = properties if properties is not None else {}
self.defaults = defaults if defaults is not None else []
self.overrides = overrides if overrides is not None else {}
Copy link
Member

Choose a reason for hiding this comment

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

Sometimes I wish I was writing Lua and could just write overrides or {} instead of all this verbosity…

Copy link
Member Author

Choose a reason for hiding this comment

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

Well you can, but I don't like it much because of how many things are falsy in python.


def blueprint_path(self) -> str:
"""
Expand Down Expand Up @@ -120,7 +120,7 @@ def _log_to_stream(self, stream: RecordingStream) -> None:
# Otherwise we delegate to the ViewContents constructor
contents = ViewContents(query=self.contents) # type: ignore[arg-type]

stream.log(self.blueprint_path() + "/ViewContents", contents) # type: ignore[attr-defined]
stream.log(self.blueprint_path() + "/ViewContents", contents)

arch = ViewBlueprint(
class_identifier=self.class_identifier,
Expand All @@ -129,22 +129,22 @@ def _log_to_stream(self, stream: RecordingStream) -> None:
visible=self.visible,
)

stream.log(self.blueprint_path(), arch, recording=stream) # type: ignore[attr-defined]
stream.log(self.blueprint_path(), arch) # type: ignore[attr-defined]

for prop_name, prop in self.properties.items():
stream.log(f"{self.blueprint_path()}/{prop_name}", prop, recording=stream) # type: ignore[attr-defined]
stream.log(f"{self.blueprint_path()}/{prop_name}", prop)

for default in self.defaults:
if hasattr(default, "as_component_batches"):
stream.log(f"{self.blueprint_path()}/defaults", default, recording=stream) # type: ignore[attr-defined]
stream.log(f"{self.blueprint_path()}/defaults", default)
elif hasattr(default, "component_descriptor"):
stream.log(f"{self.blueprint_path()}/defaults", [default], recording=stream) # type: ignore[attr-defined]
stream.log(f"{self.blueprint_path()}/defaults", [default])
else:
raise ValueError(f"Provided default: {default} is neither a component nor a component batch.")

for path, components in self.overrides.items():
stream.log( # type: ignore[attr-defined]
f"{self.blueprint_path()}/ViewContents/individual_overrides/{path}", components, recording=stream
f"{self.blueprint_path()}/ViewContents/individual_overrides/{path}", components
)

def _ipython_display_(self) -> None:
Expand Down
5 changes: 4 additions & 1 deletion rerun_py/rerun_sdk/rerun/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

from __future__ import annotations

from typing import TYPE_CHECKING

from rerun import bindings

from .recording_stream import RecordingStream
if TYPE_CHECKING:
from .recording_stream import RecordingStream


def memory_recording(recording: RecordingStream | None = None) -> MemoryRecording:
Expand Down
Loading