python / python/mypy

Allow redefinition even if a closure function is created between definitions

Abierto
#10,016 1 comentario 0 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

I was trying to debug why I was getting no-redef errors when I had --allow-redefinition. I had seen https://github.com/python/mypy/issues/6963 so, I figured that because both the definition and use of the variable were in a loop it was not triggering that condition (variable use). This did not turn out to be the case, and I was looking through similar issues I noticed #7984 and the example given did exhibit what I was trying to do and what I expected to work. I was able to confirm that it was working on the version of mypy I was using and I set to reduce my test case to something that was easy to reproduce. I made sure everything was typed by stubbing in concrete definitions and started to pull pieces of my test case apart.

Quickly and surprisingly I found out that a closure function I had defined was breaking the redefinition, but the enclosed function was unrelated to the variable. Without further ado:

To Reproduce

# mypy: allow-redefinition
from random import random

def f() -> None:
    x = 0

    reveal_type(x)
    print(x)

    def f2() -> float:
        return random()

    x = 'x'
    reveal_type(x)

Expected Behavior

reveal_type 1: int
reveal_type: 2: 'str'

Actual Behavior

note: In function "f":
7: note: Revealed type is 'builtins.int'
13: error: Incompatible types in assignment (expression has type "str", variable has type "int")  [assignment]
        x = 'x'
            ^
t2.py:14: note: Revealed type is 'builtins.int'

Your Environment

  • Mypy version used: 0.800
  • Mypy command-line flags: --warn-unused-configs --allow-redefinition
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]                                                                         
allow_redefinition = true                                                                                                                  
pretty = true                                                                  
show_error_codes = true                                                        
show_error_context = true                                                      
warn_unused_ignores = true                                                     
warn_unreachable = true
  • Python version used: 3.8.7

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 ejecutando la reproducción mínima en Python del issue con --allow-redefinition y compara los dos resultados de reveal_type. Sigue la comprobación de redefinición alrededor de la closure anidada f2; está terminado cuando se acepta la segunda asignación y los tipos revelados son int y después str.

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

Evaluación

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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.