python / python/typeshed

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

Abierto
#15,007 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

stubs: improvement
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:

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. 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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.