Skip to content

Commit

Permalink
make sure __all__ manifests are lexically sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc committed Jun 13, 2023
1 parent 01fc48a commit 28df80b
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions crates/re_types_builder/src/codegen/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ fn quote_lib(out_path: impl AsRef<Path>, archetype_names: &[String]) -> PathBuf
.unwrap();

let path = out_path.join("__init__.py");
let all_names = archetype_names
.iter()
.map(|name| format!("{name:?}"))
.collect::<Vec<_>>()
.join(", ");
let manifest = quote_manifest(archetype_names);
let archetype_names = archetype_names.join(", ");

let mut code = String::new();
Expand All @@ -108,7 +104,7 @@ fn quote_lib(out_path: impl AsRef<Path>, archetype_names: &[String]) -> PathBuf
from __future__ import annotations
__all__ = [{all_names}]
__all__ = [{manifest}]
from .archetypes import {archetype_names}
"#
Expand Down Expand Up @@ -196,16 +192,12 @@ fn quote_objects(
let mut code = String::new();
code.push_str(&format!("# {AUTOGEN_WARNING}\n\n"));

let names = names
.into_iter()
.map(|name| format!("{name:?}"))
.collect::<Vec<_>>()
.join(", ");
let manifest = quote_manifest(names);
code.push_str(&unindent::unindent(&format!(
"
from __future__ import annotations
__all__ = [{names}]
__all__ = [{manifest}]
",
)));
Expand All @@ -225,18 +217,14 @@ fn quote_objects(

let mut code = String::new();

let all_names = mods
.iter()
.flat_map(|(_, names)| names.iter().map(|name| format!("{name:?}")))
.collect::<Vec<_>>()
.join(", ");
let manifest = quote_manifest(mods.iter().flat_map(|(_, names)| names.iter()));

code.push_str(&format!("# {AUTOGEN_WARNING}\n\n"));
code.push_str(&unindent::unindent(&format!(
"
from __future__ import annotations
__all__ = [{all_names}]
__all__ = [{manifest}]
# NOTE: we use fully qualified paths to prevent lazy circular imports.
",
Expand Down Expand Up @@ -472,6 +460,16 @@ impl QuotedObject {

// --- Code generators ---

fn quote_manifest(names: impl IntoIterator<Item = impl AsRef<str>>) -> String {
let mut quoted_names: Vec<_> = names
.into_iter()
.map(|name| format!("{:?}", name.as_ref()))
.collect();
quoted_names.sort();

quoted_names.join(", ")
}

fn quote_module_prelude() -> String {
// NOTE: All the extraneous stuff will be cleaned up courtesy of `ruff`.
unindent::unindent(
Expand Down

0 comments on commit 28df80b

Please sign in to comment.