python / python/mypy

Better message for overloaded functions with required kwargs

Abierto
#16,528 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

feature topic-overloads topic-usability
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Feature

The following:

from typing import overload, Union

def fun(a: Union[int, str], *, b: bool) -> None:
    pass

fun(1, True)

says helpfully:

main.py:6: error: Too many positional arguments for "fun"  [misc]
Found 1 error in 1 file (checked 1 source file)

mypy-playground

In the following case however:

from typing import overload, Union

@overload
def fun(a: int, *, b: bool) -> None: ...

@overload
def fun(a: str, *, b: bool) -> None: ...

def fun(a: Union[int, str], *, b: bool) -> None:
    pass

fun(1, True)

we get:

main.py:12: error: No overload variant of "fun" matches argument types "int", "bool"  [call-overload]
main.py:12: note: Possible overload variants:
main.py:12: note:     def fun(a: int, *, b: bool) -> None
main.py:12: note:     def fun(a: str, *, b: bool) -> None
Found 1 error in 1 file (checked 1 source file)

mypy-playground

I think the error should also point out that the number of positional arguments is incorrect.

Pitch

I found myself confused by this a couple of times and have wasted good few hours trying to understand what's wrong with the call which is clearly correct, and just glancing straight past the * - something that's easy to do with a longer / more complicated function signature. I think it would save a meaningful amount of time for users and it feels like a simple change.

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

Reproduce ambos ejemplos de Python en los casos enlazados de mypy-playground y compara sus diagnósticos. Se considera terminado cuando el error de la función sobrecargada también explica que el número de argumentos posicionales no es válido, conservando los detalles de las sobrecargas.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
devtools
Tipo de issue
Nueva funcionalidad
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
48/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.