python / python/cpython

inspect.Signature doesn't display defaults as they appear in code

Open
#91,544 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

Certain types of defaults show up in output of Signature instances as overly-verbose representations that don't match their syntax display.

>>> import inspect
>>> def a_test(a=str, b=inspect, c=inspect.Signature, d=inspect.signature, e=inspect.Signature.replace, f=(i for i in range(3))):
...    """lots of objects with awkward reprs"""
...
>>> print(inspect.signature(a_test))
(a=<class 'str'>, b=<module 'inspect' from '/Users/jeremyp/git/cpython/Lib/inspect.py'>, c=<class 'inspect.Signature'>, d=<function signature at 0x10c77e980>, e=<function Signature.replace at 0x10c77fba0>, f=<generator object <genexpr> at 0x10c6d7100>)
>>>
>>> def b_test(a: str = str):
...    """annotations seem to get this right"""
...
>>> print(inspect.signature(b_test))
(a: str = <class 'str'>)

The string representation of Signature is important as it is used, among other places, as the output of help and pydoc

>>> help(locale.atof)
Help on function atof in module locale:

atof(string, func=<class 'float'>)
    Parses a string as a float according to the locale settings.

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 reading the inspect.signature and Signature string-representation entry points, then reproduce the examples for defaults involving classes, modules, functions, methods, and generators. Compare the output with help and pydoc usage; done means Signature displays defaults in a less overly verbose form while retaining useful information for callers.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
developer-experience
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.