Skip to content
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

Implement __init__.py lazy loading for codegen'd objects #4058

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 31 additions & 6 deletions crates/re_types_builder/src/codegen/python.rs
Expand Up @@ -442,16 +442,41 @@ fn write_init_file(
"
from __future__ import annotations

from typing import TYPE_CHECKING, Any
",
0,
1,
);
for (module, names) in mods {
let names = names.join(", ");
code.push_text(&format!("from .{module} import {names}"), 1, 0);
}

if !manifest.is_empty() {
code.push_unindented_text(format!("\n__all__ = [{manifest}]"), 0);
code.push_unindented_text("if TYPE_CHECKING:", 1);
for (module, names) in mods {
let names = names.join(", ");
code.push_text(&format!("from .{module} import {names}"), 1, 4);
}

code.push_unindented_text(format!("\n\n__all__ = [{manifest}]"), 1);
}

code.push_text("\nmodule_content: dict[str, str] = {", 1, 0);
for (module, names) in mods {
for name in names {
code.push_text(&format!("{name:?}: {module:?},"), 1, 4);
}
}
code.push_unindented_text("}", 1);

code.push_unindented_text(
r#"
def __getattr__(name: str) -> Any:
from importlib import import_module
if name in module_content:
module = import_module(f".{module_content[name]}", __name__)
return getattr(module, name)
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
"#,
0,
);

files_to_write.insert(path, code);
}

Expand Down
102 changes: 60 additions & 42 deletions rerun_py/rerun_sdk/rerun/__init__.py
Expand Up @@ -2,7 +2,7 @@

import functools
import random
from typing import Any, Callable, TypeVar, cast
from typing import TYPE_CHECKING, Any, Callable, TypeVar, cast
from uuid import UUID

import numpy as np
Expand Down Expand Up @@ -126,48 +126,51 @@
from ._image import ImageEncoded, ImageFormat
from ._log import AsComponents, ComponentBatchLike, IndicatorComponentBatch, log, log_components
from .any_value import AnyValues
from .archetypes import (
AnnotationContext,
Arrows3D,
Asset3D,
BarChart,
Boxes2D,
Boxes3D,
Clear,
DepthImage,
DisconnectedSpace,
Image,
LineStrips2D,
LineStrips3D,
Mesh3D,
Pinhole,
Points2D,
Points3D,
SegmentationImage,
Tensor,
TextDocument,
TextLog,
TimeSeriesScalar,
Transform3D,
ViewCoordinates,
)

if TYPE_CHECKING:
from .archetypes import (
AnnotationContext,
Arrows3D,
Asset3D,
BarChart,
Boxes2D,
Boxes3D,
Clear,
DepthImage,
DisconnectedSpace,
Image,
LineStrips2D,
LineStrips3D,
Mesh3D,
Pinhole,
Points2D,
Points3D,
SegmentationImage,
Tensor,
TextDocument,
TextLog,
TimeSeriesScalar,
Transform3D,
ViewCoordinates,
)
from .components import (
Material,
MediaType,
MeshProperties,
OutOfTreeTransform3D,
OutOfTreeTransform3DBatch,
TextLogLevel,
)
from .datatypes import (
Quaternion,
RotationAxisAngle,
Scale3D,
TensorData,
TranslationAndMat3x3,
TranslationRotationScale3D,
)

from .archetypes.boxes2d_ext import Box2DFormat
from .components import (
Material,
MediaType,
MeshProperties,
OutOfTreeTransform3D,
OutOfTreeTransform3DBatch,
TextLogLevel,
)
from .datatypes import (
Quaternion,
RotationAxisAngle,
Scale3D,
TensorData,
TranslationAndMat3x3,
TranslationRotationScale3D,
)
from .error_utils import set_strict_mode
from .log_deprecated.annotation import AnnotationInfo, ClassDescription, log_annotation_context
from .log_deprecated.arrow import log_arrow
Expand Down Expand Up @@ -213,6 +216,21 @@
from . import experimental # isort: skip


def __getattr__(name: str) -> Any:
from . import archetypes, components, datatypes

if name in archetypes.module_content:
return getattr(archetypes, name)

if name in components.module_content:
return getattr(components, name)

if name in datatypes.module_content:
return getattr(datatypes, name)

raise AttributeError(f"module {__name__!r} has no attribute {name!r}")


# =====================================
# UTILITIES

Expand Down
84 changes: 61 additions & 23 deletions rerun_py/rerun_sdk/rerun/archetypes/__init__.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 69 additions & 29 deletions rerun_py/rerun_sdk/rerun/blueprint/__init__.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.