python / python/mypy

Annotation parsing incorrect when class scope defines same name as import

Abierto
#12,414 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug topic-runtime-semantics topic-variable-scope
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Bug Report

Given:

import uuid
class Bar:
    uuid: uuid.UUID = uuid.uuid4()
reveal_type(Bar().uuid)

mypy reports no errors and will inform the user that the type of the attribute is uuid.UUID.

However this isn't strictly correct and without lazy annotations enabled this code will actually error at runtime:

  File ".../demo.py", line 3, in Bar
    uuid: uuid.UUID = uuid.uuid4()
AttributeError: 'UUID' object has no attribute 'UUID'

Ideally mypy would report the missing attribute (UUID) along with a mention of the name overlap between the type name and the attribute name.

Cross-linking: a related issue was raised against pyflakes (that the import appeared unused; https://github.com/PyCQA/pyflakes/issues/682; https://github.com/PyCQA/pyflakes/issues/648), though rejected on the grounds that this code is indeed invalid. Unfortunately when this code is alongside other code it is easy for there not to be any linter errors.

Your Environment

  • Mypy version used: 0.910, also tested 0.941
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none that matter
  • Python version used: 3.9
  • Operating system and version: Ubuntu 20.04

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 el problema con el fragmento de Python proporcionado usando mypy sin indicadores de configuración y, después, sigue la resolución de nombres de anotaciones para el atributo de clase y el uuid importado. Añade cobertura para los diagnósticos missing-attribute y name-overlap y verifica el comportamiento con el ejemplo no válido en tiempo de ejecución.

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
4/5
Tiempo estimado
3-5 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.