python / python/typeshed

Adding Overloads of stdlib Built-in Types __new__ with No Arguments

Aperta
#15,007 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Calling int() and str() with no arguments returns 0 and "" respectively, likewise for bool() == False and bytes() == b"". But to actually call these constructors without arguments is very unusual and is likely a mistake on part of the user.

# a and b are two strings
x = int(a)
y = int()    # typo, should be int(b)

x / y    # ZeroDivisionError

It will be helpful for type stubs to denote that such no-argument calls return a constant value, and for IDEs to possibly notify of such use.

Currently the stub for int.__new__ looks like this:

https://github.com/python/typeshed/blob/904ea9ec022ae75d282f6a4c34563e4e9b8eed15/stdlib/builtins.pyi#L257

class int:
    @overload
    def __new__(cls, x: ConvertibleToInt = 0, /) -> Self: ...

and str.__new__:

class str(Sequence[str]):
    @overload
    def __new__(cls, object: object = "") -> Self: ...

I suggest adding an overload with no argument and removing the default parameter value from the one-arg __new__, though I'm not sure if there are existing special behaviors of type checkers regarding builtins that I'm unaware of.

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, in particolare dalle definizioni di int.new e str.new collegate nell’issue, e ispeziona gli stub dei costruttori integrati corrispondenti per bool e bytes. Verifica come i type checker esistenti gestiscono i parametri predefiniti rispetto agli overload separati, quindi assicurati che gli stub distinguano le chiamate senza argomenti dalle chiamate con un argomento e convalidino i valori costanti inferiti risultanti.

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

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Funzionalità
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.