Skip to content

experimental-inspect: slot trailing arguments are introspected without their None default #6364

Description

@jonasdedden

Bug Description

CPython's slot wrappers let the caller omit a trailing argument and substitute None. Introspection emits those parameters as required, so the stub disagrees with the runtime and mypy.stubtest reports one error per method.

Affects __pow__, __rpow__ and __get__.

Steps to Reproduce

#[pyclass]
struct Number(u64);

#[pymethods]
impl Number {
    fn __pow__(&self, other: &Self, modulo: Option<&Self>) -> Self {
        Self(self.0.pow(other.0 as u32) % modulo.map_or(u64::MAX, |m| m.0))
    }
}

Build with experimental-inspect, then generate stubs with pyo3-introspection:

def __pow__(self, other: object, modulo: object, /) -> Number: ...   # generated
$ python -c "import slotrepro; print(slotrepro.Number.__pow__.__text_signature__)"
($self, value, mod=None, /)

$ python -m mypy.stubtest slotrepro
error: slotrepro.Number.__pow__ is inconsistent, runtime parameter "mod" has a default value but stub parameter does not
Stub:
def (slotrepro.Number, object, object) -> slotrepro.Number
Runtime:
def (self, value, mod=None, /)

Fix

I already worked on a fix here (#6363) but am unsure whether this implementation is really clean & good. Open for suggestions!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions