python / python/typeshed

function is lacking a `__signature__` attribute

Aperta
#13,953 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Python
Stelle
5.1k
Fork
2.1k
Merge medio
1g 19h
PR unite (30g)
82

Descrizione

PEP-362 specifies that in order to cache the signature of any function, it is possible for the user to set its __signature__ attribute.

Actual

This attribute is not set in the builtins module over at https://github.com/python/typeshed/blob/main/stdlib/builtins.pyi#L1009

As a result a typechecker might reject the following piece of code

from inspect import signature


def main():

    def f():
        pass

    f.__signature__ = signature(f)


if __name__ == "__main__":
    main()

e.g. with pyright I have a false positive

/Users/leogermond/Lab/python/signature/main.py
  /Users/leogermond/Lab/python/signature/main.py:9:7 - error: Cannot assign to attribute "__signature__" for class "function"
    Attribute "__signature__" is unknown (reportFunctionMemberAccess)
1 error, 0 warnings, 0 informations

Expected

I would the __signature__ attribute to be specified in the function class (with a special marker indicating it's optional?) of the builtins module. A type checker such as pyright could then infer that setting this attribute is possible and accept the piece of code given in example.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia da stdlib/builtins.pyi intorno alla riga 1009 e verifica come è dichiarata la function class. Confronta il supporto opzionale richiesto per signature con PEP 362 e con l’esempio di assegnazione fornito, quindi verifica che un type checker come pyright lo accetti quando la modifica dello stub è completa.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
55/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.