python / python/mypy

@overload declarations in stubs - required to be directly adjacent?

Abierto
#10,120 0 comentarios 2 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug topic-overloads
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Merge medio
1 d 18 h
PR fusionados (30 d)
54

Descripción

Bug Report

Currently, mypy requires @overload declarations in stubs to be directly adjacent, with no declarations (e.g. type vars) in between. However, I don't find that requirement in PEP 484 [1], nor in the typing module documentation [2].

[1] https://www.python.org/dev/peps/pep-0484/#function-method-overloading
[2] https://docs.python.org/3/library/typing.html#typing.overload

To Reproduce
Consider the following type stub:

import typing

@typing.overload
def foo() -> None: ...

K = typing.TypeVar('K')
@typing.overload
def foo(k: K) -> K: ...

Then run stubtest on it.

Expected Behavior
According to the documentation, I would expect this stub to be valid.

Actual Behavior

$ python -m mypy.stubtest teststub
error: failed mypy build.
teststub.pyi:4: error: Single overload definition, multiple required
teststub.pyi:8: error: Name 'foo' already defined on line 4
teststub.pyi:8: error: Single overload definition, multiple required

Pulling the type var declaration up fixes it:

import typing

K = typing.TypeVar('K')

@typing.overload
def foo() -> None: ...

@typing.overload
def foo(k: K) -> K: ...

Your Environment
Python 3.7.1 (anaconda) on Linux, latest master of mypy.
I also tested with mypy 0.790 on Windows, with the same result.

$ python --version
Python 3.7.1
$ python -m mypy --version
mypy 0.820+dev.497556f466dcda90d850b23e86c55ec4082be3f5

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

Empieza reproduciendo el fallo con el teststub mostrado y python -m mypy.stubtest teststub. Sigue cómo mypy valida las declaraciones @typing.overload adyacentes y añade una prueba de regresión que cubra el TypeVar intermedio; se considera terminado cuando el stub se acepta y los diagnósticos de overload existentes siguen siendo correctos.

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

Evaluación

Stack tecnológico
python
Área
devtools
Tipo de issue
Error
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.