Skip to content

Commit

Permalink
Fix codegen of python scoped objects to handle mixed-scope imports (#…
Browse files Browse the repository at this point in the history
…5026)

### What
Previously importing scopes would overlay the `datatypes` /
`components`.

We now just import the scope and instead adjust the references. This
allows an archetype to contain both "regular" components as well as
blueprint components.

### 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 the web demo (if applicable):
* Using newly built examples:
[app.rerun.io](https://app.rerun.io/pr/5026/index.html)
* Using examples from latest `main` build:
[app.rerun.io](https://app.rerun.io/pr/5026/index.html?manifest_url=https://app.rerun.io/version/main/examples_manifest.json)
* Using full set of examples from `nightly` build:
[app.rerun.io](https://app.rerun.io/pr/5026/index.html?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG

- [PR Build Summary](https://build.rerun.io/pr/5026)
- [Docs
preview](https://rerun.io/preview/6a0878c81c8253740c84d56d317febcb6f67aa95/docs)
<!--DOCS-PREVIEW-->
- [Examples
preview](https://rerun.io/preview/6a0878c81c8253740c84d56d317febcb6f67aa95/examples)
<!--EXAMPLES-PREVIEW-->
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)
  • Loading branch information
jleibs authored Feb 4, 2024
1 parent 5aa27d6 commit 6bfd0e4
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 78 deletions.
33 changes: 23 additions & 10 deletions crates/re_types_builder/src/codegen/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,13 @@ fn code_for_struct(
// Delegating component inheritance comes after the `ExtensionClass`
// This way if a component needs to override `__init__` it still can.
if obj.is_delegating_component() {
let delegate = obj.delegate_datatype(objects).unwrap();
let scope = match delegate.scope() {
Some(scope) => format!("{scope}."),
None => String::new(),
};
superclasses.push(format!(
"datatypes.{}",
"{scope}datatypes.{}",
obj.delegate_datatype(objects).unwrap().name
));
}
Expand Down Expand Up @@ -1286,17 +1291,17 @@ fn quote_import_clauses_from_fqname(obj_scope: &Option<String>, fqname: &str) ->
if from.starts_with("rerun.datatypes") {
"from ... import datatypes".to_owned() // NOLINT
} else if from.starts_with(format!("rerun.{scope}.datatypes").as_str()) {
"from .. import datatypes".to_owned()
format!("from ... import {scope}")
} else if from.starts_with("rerun.components") {
"from ... import components".to_owned() // NOLINT
} else if from.starts_with(format!("rerun.{scope}.components").as_str()) {
"from .. import components".to_owned()
format!("from ... import {scope}")
} else if from.starts_with("rerun.archetypes") {
// NOTE: This is assuming importing other archetypes is legal… which whether it is or
// isn't for this code generator to say.
"from ... import archetypes".to_owned() // NOLINT
} else if from.starts_with(format!("rerun.{scope}.archetytpes").as_str()) {
"from .. import archetypes".to_owned()
"from .. import {scope}".to_owned()
} else if from.is_empty() {
format!("from . import {class}")
} else {
Expand Down Expand Up @@ -1502,9 +1507,9 @@ fn fqname_to_type(fqname: &str) -> String {
["rerun", "datatypes", name] => format!("datatypes.{name}"),
["rerun", "components", name] => format!("components.{name}"),
["rerun", "archetypes", name] => format!("archetypes.{name}"),
["rerun", _scope, "datatypes", name] => format!("datatypes.{name}"),
["rerun", _scope, "components", name] => format!("components.{name}"),
["rerun", _scope, "archetypes", name] => format!("archetypes.{name}"),
["rerun", scope, "datatypes", name] => format!("{scope}.datatypes.{name}"),
["rerun", scope, "components", name] => format!("{scope}.components.{name}"),
["rerun", scope, "archetypes", name] => format!("{scope}.archetypes.{name}"),
_ => {
panic!("Unexpected fqname: {fqname}");
}
Expand Down Expand Up @@ -1554,7 +1559,11 @@ fn quote_arrow_support_from_obj(
let mut batch_superclasses: Vec<String> = vec![];

let many_aliases = if let Some(data_type) = obj.delegate_datatype(objects) {
format!("datatypes.{}ArrayLike", data_type.name)
let scope = match data_type.scope() {
Some(scope) => format!("{scope}."),
None => String::new(),
};
format!("{scope}datatypes{}ArrayLike", data_type.name)
} else {
format!("{name}ArrayLike")
};
Expand All @@ -1564,8 +1573,12 @@ fn quote_arrow_support_from_obj(
batch_superclasses.push(format!("BaseBatch[{many_aliases}]"));
} else if obj.kind == ObjectKind::Component {
if let Some(data_type) = obj.delegate_datatype(objects) {
let data_extension_type = format!("datatypes.{}Type", data_type.name);
let data_extension_array = format!("datatypes.{}Batch", data_type.name);
let scope = match data_type.scope() {
Some(scope) => format!("{scope}."),
None => String::new(),
};
let data_extension_type = format!("{scope}datatypes.{}Type", data_type.name);
let data_extension_array = format!("{scope}datatypes.{}Batch", data_type.name);
type_superclasses.push(data_extension_type);
batch_superclasses.push(data_extension_array);
} else {
Expand Down
5 changes: 5 additions & 0 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.

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

15 changes: 9 additions & 6 deletions rerun_py/rerun_sdk/rerun/blueprint/archetypes/plot_legend.py

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

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

Loading

0 comments on commit 6bfd0e4

Please sign in to comment.