python / python/cpython

Speed up inspect.signature() for Python functions

Open
#150,816 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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.

Linked PRs
  • gh-150823
  • gh-157463

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing inspect.signature() through Signature and the public Parameter constructor, focusing on parameters sourced from a function's code object. Compare the existing and trusted-source paths with the 400-callable benchmark described in the issue, and verify that public Parameter() validation remains unchanged while the reported performance improvement is reproduced.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
developer-experience
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.