python / python/mypy

Methods with the same name as a type sometimes cause error "not valid as a type"

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

Nadie ha tomado este issue todavía.

bug
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Bug Report

When using a decorator on a class method with the same name as a type used in its return type, and the method is decorated with a decorator that spans multiple lines, an error is thrown.

What puzzles me is that this only causes an error if the decorator spans multiple lines, not if it is just on a single line.

Neither pylint nur flake8 do complain about having a method that shadows another name in general. So I am assuming that this should be generally ok.

To Reproduce

class Foo:
    @property
    # the existence of this comment triggers the error
    def int(self) -> int:
        return 0

Some more examples show that this

  • The error occurs in any situation where the decorator starts more than one line before the function definition.
    • with a blank line in between
    • with a comment line in between
    • with a multi-line argument decorator (this is the case where I initially encountered it when using @deprecated.
  • The error occurs with both built-in decorators (@property) and custom defined decorators.
  • The error occurs both with built-in types and imported types.

Expected Behavior

Use the types from the outer scope to construct the return type and validate correctly.

Actual Behavior

error: Function "__main__.Foo.int" is not valid as a type  [valid-type]
note: Perhaps you need "Callable[...]" or a callback protocol?

Your Environment

  • Mypy version used: 1.2.0, master branch via mypy-play.net
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10 and 3.11 tested

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 el ejemplo mínimo de Python del informe y ejecuta mypy 1.2.0 o el master actual; después compara un decorador de una sola línea con versiones separadas por un comentario, una línea en blanco o argumentos multilínea. Sigue cómo se resuelve el nombre del método mientras se comprueba su anotación de retorno. La tarea está terminada cuando todas las disposiciones de decoradores mostradas aceptan el tipo del ámbito externo sin el error valid-type.

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
Bien especificado
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.