python / python/mypy

Annotation parsing incorrect when class scope defines same name as import

Aperta
#12,414 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-runtime-semantics topic-variable-scope
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci il problema con lo snippet Python fornito usando mypy senza flag di configurazione, quindi traccia la risoluzione dei nomi delle annotazioni per l’attributo di classe e l’uuid importato. Aggiungi la copertura per le diagnostiche missing-attribute e name-overlap e verifica il comportamento rispetto all’esempio non valido a runtime.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.