-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python codegen: big cleaning and paving the way towards transforms (#…
…2603) ### What **Note**: as usual, review commit-by-commit (the bulk of the changes are in the first commit). This time, reviewers might want to spend some time on the auto-generated code, though, as this is a major focus of this PR. This PR has the following aims: - Enforce and exploit the decision that a component must be a struct of one, and exactly one, field, which may be a datatype (in which case the component is known as "delegating", as its implementation delegates to the datatype) or a native type (e.g. float, int, etc.). - Significantly polish the Python SDK, with the goal of a cleaner, fully typed user-facing API, and a streamlined implementation. Part of this is achieved using `attrs` (see below). - Improve the `from_similar()` hook and introduce several others. The proposed mechanisms consist of storing the so-called (hand-coded) overrides in the `_overrides` sub-packages in each of the `archetypes`, `components`, and `datatypes` packages. These overrides are discovered by the code generator by "parsing" (well, "brute-force extracting symbols" from) `_overrides/__init__.py` [1]. The following hooks are currently implemented: - `xxx_native_to_pa_array()` (where `xxx` is the lower-cased name of the corresponding object): this is the primary way to flexibly convert native input into the Arrow array form of the component. - `xxx_yyy_converter` (`yyy` is a field name): optional converter function a given field of a native objects (e.g. `Color`). This enables the object to flexibly accept native input for the given field. - `xxx_as_array`: optional `__array__()` magic function for native objects (e.g. `Point2D`). This function enable the object to be interpreted as array data by Numpy, which can considerably simplify the implementation of, e.g., `xxx_native_to_pa_array` (`Point2D` is a good example). This PR also introduces the following changes: - So far, the archetypes and `log_any()` were pulled into the `rr` namespace. This PR now pulls all component- and datatype-related symbols in `rr.cmp` and `rr.dt`, respectively. This is intended as a temporary solution, to avoid name clashes with the legacy API [2]. - The [`attrs`](https://www.attrs.org/en/stable/) dependency is introduced as it allows much more flexibility than the standard library `dataclass`, in particular with the `converter` feature which I use extensively. Introducing new deps is always a tough call. In the case of `attrs`, it's most probably safe as this is one of the most common and well maintained package of the ecosystem (110+M dl/month). - Native objects automatically get `__str__()`, `__int__()`, or `__float__()` functions when they contain a single field of the related type. - The `Point2D` component now delegate to a new `Point2D` datatype, defined as a struct of two floats. This is done such as to avoid breaking compatibility with the current viewer. The long-term goal is to use the `Vec2D` datatype instead. - The CI now uses Python 3.11 for linting (and linting only, i.e. not testing, etc.) Addressing union types is a non-goal of this PR. This will be part of the next PR (transforms). As such `QuotedObject::from_union()` is most likely broken. TODO/TBD: - PyCharm's typechecker isn't happy with the Archetype, with is irritating. This could apparently be addressed by using `xxxArrayLike` instead of `xxxArray` annotations for archetype's fields. I'm curious how VSCode fares, though. - Maybe passing `None` as args to the archetype should set `None` instead of empty arrays (they aren't logged either way). - Maybe the notion of delegating vs. non-delegating component should be promoted to the langauge-agnostic code? [1] I actually prototyped and tested an approach based on PyO3. It sorta worked, until I tried to import stuff (Numpy, ...) for the overrides. The codegen would somehow be given access to a venv with the required deps, etc. Hell no. [2] Name clashes also exist internally to the new API, e.g. `components.Point2D` vs. `datatypes.Point2D`. This will have to be addressed before we can pull "everything" into the `rr` namespace. ### Checklist * [x] I have read and agree to [Contributor Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and the [Code of Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md) * [x] I've included a screenshot or gif (if applicable) * [x] I have tested [demo.rerun.io](https://demo.rerun.io/pr/2603) (if applicable) - [PR Build Summary](https://build.rerun.io/pr/2603) - [Docs preview](https://rerun.io/preview/pr%3Aantoine%2Fhope-python-cleanup-new/docs) - [Examples preview](https://rerun.io/preview/pr%3Aantoine%2Fhope-python-cleanup-new/examples) --------- Co-authored-by: Clement Rey <[email protected]>
- Loading branch information
Showing
85 changed files
with
4,948 additions
and
2,925 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
include "./datatypes/point2d.fbs"; | ||
include "./datatypes/vec2d.fbs"; | ||
|
||
namespace rerun.datatypes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
include "arrow/attributes.fbs"; | ||
include "python/attributes.fbs"; | ||
include "fbs/attributes.fbs"; | ||
include "rust/attributes.fbs"; | ||
|
||
namespace rerun.datatypes; | ||
|
||
// --- | ||
|
||
/// A point in 2D space. | ||
struct Point2D ( | ||
"attr.python.aliases": "Sequence[float]", | ||
"attr.python.array_aliases": "npt.NDArray[np.float32], Sequence[npt.NDArray[np.float32]], Sequence[Tuple[float, float]], Sequence[float]", | ||
"attr.rust.derive": "Debug, Default, Clone, Copy, PartialEq, PartialOrd", | ||
order: 100 | ||
) { | ||
x: float (order: 100); | ||
y: float (order: 200); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# This is a sha256 hash for all direct and indirect dependencies of this crate's build script. | ||
# It can be safely removed at anytime to force the build script to run again. | ||
# Check out build.rs to see how it's computed. | ||
d828fdb05c35a01b92dd30b7ce40987b87c5dfe8d1bb8f729004ae88b62c830c | ||
dec6b10b9e90c9c95b3e1436c1b53987573319efd121cc5e715c31466163b0bc |
Oops, something went wrong.
59b20e9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible performance regression was detected for benchmark 'Rust Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold
1.25
.mono_points_arrow_batched/decode_message_bundles
11454228
ns/iter (± 5099078
)7419857
ns/iter (± 6645
)1.54
This comment was automatically generated by workflow using github-action-benchmark.