python / python/mypy

mypyc: dataclasss __annotations__ are wrong

Aperta
#11,330 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-dataclasses topic-mypyc
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

The __annotations__ of a compiled dataclass are not the same as its pure-python counterpart.

To Reproduce

  1. Create a file with a dataclass
from dataclasses import dataclass

@dataclass
class Person:
    age : int
    name : str
  1. check it's annotations
python -c "import myfile;print(myfile.Person.__annotations__)"
{'age': <class 'int'>, 'name':  <class 'str'>}
  1. Compile it

  2. Check the compiled annotations:

python -c "import myfile;print(myfile.Person.__annotations__)"
{'age': <class 'type'>, 'name':  <class 'type'>}

Expected Behavior

The annotations for the compiled class should be {'age': <class 'int'>, 'name': <class 'str'>} but instead they are all <class 'type'>.

I see this in the code:

def add_non_ext_class_attr(builder: IRBuilder,
                           non_ext: NonExtClassInfo,
                           lvalue: NameExpr,
                           stmt: AssignmentStmt,
                           cdef: ClassDef,
                           attr_to_cache: List[Tuple[Lvalue, RType]]) -> None:
    """Add a class attribute to __annotations__ of a non-extension class.

    If the attribute is initialized with a value, also add it to __dict__.
    """
    # We populate __annotations__ because dataclasses uses it to determine
    # which attributes to compute on.
    # TODO: Maybe generate more precise types for annotations
    key = builder.load_str(lvalue.name)
    typ = builder.add(LoadAddress(type_object_op.type, type_object_op.src, stmt.line))
    builder.call_c(dict_set_item_op, [non_ext.anns, key, typ], stmt.line)

So it looks like it's just hard-wiring type_object_op.

It's important to have accurate type annotations, because dataclasses are often inspected dynamically in order to automatically serialize them, generate protobufs, create UIs, etc based on their field data types.

Your Environment

  • Mypy version used: 0.920 master
  • Python version used: 3.8
  • Operating system and version: mac

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

Inizia riproducendo la discrepanza con l’esempio di dataclass, quindi esamina la funzione add_non_ext_class_attr di mypyc e il suo utilizzo di type_object_op. Traccia come viene selezionato il valore dell’annotazione e verifica che le classi compilate espongano le stesse annotazioni int e str della versione in puro Python.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.