Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mypyc: dataclasss __annotations__ are wrong #11330

Open
chadrik opened this issue Oct 14, 2021 · 1 comment
Open

mypyc: dataclasss __annotations__ are wrong #11330

chadrik opened this issue Oct 14, 2021 · 1 comment
Labels
bug mypy got something wrong topic-dataclasses topic-mypyc mypyc bugs

Comments

@chadrik
Copy link
Contributor

chadrik commented Oct 14, 2021

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
@chadrik chadrik added the bug mypy got something wrong label Oct 14, 2021
@chadrik
Copy link
Contributor Author

chadrik commented Oct 14, 2021

Hmmm... actually I think I just solved this.

    from mypyc.irbuild.function import load_type
    from mypy.types import Instance
    # We populate __annotations__ because dataclasses uses it to determine
    # which attributes to compute on.
    if isinstance(stmt.type, Instance):
        typ = load_type(builder, stmt.type.type, stmt.line)
    else:
        typ = builder.add(LoadAddress(type_object_op.type, type_object_op.src, stmt.line))
    key = builder.load_str(lvalue.name)
    builder.call_c(dict_set_item_op, [non_ext.anns, key, typ], stmt.line)

I'll make a PR for this shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-dataclasses topic-mypyc mypyc bugs
Projects
None yet
Development

No branches or pull requests

2 participants