Skip to content
Next Next commit
Drop the $module marker from generated __text_signature__
CPython's C functions receive the module as their first argument, so
PyCFunction.__self__ is the module and inspect strips the $module
parameter when building a Signature. A #[pyfunction] takes no such
argument, PyNativeFunction::zelf is None, and inspect has nothing to
strip, so the marker surfaced as a parameter that does not exist:

    inspect.signature(len)
    (module, /, obj)     # was
    (obj)                # now

All 45 builtins shared with CPython carried it. Methods are unaffected;
their $self marker comes from func_sig and both branches now produce the
same string.

Assisted-by: Claude Code:claude-opus-5
  • Loading branch information
leehanjeong committed Aug 11, 2026
commit 6725e25a326be501dde7111dcf7b73b94af894d6
9 changes: 4 additions & 5 deletions crates/derive-impl/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,13 +732,12 @@ where

// Best effort attempt to generate a template from which a
// __text_signature__ can be created.
//
// Unlike CPython, a `#[pyfunction]` doesn't take the module as an argument,
Comment thread
youknowone marked this conversation as resolved.
Outdated
// so there's no module to mark with `$module`.
pub(crate) fn text_signature(sig: &Signature, name: &str) -> String {
let signature = func_sig(sig);
if signature.starts_with("$self") {
format!("{name}({signature})")
} else {
format!("{}({}, {})", name, "$module", signature)
}
format!("{name}({signature})")
}

pub(crate) fn infer_native_call_flags(sig: &Signature, drop_first_typed: usize) -> TokenStream {
Expand Down