Skip to content

Speed up inspect.signature() for Python functions #150816

@gaborbernat

Description

@gaborbernat

inspect.signature() builds a Signature whose every Parameter is constructed through the public Parameter constructor, which validates the name and kind on each call. When the parameters come from a function's own code object the names are already valid identifiers and the kinds are already the canonical constants, so that validation re-checks facts that are guaranteed to hold.

Signature introspection runs constantly in the ecosystem: web frameworks resolving view and dependency arguments, CLI libraries like click, pytest fixture wiring, and serialization and validation layers all call it per function, often at import time while wiring an application together.

Taking the signature of 400 real callables collected from popular packages (requests, click, jinja2, sqlalchemy, werkzeug, flask and others) takes 1.46 ms today and 1.16 ms when trusted-source parameters skip the redundant validation, 26% faster, with the public Parameter() constructor and its validation unchanged for every other caller.

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement
    No fields configured for issues without a type.

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions