alef 0.22.23

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
use crate::core::backend::GeneratedFile;
use crate::core::config::{AdapterPattern, Language, ResolvedCrateConfig};
use crate::core::ir::ApiSurface;
use crate::core::template_versions as tv;
use crate::scaffold::naming::python_pip_name;
use crate::{
    scaffold::cargo_package_header, scaffold::core_dep_features, scaffold::detect_workspace_inheritance,
    scaffold::render_extra_deps, scaffold::scaffold_meta, scaffold::to_pep440,
};
use std::path::PathBuf;

/// pyproject-fmt's default `column_width` is 80 chars. Arrays whose inline
/// rendering (`prefix_len + "[ a, b ]".len()`) fits within this width are
/// emitted inline-with-inner-spaces (`[ "a", "b" ]`); otherwise they are
/// expanded to one-element-per-line with a trailing comma. Matching this rule
/// at emission time keeps prek's `pyproject-fmt` hook a no-op on every regen.
const PYPROJECT_FMT_COLUMN_WIDTH: usize = 80;

/// Format a list of pre-quoted TOML entries to match `pyproject-fmt`'s canonical
/// output. `prefix_len` is the on-screen column where the array opens (e.g.
/// `"keywords = ".len() == 11`); it is needed because pyproject-fmt picks
/// inline vs multi-line based on the total line length including the prefix.
///
/// - Empty: `[]`
/// - Inline form (`[ a, b, c ]`, inner spaces) when total length ≤
///   [`PYPROJECT_FMT_COLUMN_WIDTH`].
/// - Multi-line otherwise: 2-space indent, trailing comma after every element.
fn format_toml_array_with_prefix(entries: &[String], prefix_len: usize) -> String {
    if entries.is_empty() {
        return "[]".to_string();
    }
    let inline = format!("[ {} ]", entries.join(", "));
    if prefix_len + inline.len() <= PYPROJECT_FMT_COLUMN_WIDTH {
        return inline;
    }
    let inner = entries.iter().map(|e| format!("  {e},")).collect::<Vec<_>>().join("\n");
    format!("[\n{inner}\n]")
}

/// Canonicalize a PEP 440 version specifier to `pyproject-fmt`'s normalized form.
///
/// `pyproject-fmt` strips redundant trailing `.0` release segments from each
/// version number in a specifier (e.g. `>=1.0,<2.0` → `>=1,<2`, `>=1.19.0`
/// → `>=1.19`). The renovate-tracked constants in [`crate::core::template_versions`]
/// keep their human-readable form; this normalizes them at emission time so the
/// generated `pyproject.toml` stays a no-op under the `pyproject-fmt` hook.
///
/// A trailing `.0` is only stripped when it is not the sole release segment — a
/// bare `0` (e.g. `==0`) is left untouched.
fn canonicalize_pep440_specifier(specifier: &str) -> String {
    // Split into comma-separated clauses, normalize each, rejoin without spaces
    // (pyproject-fmt emits `>=1,<2`, not `>=1, <2`).
    specifier
        .split(',')
        .map(|clause| {
            let clause = clause.trim();
            // Separate the comparison operator prefix from the version number.
            let op_len = clause
                .char_indices()
                .find(|(_, c)| c.is_ascii_digit())
                .map(|(idx, _)| idx)
                .unwrap_or(clause.len());
            let (op, version) = clause.split_at(op_len);
            format!("{op}{}", canonicalize_pep440_version(version))
        })
        .collect::<Vec<_>>()
        .join(",")
}

/// Strip redundant trailing `.0` release segments from a single PEP 440 version
/// number while preserving at least one release segment (`2.0` → `2`, `1.19.0`
/// → `1.19`, `0` → `0`). Pre/post/dev suffixes and local versions are left as-is.
fn canonicalize_pep440_version(version: &str) -> String {
    // Only touch the leading release segment (digits and dots before any
    // pre/post/dev/local marker); leave the remainder untouched.
    let release_len = version
        .char_indices()
        .find(|(_, c)| !(c.is_ascii_digit() || *c == '.'))
        .map(|(idx, _)| idx)
        .unwrap_or(version.len());
    let (release, suffix) = version.split_at(release_len);
    let mut segments: Vec<&str> = release.split('.').collect();
    while segments.len() > 1 && segments.last() == Some(&"0") {
        segments.pop();
    }
    format!("{}{}", segments.join("."), suffix)
}

pub(crate) fn scaffold_python_cargo(
    api: &ApiSurface,
    config: &ResolvedCrateConfig,
) -> anyhow::Result<Vec<GeneratedFile>> {
    let meta = scaffold_meta(config);
    let version = &api.version;
    let module_name = config.python_module_name();
    let core_crate_dir = config.core_crate_dir();
    let ws = detect_workspace_inheritance(config.workspace_root.as_deref());
    let pkg_header = cargo_package_header(&format!("{core_crate_dir}-py"), version, "2024", &meta, &ws);

    let extra_deps = render_extra_deps(config, Language::Python);

    let has_trait_bridges = !config.trait_bridges.is_empty();
    let has_streaming = config
        .adapters
        .iter()
        .any(|a| matches!(a.pattern, AdapterPattern::Streaming));
    let mut all_deps = extra_deps;
    if has_trait_bridges && !all_deps.contains("async-trait") {
        if !all_deps.is_empty() {
            all_deps.push('\n');
        }
        all_deps.push_str("async-trait = \"0.1\"");
    }
    if (has_trait_bridges || has_streaming) && !all_deps.contains("tokio = ") {
        if !all_deps.is_empty() {
            all_deps.push('\n');
        }
        let features = if has_streaming {
            "[\"rt-multi-thread\", \"sync\"]"
        } else {
            "[\"rt-multi-thread\"]"
        };
        all_deps.push_str(&format!("tokio = {{ version = \"1\", features = {features} }}"));
    }
    if has_streaming && !all_deps.contains("futures = ") && !all_deps.contains("futures =\"") {
        if !all_deps.is_empty() {
            all_deps.push('\n');
        }
        all_deps.push_str("futures = \"0.3\"");
    }

    let extra_deps_section = if all_deps.is_empty() {
        String::new()
    } else {
        format!("\n{all_deps}")
    };
    // Build the cargo-machete ignored list. `pyo3-async-runtimes` and
    // `serde_json` are emitted unconditionally above so they are always
    // ignored. Conditional deps (`async-trait` / `tokio` for trait bridges
    // and streaming, `futures` for streaming) are appended only when the
    // scaffold actually adds them to `[dependencies]`, so cargo-machete
    // doesn't flap on umbrellas whose API surface doesn't exercise the
    // trait-bridge / streaming codepath.
    let mut machete_ignored: Vec<&str> = vec!["pyo3-async-runtimes", "serde_json"];
    if has_trait_bridges {
        machete_ignored.push("async-trait");
    }
    if has_trait_bridges || has_streaming {
        machete_ignored.push("tokio");
    }
    if has_streaming {
        machete_ignored.push("futures");
    }
    let machete_ignored_str = machete_ignored
        .iter()
        .map(|d| format!("\"{d}\""))
        .collect::<Vec<_>>()
        .join(", ");
    // Build [dependencies] block alphabetically sorted to match cargo-sort.
    let core_dep_py = crate::scaffold::render_core_dep(
        &config.name,
        &format!("../{core_crate_dir}"),
        &core_dep_features(config, Language::Python),
        version,
    );
    let mut dep_entries: Vec<String> = vec![
        format!("pyo3 = {{ version = \"{}\" }}", tv::cargo::PYO3),
        format!(
            "pyo3-async-runtimes = {{ version = \"{}\", features = [\"tokio-runtime\"] }}",
            tv::cargo::PYO3_ASYNC_RUNTIMES
        ),
        "serde = { version = \"1\", features = [\"derive\"] }".to_string(),
        "serde_json = \"1\"".to_string(),
    ];
    if !core_dep_py.is_empty() {
        dep_entries.push(core_dep_py.clone());
    }
    if !all_deps.is_empty() {
        for line in all_deps.lines() {
            if !line.is_empty() {
                dep_entries.push(line.to_string());
            }
        }
    }
    dep_entries.sort();
    let dep_block = dep_entries.join("\n");
    let _ = extra_deps_section;

    let content = format!(
        r#"{pkg_header}

# `pyo3-async-runtimes` and `serde_json` are emitted unconditionally above so
# the manifest is stable across regens, but for umbrella crates with no
# async fns or no JSON-marshalled return types they are genuinely unused.
# The conditional `async-trait` / `tokio` / `futures` deps are similarly
# flagged when the umbrella has trait-bridge / streaming adapters configured
# but no actual async-trait / async callsite in the generated PyO3 shim.
[package.metadata.cargo-machete]
ignored = [{machete_ignored_str}]

[lib]
name = "{module_name}"
crate-type = ["cdylib"]

[features]
extension-module = ["pyo3/extension-module", "pyo3/abi3-py310"]

[dependencies]
{dep_block}

"#,
        pkg_header = pkg_header,
        module_name = module_name,
        dep_block = dep_block,
        machete_ignored_str = machete_ignored_str,
    );

    Ok(vec![GeneratedFile {
        path: PathBuf::from(format!("crates/{}-py/Cargo.toml", core_crate_dir)),
        content,
        generated_header: true,
    }])
}

pub(crate) fn scaffold_python(api: &ApiSurface, config: &ResolvedCrateConfig) -> anyhow::Result<Vec<GeneratedFile>> {
    let meta = scaffold_meta(config);
    let pip_name = python_pip_name(config);
    let version = to_pep440(&api.version);
    let module_name = config.python_module_name();
    let core_crate_dir = config.core_crate_dir();
    let python_package = pip_name.replace('-', "_");
    let pkg_dir = config.package_dir(Language::Python);

    let authors_toml = if meta.authors.is_empty() {
        String::new()
    } else {
        let entries: Vec<String> = meta.authors.iter().map(|a| format!("{{ name = \"{}\" }}", a)).collect();
        format!(
            "authors = {}\n",
            format_toml_array_with_prefix(&entries, "authors = ".len())
        )
    };

    let keywords_toml = if meta.keywords.is_empty() {
        String::new()
    } else {
        let mut sorted_keywords = meta.keywords.clone();
        sorted_keywords.sort();
        let entries: Vec<String> = sorted_keywords.iter().map(|k| format!("\"{}\"", k)).collect();
        format!(
            "keywords = {}\n",
            format_toml_array_with_prefix(&entries, "keywords = ".len())
        )
    };

    let homepage_toml = if meta.homepage.is_empty() {
        String::new()
    } else {
        format!("homepage = \"{}\"\n", meta.homepage)
    };

    let dependencies_toml = match config.python.as_ref().map(|p| &p.pip_dependencies) {
        Some(deps) if !deps.is_empty() => {
            let entries: Vec<String> = deps.iter().map(|d| format!("\"{}\"", d)).collect();
            // Force multi-line to match pyproject-fmt's output (dependencies always wraps)
            let inner = entries.iter().map(|e| format!("  {e},")).collect::<Vec<_>>().join("\n");
            format!("dependencies = [\n{inner}\n]\n")
        }
        _ => String::new(),
    };

    let urls_line = meta
        .configured_repository
        .as_deref()
        .map(|repository| format!("urls.repository = \"{repository}\"\n"))
        .unwrap_or_default();

    let dev_group_entries = [
        format!("\"mypy{}\"", canonicalize_pep440_specifier(tv::pypi::MYPY)),
        format!("\"ruff{}\"", canonicalize_pep440_specifier(tv::pypi::RUFF)),
    ];
    let dev_group_array = format_toml_array_with_prefix(&dev_group_entries, "dev = ".len());

    // The `disable_error_code` array lives inside an inline table inside the
    // `overrides = [...]` array, so the on-disk prefix exceeds pyproject-fmt's
    // 80-char column width and the array would always wrap. pyproject-fmt
    // keeps the array inline regardless (matching its handling of nested
    // inline tables), so render it inline with inner spaces directly.
    let mypy_disable_codes = format!(
        "[ {} ]",
        ["\"call-arg\"", "\"arg-type\"", "\"return-value\"", "\"attr-defined\""].join(", ")
    );

    let content = format!(
        r#"[build-system]
build-backend = "maturin"
requires = [ "{maturin_build_requires}" ]

[project]
name = "{pip_name}"
version = "{version}"
description = "{description}"
{keywords}license = "{license}"
license-files = [ "LICENSE" ]
{authors}requires-python = ">=3.10"
classifiers = [
  "Programming Language :: Python :: 3 :: Only",
  "Programming Language :: Python :: 3.10",
  "Programming Language :: Python :: 3.11",
  "Programming Language :: Python :: 3.12",
  "Programming Language :: Python :: 3.13",
  "Programming Language :: Python :: 3.14",
]
{dependencies}{urls_line}{homepage}
[dependency-groups]
dev = {dev_group}

[tool.maturin]
module-name = "{python_package}.{module_name}"
manifest-path = "../../crates/{crate_dir}-py/Cargo.toml"
# abi3-py310 produces a single wheel per platform that loads on Python 3.10+,
# avoiding a per-Python-version build matrix.
features = [ "pyo3/extension-module", "pyo3/abi3-py310" ]
python-packages = [ "{python_package}" ]

[tool.ruff]
target-version = "py310"
line-length = 120
format.docstring-code-line-length = 120
format.docstring-code-format = true
lint.select = [ "ALL" ]
lint.ignore = [
  "ANN401",
  "ASYNC109",
  "ASYNC110",
  "BLE001",
  "COM812",
  "D100",
  "D104",
  "D107",
  "D205",
  "E501",
  "EM",
  "FBT",
  "FIX",
  "ISC001",
  "PD011",
  "PGH003",
  "PLR2004",
  "PLW0603",
  "S104",
  "S110",
  "S603",
  "TD",
  "TRY",
]
lint.per-file-ignores."{python_package}/__init__.py" = [ "I001" ]
# The alef Python codegen still emits cosmetic warnings on the wrapper
# modules: api.py keeps the legacy `from typing import AsyncIterator` and a
# single-line import block, options.py carries # noqa: TC001 / F401 markers
# that turn out unused on every regen, __init__.py star-imports re-sort with
# a different convention. Silence these specific rules on the wrappers until
# the codegen is updated to emit ruff-clean output.
lint.per-file-ignores."{python_package}/api.py" = [ "F401", "I001", "UP035" ]
lint.per-file-ignores."{python_package}/options.py" = [ "F401", "RUF100" ]
lint.per-file-ignores."tests/**" = [ "ANN", "D103", "PLR2004", "S101" ]
lint.mccabe.max-complexity = 15
lint.pydocstyle.convention = "google"
lint.pylint.max-args = 10
lint.pylint.max-branches = 15
lint.pylint.max-returns = 10

[tool.mypy]
python_version = "3.10"
strict = true
show_error_codes = true
implicit_reexport = false
namespace_packages = true
overrides = [
  # The alef-emitted `api.py` wrapper has a structural mismatch between its
  # `options.*` dataclass signatures and the `_internal_bindings.*` pyclass
  # types pyo3 accepts/returns at runtime. pyo3 reconciles them dynamically via
  # FromPyObject — the Python e2e suite exercises the runtime path — but mypy
  # sees only the static-type discrepancy. Disable the four error codes the
  # discrepancy raises until the codegen emits matching `_to_rust_*` calls and
  # casts the return values.
  {{ module = "{python_package}.api", disable_error_code = {mypy_disable_codes} }},
]
"#,
        pip_name = pip_name,
        version = version,
        description = meta.description,
        license = meta.license,
        authors = authors_toml,
        keywords = keywords_toml,
        homepage = homepage_toml,
        dependencies = dependencies_toml,
        urls_line = urls_line,
        python_package = python_package,
        module_name = module_name,
        crate_dir = core_crate_dir,
        maturin_build_requires = canonicalize_pep440_specifier(tv::pypi::MATURIN_BUILD_REQUIRES),
        dev_group = dev_group_array,
        mypy_disable_codes = mypy_disable_codes,
    );

    Ok(vec![
        GeneratedFile {
            path: PathBuf::from(format!("{pkg_dir}/pyproject.toml")),
            content,
            generated_header: true,
        },
        GeneratedFile {
            path: PathBuf::from(format!("{pkg_dir}/{python_package}/py.typed")),
            // Empty (0 bytes): end-of-file-fixer leaves a 0-byte file alone, but strips a
            // file whose sole content is a trailing newline back to empty — emitting "\n"
            // here causes churn on every regen. py.typed is a PEP 561 marker; empty is correct.
            content: String::new(),
            generated_header: false,
        },
    ])
}

#[cfg(test)]
mod tests {
    use super::{canonicalize_pep440_specifier, canonicalize_pep440_version};

    /// `pyproject-fmt` strips redundant trailing `.0` release segments from a
    /// single version number, keeping at least one segment.
    #[test]
    fn canonicalize_version_strips_redundant_trailing_zero() {
        assert_eq!(canonicalize_pep440_version("2.0"), "2");
        assert_eq!(canonicalize_pep440_version("1.19.0"), "1.19");
        assert_eq!(canonicalize_pep440_version("1.0.0"), "1");
        // Non-redundant segments are preserved.
        assert_eq!(canonicalize_pep440_version("1.19"), "1.19");
        assert_eq!(canonicalize_pep440_version("0.14.8"), "0.14.8");
        // A bare zero is the sole segment and must be kept.
        assert_eq!(canonicalize_pep440_version("0"), "0");
        // Suffixes (pre/post/dev/local) are left untouched.
        assert_eq!(canonicalize_pep440_version("1.0rc1"), "1rc1");
    }

    /// Multi-clause specifiers are normalized clause-by-clause and rejoined with
    /// a bare comma (no space), matching `pyproject-fmt`'s output.
    #[test]
    fn canonicalize_specifier_handles_comparison_operators_and_clauses() {
        assert_eq!(canonicalize_pep440_specifier(">=1.0,<2.0"), ">=1,<2");
        assert_eq!(canonicalize_pep440_specifier(">=1.19.0"), ">=1.19");
        assert_eq!(canonicalize_pep440_specifier(">=0.14.8"), ">=0.14.8");
        assert_eq!(canonicalize_pep440_specifier("==1.0"), "==1");
        // Surrounding whitespace inside clauses is normalized away.
        assert_eq!(canonicalize_pep440_specifier(">=1.0, <2.0"), ">=1,<2");
    }
}