Adding Overloads of stdlib Built-in Types __new__ with No Arguments
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 5.1k
- Forks
- 2.1k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 82
Descripción
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.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con stdlib/builtins.pyi, especialmente con las definiciones de int.new y str.new enlazadas en el issue, e inspecciona los stubs de los constructores integrados correspondientes para bool y bytes. Comprueba cómo los comprobadores de tipos existentes gestionan los parámetros predeterminados frente a los overloads separados y, después, asegúrate de que los stubs distingan las llamadas sin argumentos de las llamadas con un argumento y validen los valores constantes inferidos resultantes.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- tooling
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 45/100