alef 0.79.0

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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
use crate::codegen::shared::binding_fields;
use crate::core::ir::{TypeDef, TypeRef};
use ahash::{AHashMap, AHashSet};
use heck::ToSnakeCase;

use super::optional_kwargs::emit_optional_kwarg_helper;

type OptionsFieldBridges<'a> = AHashMap<&'a str, (&'a str, &'a str, Option<&'a str>)>;

/// True when the options dataclass may hand this field a `None` that the *native* constructor is
/// expected to replace with the Rust default, so the Python converter must pass it through
/// untouched instead of eagerly coercing it.
///
/// Two serde spellings reach that state and only one used to be recognised here. Bare
/// `#[serde(default)]` is stored as the `"/* serde(default) */"` marker in `FieldDef::default`,
/// while `#[serde(default = "path")]` is stored as `serde(default = "path")` there and as
/// `DefaultValue::FunctionCall`/`PublicFunctionCall` in `typed_default`
/// (`extract::extractor::helpers::fields`). `typed_default_to_python` renders both function-call
/// variants as the Python literal `None`, so an equality test against the marker alone left
/// exactly those fields unguarded: `_coerce_enum(_rust.Mode, None)` raises `ValueError`, and the
/// `Vec<enum>` form raises `TypeError: 'NoneType' object is not iterable` — both before the value
/// ever reaches the `#[pyo3(signature = (field=None, ...))]` constructor that would have applied
/// the real default. `codegen::config_gen::default_value_for_field` already discriminates the two
/// spellings the same way. ~keep
fn defers_to_rust_default(field: &crate::core::ir::FieldDef) -> bool {
    use crate::core::ir::DefaultValue;
    field.default.as_deref() == Some("/* serde(default) */")
        || matches!(
            field.typed_default,
            Some(DefaultValue::FunctionCall(_) | DefaultValue::PublicFunctionCall(_))
        )
}

/// Check if a cfg condition is present in the pyo3 build (i.e., the field should be
/// included in the pyo3-compiled binding). This mirrors the logic in gen_stubs/classes.rs
/// to ensure the converter includes the same fields as the .pyi stub.
fn cfg_present_for_pyo3(cfg: &str) -> bool {
    let normalized: String = cfg.chars().filter(|c| !c.is_whitespace()).collect();
    if normalized == "not(target_arch=\"wasm32\")" {
        return true;
    }
    if normalized.starts_with("feature=") {
        return true;
    }
    if normalized.starts_with("any(") && normalized.ends_with(')') {
        let inner = &normalized[4..normalized.len() - 1];
        return inner
            .split(',')
            .all(|part| part.starts_with("feature=") || part == "not(target_arch=\"wasm32\")");
    }
    false
}

#[allow(clippy::too_many_arguments)]
pub(super) fn emit_converters(
    out: &mut String,
    needed_converters: &[String],
    default_types: &AHashMap<String, &TypeDef>,
    options_field_bridges: &OptionsFieldBridges<'_>,
    enum_names: &AHashSet<&str>,
    data_enum_names: &AHashSet<&str>,
    reexported_types: &[String],
    config: &crate::core::config::ResolvedCrateConfig,
    field_defaults: &crate::backends::pyo3::gen_bindings::types::OptionsFieldDefaults<'_>,
    options_return_types: &std::collections::HashSet<String>,
) {
    let reexported_names: AHashSet<&str> = reexported_types.iter().map(|s| s.as_str()).collect();
    out.push_str("_E = TypeVar(\"_E\")\n\n");
    out.push_str(
    "def _pascal_to_snake(value: str) -> str:\n    \"\"\"Convert PascalCase/camelCase to snake_case (AtxClosed -> atx_closed).\"\"\"\n    out_chars: list[str] = []\n    for index, ch in enumerate(value):\n        if ch.isupper() and index > 0 and (value[index - 1].islower() or (index + 1 < len(value) and value[index + 1].islower())):\n            out_chars.append(\"_\")\n        out_chars.append(ch.lower())\n    return \"\".join(out_chars)\n\n\n",
);
    out.push_str(
    "def _coerce_enum(enum_cls: type[_E], value: object) -> _E:\n    \"\"\"Coerce a string/alias value into the matching pyclass enum instance.\"\"\"\n    if isinstance(value, enum_cls):\n        return value\n    if value is None:\n        msg = f\"unknown {getattr(enum_cls, '__name__', enum_cls)!s} value: {value!r}\"\n        raise ValueError(msg)\n    s = str(value).replace(\"-\", \"_\").replace(\" \", \"_\")\n    snake = _pascal_to_snake(s)\n    candidates = (\n        s,\n        s.upper(),\n        s.lower(),\n        snake,\n        snake.upper(),\n        \"\".join(part.capitalize() for part in s.split(\"_\")),\n        \"\".join(part.capitalize() for part in snake.split(\"_\")),\n    )\n    for candidate in candidates:\n        attr = getattr(enum_cls, candidate, None)\n        if isinstance(attr, enum_cls):\n            return attr\n    msg = f\"unknown {getattr(enum_cls, '__name__', enum_cls)!s} value: {value!r}\"\n    raise ValueError(msg)\n\n\n",
);
    for type_name in needed_converters {
        let typ = default_types[type_name];
        let snake = type_name.to_snake_case();

        let is_typeddict = options_return_types.contains(type_name);
        let is_reexported = reexported_names.contains(type_name.as_str());
        let helpers_insert_pos = out.len();
        let mut optional_kwarg_helpers = String::new();

        // The same per-type `rename_fields` lookup `gen_stubs/classes.rs::gen_type_init_stub`
        // builds before calling `resolve_param_ident` -- keeping both call sites' `config_renames`
        // construction identical is what makes `resolve_param_ident` an actual single source of
        // truth for the `#[new]` keyword name, rather than three independently-derived spellings
        // that happen to agree on the common case. ~keep
        let config_renames: std::collections::HashMap<String, String> = typ
            .fields
            .iter()
            .filter_map(|f| {
                config
                    .resolve_field_name(crate::core::config::Language::Python, type_name, &f.name)
                    .map(|renamed| (f.name.clone(), renamed))
            })
            .collect();
        let config_renames_ref = if config_renames.is_empty() {
            None
        } else {
            Some(&config_renames)
        };

        // `name` is a raw Rust field name, but what is being read here is the *emitted* Python
        // attribute / TypedDict key, and both of those are already escaped (`types.rs` and
        // `types/typeddict.rs` build them with `python_ident`). Reading `value.global` would be a
        // SyntaxError, and `value.get("global")` would miss the `"global_"` key that is actually
        // there. `python_ident` is idempotent, so non-keyword names are untouched. ~keep
        let field_access = |name: &str| -> String {
            let name = crate::core::keywords::python_ident(name);
            if is_typeddict {
                format!("value.get(\"{name}\")")
            } else {
                format!("value.{name}")
            }
        };

        let bridge_visitor_field = options_field_bridges.get(type_name.as_str()).copied();
        let bridge_visitor_type = bridge_visitor_field.and_then(|(_, _, alias)| alias).unwrap_or("object");

        let has_visitor_override = bridge_visitor_field.is_some();
        let overloads_start_pos = out.len();
        out.push_str(&crate::backends::pyo3::template_env::render(
            "converters/overload_none.jinja",
            minijinja::context! {
                snake => &snake,
                type_name => type_name,
                has_visitor_override => has_visitor_override,
                bridge_visitor_type => bridge_visitor_type,
            },
        ));
        out.push_str(&crate::backends::pyo3::template_env::render(
            "converters/overload_some.jinja",
            minijinja::context! {
                snake => &snake,
                type_name => type_name,
                has_visitor_override => has_visitor_override,
                bridge_visitor_type => bridge_visitor_type,
            },
        ));

        if bridge_visitor_field.is_some() {
            out.push_str(&crate::backends::pyo3::template_env::render(
                "converters/signature_with_visitor.jinja",
                minijinja::context! {
                    snake => &snake,
                    type_name => type_name,
                    bridge_visitor_type => bridge_visitor_type,
                },
            ));
        } else {
            let sig_len = 47 + snake.len() + 2 * type_name.len();
            if sig_len > 100 {
                out.push_str(&crate::backends::pyo3::template_env::render(
                    "converters/signature_multiline.jinja",
                    minijinja::context! {
                        snake => &snake,
                        type_name => type_name,
                    },
                ));
            } else {
                out.push_str(&crate::backends::pyo3::template_env::render(
                    "converters/signature_singleline.jinja",
                    minijinja::context! {
                        snake => &snake,
                        type_name => type_name,
                    },
                ));
            }
        }
        out.push_str(&crate::backends::pyo3::template_env::render(
            "converters/docstring.jinja",
            minijinja::context! {
                type_name => type_name,
            },
        ));
        out.push_str("    if isinstance(value, str):\n        value = json.loads(value)\n");

        fn get_inner_name(ty: &TypeRef) -> Option<&str> {
            match ty {
                TypeRef::Named(n) => Some(n.as_str()),
                TypeRef::Optional(inner) => {
                    if let TypeRef::Named(n) = inner.as_ref() {
                        Some(n.as_str())
                    } else {
                        None
                    }
                }
                _ => None,
            }
        }

        let struct_coercible: Vec<_> = typ
            .fields
            .iter()
            .filter(|f| get_inner_name(&f.ty).is_some_and(|n| default_types.contains_key(n)))
            .collect();
        let simple_enum_coercible: Vec<_> = typ
            .fields
            .iter()
            .filter(|f| get_inner_name(&f.ty).is_some_and(|n| enum_names.contains(n) && !data_enum_names.contains(n)))
            .collect();
        let data_enum_coercible: Vec<_> = typ
            .fields
            .iter()
            .filter(|f| get_inner_name(&f.ty).is_some_and(|n| data_enum_names.contains(n)))
            .collect();
        let total_coercible = struct_coercible.len() + simple_enum_coercible.len() + data_enum_coercible.len();

        const DICT_HELPER_THRESHOLD: usize = 5;
        let use_dict_helper = total_coercible > DICT_HELPER_THRESHOLD;

        if use_dict_helper {
            let insert_pos = overloads_start_pos;

            let mut helper = String::new();
            helper.push_str(&crate::backends::pyo3::template_env::render(
                "converters/dict_coercer_header.jinja",
                minijinja::context! {
                    snake => &snake,
                    type_name => type_name,
                },
            ));
            helper.push_str(&crate::backends::pyo3::template_env::render(
                "converters/dict_coercer_docstring.jinja",
                minijinja::context! {
                    type_name => type_name,
                },
            ));

            if !struct_coercible.is_empty() {
                helper.push_str("    _struct_coercions = {\n");
                for field in &struct_coercible {
                    let nested_name = get_inner_name(&field.ty).unwrap();
                    let nested_snake = nested_name.to_snake_case();
                    helper.push_str(&crate::backends::pyo3::template_env::render(
                        "converters/struct_coercion_entry.jinja",
                        minijinja::context! {
                            field_name => &field.name,
                            nested_snake => &nested_snake,
                        },
                    ));
                }
                helper.push_str("    }\n");
                helper.push_str("    for _k, _fn in _struct_coercions.items():\n");
                helper.push_str(
                    "        if _k in value and value[_k] is not None:\n            value[_k] = _fn(value[_k])\n",
                );
            }

            if !simple_enum_coercible.is_empty() {
                helper.push_str("    _enum_coercions = {\n");
                for field in &simple_enum_coercible {
                    let enum_name = get_inner_name(&field.ty).unwrap();
                    helper.push_str(&crate::backends::pyo3::template_env::render(
                        "converters/enum_coercion_entry.jinja",
                        minijinja::context! {
                            field_name => &field.name,
                            enum_name => enum_name,
                        },
                    ));
                }
                helper.push_str("    }\n");
                helper.push_str("    for _k, _cls in _enum_coercions.items():\n");
                helper.push_str(
                "        if _k in value and value[_k] is not None:\n            value[_k] = _coerce_enum(_cls, value[_k])\n",
            );
            }

            if !data_enum_coercible.is_empty() {
                helper.push_str("    _data_enum_coercions = {\n");
                for field in &data_enum_coercible {
                    let enum_name = get_inner_name(&field.ty).unwrap();
                    helper.push_str(&crate::backends::pyo3::template_env::render(
                        "converters/enum_coercion_entry.jinja",
                        minijinja::context! {
                            field_name => &field.name,
                            enum_name => enum_name,
                        },
                    ));
                }
                helper.push_str("    }\n");
                helper.push_str("    for _k, _data_cls in _data_enum_coercions.items():\n");
                helper.push_str(
                "        if _k in value and value[_k] is not None and not isinstance(value[_k], _data_cls):\n            value[_k] = _data_cls(value[_k])\n",
            );
            }

            helper.push_str(&crate::backends::pyo3::template_env::render(
                "converters/return_coerced_type.jinja",
                minijinja::context! {
                    type_name => type_name,
                    is_typeddict => is_typeddict,
                },
            ));
            out.insert_str(insert_pos, &helper);
        }

        out.push_str("    if isinstance(value, dict):\n");

        // When a field has #[serde(rename = "...")], map the serde name back.
        let serde_renamed_fields: Vec<_> = typ
            .fields
            .iter()
            .filter_map(|f| f.serde_rename.as_ref().map(|sr| (f.name.as_str(), sr.as_str())))
            .collect();
        if !serde_renamed_fields.is_empty() {
            out.push_str("        # Alias serde-renamed keys back to Rust field names\n");
            for (field_name, serde_name) in &serde_renamed_fields {
                out.push_str(&crate::backends::pyo3::template_env::render(
                    "converters/serde_alias.jinja",
                    minijinja::context! {
                        field_name => field_name,
                        serde_name => serde_name,
                    },
                ));
            }
        }

        if use_dict_helper {
            out.push_str(&crate::backends::pyo3::template_env::render(
                "converters/call_dict_helper.jinja",
                minijinja::context! {
                    snake => &snake,
                    is_typeddict => is_typeddict,
                },
            ));
        } else {
            let has_enum_field = !simple_enum_coercible.is_empty();
            if has_enum_field {
                for field in &simple_enum_coercible {
                    let enum_name = get_inner_name(&field.ty).unwrap();
                    out.push_str(&crate::backends::pyo3::template_env::render(
                        "converters/inline_enum_coerce.jinja",
                        minijinja::context! {
                            field_name => &field.name,
                            enum_name => enum_name,
                        },
                    ));
                }
            }
            if !struct_coercible.is_empty() {
                for field in &struct_coercible {
                    let nested_name = get_inner_name(&field.ty).unwrap();
                    let nested_snake = nested_name.to_snake_case();
                    out.push_str(&crate::backends::pyo3::template_env::render(
                        "converters/inline_struct_coerce.jinja",
                        minijinja::context! {
                            field_name => &field.name,
                            nested_snake => &nested_snake,
                        },
                    ));
                }
            }
            // the PyO3 #[pyclass] reconstruction at `{type_name}(**value)` requires the field
            if !data_enum_coercible.is_empty() {
                for field in &data_enum_coercible {
                    let enum_name = get_inner_name(&field.ty).unwrap();
                    out.push_str(&crate::backends::pyo3::template_env::render(
                        "converters/inline_data_enum_coerce.jinja",
                        minijinja::context! {
                            field_name => &field.name,
                            enum_name => enum_name,
                        },
                    ));
                }
            }
            out.push_str(&crate::backends::pyo3::template_env::render(
                "converters/construct_type.jinja",
                minijinja::context! {
                    type_name => type_name,
                },
            ));
        }
        out.push_str("    if value is None:\n");
        if let Some((kwarg_name, _field_name, _)) = bridge_visitor_field {
            out.push_str(&crate::backends::pyo3::template_env::render(
                "visitor_override_none_case.jinja",
                minijinja::context! {
                    type_name => type_name,
                    kwarg_name => kwarg_name,
                },
            ));
        } else {
            out.push_str("        return None\n");
        }
        if is_typeddict && bridge_visitor_field.is_none() {
            out.push_str("    value = cast(dict[str, Any], value)\n");
            out.push_str(&crate::backends::pyo3::template_env::render(
                "converters/typeddict_splat_return.jinja",
                minijinja::context! {
                    type_name => type_name,
                },
            ));
            out.push_str("\n\n");
            continue;
        }
        out.push_str(&crate::backends::pyo3::template_env::render(
            "converters/cast_value.jinja",
            minijinja::context! {
                type_name => type_name,
            },
        ));
        if is_reexported {
            out.push_str("    return value\n\n\n");
            continue;
        }
        out.push_str(&crate::backends::pyo3::template_env::render(
            "converters/return_constructed.jinja",
            minijinja::context! {
                type_name => type_name,
            },
        ));

        // Include fields that are in the pyo3 `#[new]` constructor: all non-binding-excluded fields
        // #[serde(skip)] does NOT affect this — it only affects serialization, not construction.
        for field in binding_fields(&typ.fields).filter(|f| f.cfg.as_deref().is_none_or(cfg_present_for_pyo3)) {
            let inner_named = match &field.ty {
                TypeRef::Named(n) => Some(n.as_str()),
                TypeRef::Optional(inner) => {
                    if let TypeRef::Named(n) = inner.as_ref() {
                        Some(n.as_str())
                    } else {
                        None
                    }
                }
                _ => None,
            };

            if let Some(nested_name) = inner_named {
                if default_types.contains_key(nested_name) {
                    let nested_snake = nested_name.to_snake_case();
                    let accessor = field_access(&field.name);
                    out.push_str(&crate::backends::pyo3::template_env::render(
                        "converters/field_accessor.jinja",
                        minijinja::context! {
                            field_name => &field.name,
                            accessor => format!("_to_rust_{nested_snake}({accessor})"),
                        },
                    ));
                    continue;
                }
                if enum_names.contains(&nested_name) {
                    if data_enum_names.contains(&nested_name) {
                        let accessor = field_access(&field.name);
                        let needs_none_guard =
                            matches!(&field.ty, TypeRef::Optional(_)) || field.optional || is_typeddict;
                        if needs_none_guard {
                            out.push_str(&crate::backends::pyo3::template_env::render(
                                "data_enum_dict_coerce_guard.jinja",
                                minijinja::context! {
                                    name => &field.name,
                                    accessor => &accessor,
                                    enum_name => nested_name,
                                },
                            ));
                        } else {
                            // For non-optional data enums with #[serde(default)], the user-facing
                            // #[pyo3(signature = ...)] default apply.
                            if defers_to_rust_default(field) {
                                out.push_str(&crate::backends::pyo3::template_env::render(
                                    "data_enum_dict_coerce_optional_default.jinja",
                                    minijinja::context! {
                                        name => &field.name,
                                        accessor => &accessor,
                                        enum_name => nested_name,
                                    },
                                ));
                            } else {
                                out.push_str(&crate::backends::pyo3::template_env::render(
                                    "data_enum_dict_coerce_no_guard.jinja",
                                    minijinja::context! {
                                        name => &field.name,
                                        accessor => &accessor,
                                        enum_name => nested_name,
                                    },
                                ));
                            }
                        }
                    } else {
                        let accessor = field_access(&field.name);

                        // Mirrors the data-enum branch above: a field that is already `Option<T>`
                        // in the native constructor (`is_optional`) has a real `None` default at
                        // that layer (see `constructors::replace_constructor_with_serde_rename`'s
                        // `defaults` -- `f.optional` alone forces `{param}=None`), so passing
                        // `None` straight through is exactly equivalent to omitting the kwarg and
                        // needs no `**{...}` unpack at all. Only a field that is NOT optional at
                        // that layer but still carries `#[serde(default)]` needs the omission
                        // trick, because its real default there is a non-`None` Rust expression
                        // (`Self::default().{field}`) that Python cannot represent inline; passing
                        // `None` to a non-`Option` parameter would fail extraction. Collapsing
                        // both cases into one `needs_none_guard` used to route the `is_optional`
                        // case through the omission template too, which put an unnecessary
                        // `**({...} if ... else {})` in the call -- and pyrefly's keyword-argument
                        // inference cross-assigns argument types when two such unpacks appear in
                        // the same call (alef bug: two `Option<Enum>` fields on one constructor
                        // reproduced `[bad-argument-type]` with the errors swapped between the two
                        // parameters). ~keep
                        //
                        // `#[serde(default)]` alone does not make the omission trick sound either:
                        // `options.py` renders such an enum field as `= "start"` (the enum's
                        // `#[default]` variant), never `None`, so `if value.x is not None` is
                        // statically always true and the unpack buys nothing while still costing
                        // one `[bad-argument-type]` per other unpack in the same call. Only a field
                        // `options.py` actually defaults to `None` can be absent. ~keep
                        let is_optional = matches!(field.ty, TypeRef::Optional(_)) || field.optional;

                        if is_optional || is_typeddict {
                            out.push_str(&crate::backends::pyo3::template_env::render(
                                "simple_enum_dict_coerce_guard.jinja",
                                minijinja::context! {
                                    name => &field.name,
                                    enum_name => nested_name,
                                    accessor => &accessor,
                                },
                            ));
                        } else if defers_to_rust_default(field) && field_defaults.admits_none(field) {
                            let parameter_name = crate::backends::pyo3::gen_bindings::constructors::resolve_param_ident(
                                &field.name,
                                field.serde_rename.as_ref(),
                                config_renames_ref,
                            );
                            let parameter_name = parameter_name.strip_prefix("r#").unwrap_or(&parameter_name);
                            let helper_name = emit_optional_kwarg_helper(
                                &mut optional_kwarg_helpers,
                                type_name,
                                &snake,
                                field,
                                parameter_name,
                            );
                            out.push_str(&crate::backends::pyo3::template_env::render(
                                "simple_enum_dict_coerce_optional_default.jinja",
                                minijinja::context! {
                                    name => &field.name,
                                    enum_name => nested_name,
                                    accessor => &accessor,
                                    helper_name => helper_name,
                                },
                            ));
                        } else {
                            out.push_str(&crate::backends::pyo3::template_env::render(
                                "simple_enum_dict_coerce.jinja",
                                minijinja::context! {
                                    name => &field.name,
                                    enum_name => nested_name,
                                    accessor => &accessor,
                                },
                            ));
                        }
                    }
                    continue;
                }
            }

            let vec_field = match &field.ty {
                TypeRef::Vec(inner) => Some((inner, matches!(&field.ty, TypeRef::Optional(_)) || field.optional)),
                TypeRef::Optional(opt_inner) => match opt_inner.as_ref() {
                    TypeRef::Vec(inner) => Some((inner, true)),
                    _ => None,
                },
                _ => None,
            };
            if let Some((inner, is_optional)) = vec_field
                && let TypeRef::Named(enum_name) = inner.as_ref()
                && enum_names.contains(&enum_name.as_str())
            {
                let accessor = field_access(&field.name);
                if data_enum_names.contains(&enum_name.as_str()) {
                    out.push_str(&crate::backends::pyo3::template_env::render(
                        "data_enum_vec_coerce.jinja",
                        minijinja::context! {
                            name => &field.name,
                            enum_name => enum_name.as_str(),
                            accessor => &accessor,
                            optional => is_optional,
                        },
                    ));
                } else {
                    out.push_str(&crate::backends::pyo3::template_env::render(
                        "simple_enum_vec_coerce.jinja",
                        minijinja::context! {
                            name => &field.name,
                            enum_name => enum_name.as_str(),
                            accessor => &accessor,
                            optional => is_optional,
                        },
                    ));
                }
                continue;
            }
            if let Some((inner, is_optional)) = vec_field
                && let TypeRef::Named(struct_name) = inner.as_ref()
                && default_types.contains_key(struct_name.as_str())
            {
                let accessor = field_access(&field.name);
                let struct_snake = struct_name.to_snake_case();
                out.push_str(&crate::backends::pyo3::template_env::render(
                    "struct_vec_coerce.jinja",
                    minijinja::context! {
                        name => &field.name,
                        struct_snake => &struct_snake,
                        accessor => &accessor,
                        optional => is_optional,
                    },
                ));
                continue;
            }

            if let Some((kwarg_name, field_name, _)) = bridge_visitor_field
                && field.name == field_name
            {
                out.push_str(&crate::backends::pyo3::template_env::render(
                    "visitor_override_param.jinja",
                    minijinja::context! {
                        field_name => field_name,
                        accessor => field_access(field_name),
                    },
                ));
                let _ = kwarg_name;
                continue;
            }
            let accessor = field_access(&field.name);

            let final_accessor = if let Some(inner_named) = match &field.ty {
                TypeRef::Named(n) => Some(n.as_str()),
                TypeRef::Optional(inner) => {
                    if let TypeRef::Named(n) = inner.as_ref() {
                        Some(n.as_str())
                    } else {
                        None
                    }
                }
                _ => None,
            } {
                if (matches!(&field.ty, TypeRef::Optional(_)) || field.optional)
                    && data_enum_names.contains(inner_named)
                {
                    format!(
                        "None if {accessor} is None else ({accessor} if isinstance({accessor}, _rust.{inner_named}) else _rust.{inner_named}({accessor}))",
                        accessor = accessor,
                        inner_named = inner_named
                    )
                } else {
                    accessor.clone()
                }
            } else {
                accessor.clone()
            };

            let is_json_field = matches!(field.ty, TypeRef::Json)
                || matches!(&field.ty, TypeRef::Optional(inner) if matches!(inner.as_ref(), TypeRef::Json));
            let final_accessor = if is_json_field {
                format!(
                    "(json.dumps({final_accessor}) if isinstance({final_accessor}, (dict, list)) else {final_accessor})"
                )
            } else {
                final_accessor
            };

            // This is the keyword-argument name in the emitted `_rust.{Type}(...)` call, so it must
            // be exactly what the generated `#[new]` accepts. Calling `resolve_param_ident`
            // directly -- the same function `gen_stubs/classes.rs` calls for the `.pyi` `__init__`
            // stub, and the same function `constructors.rs` calls to build the real `#[new]`
            // signature -- makes this an ASK of the single source of truth instead of a
            // re-derivation that can silently drift from it. The re-derivation that used to live
            // here (`python_ident` applied straight to the wire name) agreed with
            // `resolve_param_ident` for a Python keyword that is not ALSO a Rust keyword (`global`
            // -> `global_` either way), but diverged for a name that is a keyword in BOTH
            // languages (`type`, a `#[serde(rename = "type")]` wire name): `resolve_param_ident`
            // takes the Rust `r#type` escape (which PyO3 exposes to Python as bare `type`, not
            // `type_`), so the two spellings disagreed exactly there -- `_rust.T(type_=...)` here
            // vs `_rust.T(type=...)` (and the `.pyi` stub) is what pyrefly's
            // `[unexpected-keyword]` was catching. `resolve_param_ident` can return an `r#`-escaped
            // Rust identifier; PyO3 strips that prefix from the Python-visible name, so it must be
            // stripped here too before use as a Python keyword argument (mirrors
            // `gen_stubs/classes.rs`'s stub emission). ~keep
            let pyo3_param_name = crate::backends::pyo3::gen_bindings::constructors::resolve_param_ident(
                &field.name,
                field.serde_rename.as_ref(),
                config_renames_ref,
            );
            let pyo3_param_name = pyo3_param_name
                .strip_prefix("r#")
                .map(str::to_owned)
                .unwrap_or(pyo3_param_name);

            let is_optional = matches!(field.ty, TypeRef::Optional(_)) || field.optional;

            // Which `TypeRef` shape the field has is irrelevant to whether the omission is
            // needed: the only facts that matter are that `options.py` defaults the field to
            // `None` (`admits_none`) and that the native parameter is not an `Option`
            // (`!is_optional`), so the `None` has to be withheld rather than passed. Gating this
            // on `TypeRef::Named` additionally left every other shape passing that `None`
            // straight through to a non-`Option` pyo3 parameter -- a `Vec<String>` field whose
            // Rust default is a function call reached `_rust.T(field=None)` and failed extraction
            // with `TypeError: 'None' is not an instance of 'Sequence'`, far from the dataclass
            // that produced it. `admits_none` is the single fact both this branch and the
            // `T | None` widening in `types.rs` are derived from; the gate made this branch
            // disagree with that widening for exactly the non-`Named` shapes. ~keep
            if defers_to_rust_default(field) && !is_optional && field_defaults.admits_none(field) {
                let raw_field_accessor = field_access(&field.name);
                let helper_name =
                    emit_optional_kwarg_helper(&mut optional_kwarg_helpers, type_name, &snake, field, &pyo3_param_name);
                out.push_str(&crate::backends::pyo3::template_env::render(
                    "field_kwarg_optional_default.jinja",
                    minijinja::context! {
                        name => &pyo3_param_name,
                        raw_accessor => &raw_field_accessor,
                        final_accessor => &final_accessor,
                        helper_name => helper_name,
                    },
                ));
            } else {
                out.push_str(&crate::backends::pyo3::template_env::render(
                    "field_kwarg.jinja",
                    minijinja::context! {
                        name => &pyo3_param_name,
                        accessor => &final_accessor,
                    },
                ));
            }
        }

        out.push_str("    )\n\n\n");
        out.insert_str(helpers_insert_pos, &optional_kwarg_helpers);
    }
}

#[cfg(test)]
mod tests {
    use super::cfg_present_for_pyo3;

    #[test]
    fn cfg_present_for_pyo3_accepts_feature_gates() {
        assert!(cfg_present_for_pyo3("feature = \"my-feature\""));
        assert!(cfg_present_for_pyo3("feature=\"my-feature\""));
    }

    #[test]
    fn cfg_present_for_pyo3_accepts_non_wasm_gate() {
        assert!(cfg_present_for_pyo3("not(target_arch = \"wasm32\")"));
    }

    #[test]
    fn cfg_present_for_pyo3_accepts_any_of_feature_gates() {
        // The `crawl` field: #[cfg(any(feature = "url-ingestion", feature = "url-config-types"))].
        assert!(cfg_present_for_pyo3(
            "any(feature = \"url-ingestion\", feature = \"url-config-types\")"
        ));
    }

    #[test]
    fn cfg_present_for_pyo3_rejects_unsupported_gates() {
        assert!(!cfg_present_for_pyo3("target_os = \"windows\""));
        assert!(!cfg_present_for_pyo3("target_arch = \"x86_64\""));
        assert!(!cfg_present_for_pyo3("any(feature = \"x\", target_os = \"windows\")"));
    }
}