Adding Overloads of stdlib Built-in Types __new__ with No Arguments
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 5.1k
- Forks
- 2.1k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 82
Beschreibung
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:
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit stdlib/builtins.pyi, insbesondere mit den im Issue verlinkten Definitionen von int.new und str.new, und untersuche die entsprechenden Stubs der integrierten Konstruktoren für bool und bytes. Prüfe, wie bestehende Typechecker Standardparameter im Vergleich zu separaten Overloads behandeln, und stelle dann sicher, dass die Stubs Aufrufe ohne Argument von Aufrufen mit einem Argument unterscheiden und die daraus resultierenden inferierten Konstantenwerte validieren.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- tooling
- Issue-Typ
- Feature
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100