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

Python codegen: big cleaning and paving the way towards transforms #2603

Merged
merged 19 commits into from
Jul 5, 2023
Merged
Changes from 1 commit
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
46 changes: 46 additions & 0 deletions rerun_py/rerun_sdk/rerun/_rerun2/_baseclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ class BaseExtensionArray(NamedExtensionArray, Generic[T]): # type: ignore[misc]

@classmethod
def from_similar(cls, data: T | None) -> BaseExtensionArray[T]:
"""
Primary method for creating Arrow arrays for components.

This method must flexibly accept native data (which comply with type `T`). Subclasses must provide a type
parameter specifying the type of the native data (this is automatically handled by the code generator).

The actual creation of the Arrow array is delegated to the `_native_to_pa_array()` method, which is not
implemented by default.

Parameters
----------
data : T | None
The data to convert into an Arrow array.

Returns
-------
The Arrow array encapsulating the data.
"""
data_type = cls._EXTENSION_TYPE()

if data is None:
Expand All @@ -79,6 +97,34 @@ def from_similar(cls, data: T | None) -> BaseExtensionArray[T]:

@staticmethod
def _native_to_pa_array(data: T, data_type: pa.DataType) -> pa.Array:
"""
Converts native data into an Arrow array.

Subclasses must provide an implementation of this method (via an override) if they are to be used as either
an archetype's field (which should be the case for all components), or a (delegating) component's field (for
datatypes). Datatypes which are used only within other datatypes may omit implementing this method, provided
that the top-level datatype implements it.

A hand-coded override must be provided for the code generator to implement this method. The override must be
named `xxx_native_to_pa_array()`, where `xxx` is the lowercase name of the datatype. The override must be
located in the `_overrides` subpackage and *explicitly* imported by `_overrides/__init__.py` (to be noticed
by the code generator).

`color_native_to_pa_array()` in `_overrides/color.py` is a good example of how to implement this method, in
conjunction with the native type's converter (see `color_converter()`, used to construct the native `Color`
object).

Parameters
----------
data : T
The data to convert into an Arrow array.
data_type : pa.DataType
The Arrow data type of the data.

Returns
-------
The Arrow array encapsulating the data.
"""
Comment on lines +100 to +127
Copy link
Member

Choose a reason for hiding this comment

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

nice

raise NotImplementedError


Expand Down