alef 0.85.21

Opinionated polyglot binding generator for Rust libraries
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use super::gen_enum_stub;
use crate::core::ir::{CoreWrapper, EnumDef, EnumVariant, FieldDef, MethodDef, PrimitiveType, TypeRef};
use ahash::AHashSet;

/// No dataclass-backed config DTOs — factory params map exactly as `python_type` would.
fn no_dtos() -> AHashSet<&'static str> {
    AHashSet::new()
}

#[test]
fn builtin_named_factory_qualifies_sibling_factory_annotations() {
    let enum_def = EnumDef {
        name: "BinaryPayload".to_string(),
        variants: vec![
            EnumVariant {
                name: "Bytes".to_string(),
                fields: vec![field("marker", TypeRef::Primitive(PrimitiveType::U8))],
                ..Default::default()
            },
            EnumVariant {
                name: "Data".to_string(),
                fields: vec![field("payload", TypeRef::Bytes)],
                ..Default::default()
            },
        ],
        ..Default::default()
    };

    let stub = gen_enum_stub(&enum_def, false, &no_dtos(), true);

    assert!(
        stub.contains("def bytes("),
        "fixture must emit the shadowing factory:\n{stub}"
    );
    assert!(
        stub.contains("payload: builtins.bytes"),
        "factory names shadow builtin annotations across the enum class:\n{stub}"
    );
}

fn field(name: &str, ty: TypeRef) -> FieldDef {
    FieldDef {
        version: Default::default(),
        name: name.to_string(),
        ty,
        optional: false,
        default: None,
        doc: String::new(),
        sanitized: false,
        is_boxed: false,
        type_rust_path: None,
        cfg: None,
        typed_default: None,
        core_wrapper: CoreWrapper::None,
        vec_inner_core_wrapper: CoreWrapper::None,
        newtype_wrapper: None,
        serde_rename: None,
        serde_flatten: false,
        serde_with: None,
        serde_skip_serializing_if: false,
        serde_skip: false,
        binding_excluded: false,
        binding_exclusion_reason: None,
        original_type: None,
    }
}

fn optional_field(name: &str, ty: TypeRef) -> FieldDef {
    FieldDef {
        optional: true,
        ..field(name, ty)
    }
}

fn variant(name: &str, fields: Vec<FieldDef>) -> EnumVariant {
    EnumVariant {
        serde_untagged: false,
        name: name.to_string(),
        fields,
        doc: String::new(),
        is_default: false,
        serde_rename: None,
        binding_excluded: false,
        binding_exclusion_reason: None,
        is_tuple: false,
        originally_had_data_fields: false,
        cfg: None,
        version: Default::default(),
    }
}

fn enum_def(name: &str, variants: Vec<EnumVariant>) -> EnumDef {
    EnumDef {
        name: name.to_string(),
        rust_path: format!("test_lib::{name}"),
        original_rust_path: String::new(),
        variants,
        methods: vec![],
        doc: String::new(),
        cfg: None,
        is_copy: false,
        has_serde: true,
        has_default: false,
        serde_content: None,
        serde_tag: Some("type".to_string()),
        serde_untagged: false,
        serde_rename_all: None,
        rename_all_fields: None,
        binding_excluded: false,
        binding_exclusion_reason: None,
        excluded_variants: vec![],
        version: Default::default(),
    }
}

fn shape_enum() -> EnumDef {
    enum_def(
        "Shape",
        vec![
            variant("Circle", vec![field("radius", TypeRef::Primitive(PrimitiveType::F64))]),
            variant(
                "Rect",
                vec![
                    field("width", TypeRef::Primitive(PrimitiveType::U32)),
                    field("height", TypeRef::Primitive(PrimitiveType::U32)),
                ],
            ),
        ],
    )
}

#[test]
fn emits_staticmethod_constructor_per_struct_variant() {
    let stub = gen_enum_stub(&shape_enum(), false, &no_dtos(), true);

    assert!(stub.contains("class Shape:"), "{stub}");
    assert!(stub.contains("    type: str"), "{stub}");
    assert!(
        stub.contains("    @staticmethod\n    def circle(radius: float) -> Shape: ..."),
        "{stub}"
    );
    assert!(
        stub.contains("    @staticmethod\n    def rect(width: int, height: int) -> Shape: ..."),
        "{stub}"
    );
    let circle_at = stub.find("def circle").unwrap();
    let str_at = stub.find("def __str__").unwrap();
    assert!(circle_at < str_at, "constructors must precede dunders: {stub}");
}

fn cfg_shape_enum() -> EnumDef {
    let mut gated = variant(
        "Rect",
        vec![
            field("width", TypeRef::Primitive(PrimitiveType::U32)),
            field("height", TypeRef::Primitive(PrimitiveType::U32)),
        ],
    );
    gated.cfg = Some(r#"feature = "extra-shapes""#.to_string());
    enum_def(
        "Shape",
        vec![
            variant("Circle", vec![field("radius", TypeRef::Primitive(PrimitiveType::F64))]),
            gated,
        ],
    )
}

/// The stub must not advertise a `@staticmethod` the runtime binding drops for the same reason
/// (`gen_pyo3_enum_variant_constructors_content`, `codegen::generators::enums`): a FOREIGN
/// cfg-gated variant's factory is unreachable (compiled out via `#[cfg(...)]` naming an undeclared
/// feature) and dropped unconditionally.
#[test]
fn drops_staticmethod_for_foreign_cfg_gated_variant() {
    let stub = gen_enum_stub(&cfg_shape_enum(), false, &no_dtos(), false);

    assert!(!stub.contains("def rect("), "{stub}");
    assert!(
        stub.contains("    @staticmethod\n    def circle(radius: float) -> Shape: ..."),
        "{stub}"
    );
}

/// Control: a host-owned cfg-gated variant's factory must stay documented.
#[test]
fn keeps_staticmethod_for_host_owned_cfg_gated_variant() {
    let stub = gen_enum_stub(&cfg_shape_enum(), false, &no_dtos(), true);

    assert!(
        stub.contains("    @staticmethod\n    def rect(width: int, height: int) -> Shape: ..."),
        "{stub}"
    );
}

#[test]
fn dunder_stubs_carry_no_unused_noqa() {
    let stub = gen_enum_stub(&shape_enum(), false, &no_dtos(), true);

    assert!(stub.contains("    def __str__(self) -> str: ..."), "{stub}");
    assert!(stub.contains("    def __repr__(self) -> str: ..."), "{stub}");
    // A `# noqa: PYI029` here is flagged by ruff RUF100 (unused, PYI029 not enabled).
    assert!(
        !stub.contains("noqa"),
        "dunder stubs must not carry a suppression comment: {stub}"
    );
}

#[test]
fn maps_named_dto_field_to_its_type() {
    let def = enum_def(
        "Source",
        vec![variant(
            "Llm",
            vec![field("config", TypeRef::Named("LlmConfig".to_string()))],
        )],
    );

    let stub = gen_enum_stub(&def, false, &no_dtos(), true);

    assert!(
        stub.contains("    @staticmethod\n    def llm(config: LlmConfig) -> Source: ..."),
        "{stub}"
    );
}

#[test]
fn widens_dataclass_backed_config_dto_factory_param() {
    let def = enum_def(
        "EmbeddingModelType",
        vec![
            variant("Llm", vec![field("llm", TypeRef::Named("LlmConfig".to_string()))]),
            variant("Preset", vec![field("name", TypeRef::String)]),
        ],
    );
    let coercible: AHashSet<&str> = ["LlmConfig"].into_iter().collect();

    let stub = gen_enum_stub(&def, false, &coercible, true);

    assert!(
        stub.contains(
            "    @staticmethod\n    def llm(llm: options.LlmConfig | dict[str, Any]) -> EmbeddingModelType: ..."
        ),
        "coercible DTO factory param must accept the public dataclass or a dict: {stub}"
    );
    assert!(
        stub.contains("    @staticmethod\n    def preset(name: str) -> EmbeddingModelType: ..."),
        "primitive factory param must be unchanged: {stub}"
    );
}

#[test]
fn qualifies_builtin_shadowed_by_a_variant_factory_name() {
    let def = enum_def(
        "NodeContent",
        vec![
            variant("List", vec![field("ordered", TypeRef::String)]),
            variant(
                "MetadataBlock",
                vec![field(
                    "entries",
                    TypeRef::Vec(Box::new(TypeRef::Named("MetadataEntry".to_string()))),
                )],
            ),
        ],
    );

    let stub = gen_enum_stub(&def, false, &no_dtos(), true);

    assert!(
        stub.contains("    @staticmethod\n    def list(ordered: str) -> NodeContent: ..."),
        "{stub}"
    );
    assert!(
        stub.contains("def metadata_block(entries: builtins.list[MetadataEntry]) -> NodeContent: ..."),
        "builtin shadowed by the `list` factory must be qualified as builtins.list: {stub}"
    );
}

#[test]
fn skips_unit_tuple_excluded_and_sanitized_variants() {
    let mut tuple_variant = variant("Pair", vec![field("_0", TypeRef::String)]);
    tuple_variant.is_tuple = true;
    let mut excluded = variant("Hidden", vec![field("value", TypeRef::String)]);
    excluded.binding_excluded = true;
    let mut sanitized_field = field("raw", TypeRef::String);
    sanitized_field.sanitized = true;
    let sanitized_variant = variant("Raw", vec![sanitized_field]);

    let def = enum_def(
        "Shape",
        vec![
            variant("Empty", vec![]),
            tuple_variant,
            excluded,
            sanitized_variant,
            variant("Real", vec![field("value", TypeRef::String)]),
        ],
    );

    let stub = gen_enum_stub(&def, false, &no_dtos(), true);

    assert!(!stub.contains("def empty("), "{stub}");
    assert!(!stub.contains("def pair("), "{stub}");
    assert!(!stub.contains("def hidden("), "{stub}");
    assert!(!stub.contains("def raw("), "{stub}");
    assert!(
        stub.contains("    @staticmethod\n    def real(value: str) -> Shape: ..."),
        "{stub}"
    );
}

#[test]
fn optional_field_is_nilable_with_default() {
    let def = enum_def(
        "Source",
        vec![variant("Tag", vec![optional_field("label", TypeRef::String)])],
    );

    let stub = gen_enum_stub(&def, false, &no_dtos(), true);

    assert!(
        stub.contains("    @staticmethod\n    def tag(label: str | None = None) -> Source: ..."),
        "{stub}"
    );
}

#[test]
fn param_after_optional_is_promoted_to_nilable() {
    let def = enum_def(
        "Shape",
        vec![variant(
            "Ring",
            vec![
                optional_field("radius", TypeRef::Primitive(PrimitiveType::F64)),
                field("width", TypeRef::Primitive(PrimitiveType::U32)),
            ],
        )],
    );

    let stub = gen_enum_stub(&def, false, &no_dtos(), true);

    assert!(
        stub.contains(
            "    @staticmethod\n    def ring(radius: float | None = None, width: int | None = None) -> Shape: ..."
        ),
        "{stub}"
    );
}

/// Regression for the `ContentPart` bug: the runtime pyo3 binding never forwards
/// `enum_def.methods` (a hand-written inherent static method from a separate
/// `impl EnumType { .. }` block) into the generated `#[pymethods]` block, so skipping the derived
/// factory stub on a name collision matched a runtime binding that also dropped the constructor —
/// `ContentPart.text(...)` raised `AttributeError`. The stub must keep emitting the derived
/// factory, matching the now-fixed runtime binding.
#[test]
fn emits_factory_stub_even_with_colliding_hand_written_method() {
    let def = EnumDef {
        methods: vec![MethodDef {
            name: "circle".to_string(),
            is_static: true,
            ..Default::default()
        }],
        ..shape_enum()
    };

    let stub = gen_enum_stub(&def, false, &no_dtos(), true);

    assert!(
        stub.contains("    @staticmethod\n    def circle(radius: float) -> Shape: ..."),
        "circle factory stub must stay reachable despite the colliding hand-written method: {stub}"
    );
    assert!(
        stub.contains("    @staticmethod\n    def rect(width: int, height: int) -> Shape: ..."),
        "{stub}"
    );
}

/// The ground truth for the stub below: serde decides where an adjacently tagged variant's payload
/// goes, and the TypedDict has to describe that document rather than the internal-tagged one.
#[derive(serde::Serialize)]
#[serde(tag = "kind", content = "body")]
enum AdjacentShape {
    Circle { radius: f64 },
}

fn adjacent_shape_enum() -> EnumDef {
    EnumDef {
        serde_content: Some("body".to_string()),
        serde_tag: Some("kind".to_string()),
        ..enum_def(
            "AdjacentShape",
            vec![
                variant("Empty", vec![]),
                variant("Circle", vec![field("radius", TypeRef::Primitive(PrimitiveType::F64))]),
            ],
        )
    }
}

#[test]
fn adjacent_variant_typeddict_nests_the_payload_under_the_content_key() {
    let stub = gen_enum_stub(&adjacent_shape_enum(), false, &no_dtos(), true);
    let wire: serde_json::Value =
        serde_json::from_str(&serde_json::to_string(&AdjacentShape::Circle { radius: 1.0 }).expect("serializes"))
            .expect("serde output is JSON");
    let content_key = wire
        .as_object()
        .expect("adjacent tagging writes an object")
        .keys()
        .find(|key| key.as_str() != "kind")
        .expect("serde writes a content key for a data variant");

    assert!(
        stub.contains("class AdjacentShapeCirclePayload(TypedDict):\n    radius: float"),
        "a struct variant's payload is an object in its own right and needs its own TypedDict: {stub}"
    );
    assert!(
        stub.contains(&format!(
            "class AdjacentShapeCircleVariant(TypedDict):\n    kind: Literal[\"Circle\"]\n    {content_key}: AdjacentShapeCirclePayload"
        )),
        "the variant TypedDict must nest the payload under serde's content key {content_key:?}: {stub}"
    );
    assert!(
        !stub.contains("class AdjacentShapeCircleVariant(TypedDict):\n    kind: Literal[\"Circle\"]\n    radius:"),
        "declaring the payload's fields flat beside the tag is serde's *internal* form: {stub}"
    );
    assert!(
        stub.contains("class AdjacentShapeEmptyVariant(TypedDict):\n    kind: Literal[\"Empty\"]\n"),
        "a unit variant gets no content key at all, because serde writes none: {stub}"
    );
}