Skip to content

Add inspect.signature support for more builtins #107161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Dutcho opened this issue Jul 23, 2023 · 3 comments
Open

Add inspect.signature support for more builtins #107161

Dutcho opened this issue Jul 23, 2023 · 3 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@Dutcho
Copy link

Dutcho commented Jul 23, 2023

Feature or enhancement

Like mentioned before for datetime classes, inspect.signature() also fails on many classes (and some functions) in builtins. However, this is not universal behaviour, as inspect.signature() works well with some classes (and most functions) in builtins.
The functions and classes in builtins that inspect.signature() supports have a str __text_signature__ pseudo-attribute, e.g.:

>>> divmod.__text_signature__
'($module, x, y, /)'
>>> complex.__text_signature__
'(real=0, imag=0)'

The proposed enhancement is to add the same __text_signature__ to others:

  • Functions: anext, breakpoint, dir, getattr, iter, max, min, next, vars
  • Classes: bool, filter, int, map, range, slice, str, and many more, incl. all Exception classes

Pitch

This addition will help consistency and avoid confusion and the need for special-casing.

Example

I ran into this when coding an arity() function using inspect.signature().parameters. My arity() works e.g. for float and complex, but fails for int, which was unexpected and confusing.

Beneficial side-effect

Likely, this will also improve help() output, e.g.

>>> help(iter)
iter(...)  <<< signature missing
    Get an iterator from an object.

Already available

This might seem challenging for some of the builtins functions and classes that have multiple signatures (e.g. iter(iterable) next to iter(callable, sentinel)). However, these are already available in the typeshed stub file, builtins.pyi.

@Dutcho Dutcho added the type-feature A feature request or enhancement label Jul 23, 2023
@Dutcho Dutcho changed the title Add inspect.signature support for builtins Add inspect.signature support for more builtins Jul 23, 2023
@iritkatriel iritkatriel added the stdlib Python modules in the Lib dir label Nov 27, 2023
@za3k
Copy link

za3k commented Mar 1, 2025

A small update for python 3.13.1 and a more exhaustive list:

inspect.signature fails on the following builtins: anext, bytearray, bytes, dict, dir, getattr, int, iter, max, min, next, range, slice, str, super, type, vars

As well as all error types, which are callable.

import inspect

failing = []
for k in dir(__builtins__):
    v = eval(k)
    if not hasattr(v, "__call__"):
        continue
    try:
        sig = inspect.signature(v)
    except:
        failing.append(k)

print(failing)

@picnixz picnixz added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Mar 1, 2025
@skirpichev
Copy link
Member

For exception types: #111405

For mentioned above builtins we can't do anything, until #73536 will be solved.

@Dutcho
Copy link
Author

Dutcho commented Mar 12, 2025

It looks #73536 can be solved by multi-signatures. What's required for it to be resolved?

And for #111405, brian030128 wanted to work on that. Don't know if it ever matured?

@skirpichev skirpichev self-assigned this Mar 13, 2025
@skirpichev skirpichev removed their assignment Apr 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

5 participants