python / python/cpython

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

Offen
#91,544 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

stdlib
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
36k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Lies zunächst die Einstiegspunkte inspect.signature und für die String-Darstellung von Signature, und reproduziere dann die Beispiele für Defaults mit Klassen, Modulen, Funktionen, Methoden und Generatoren. Vergleiche die Ausgabe mit der Verwendung von help und pydoc; abgeschlossen ist die Arbeit, wenn Signature Defaults in einer weniger übermäßig ausführlichen Form anzeigt und dabei nützliche Informationen für Aufrufer beibehält.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
developer-experience
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
28/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.