mypy icon indicating copy to clipboard operation
mypy copied to clipboard

[mypyc] cannot compile parameterized dataclass

Open nstarman opened this issue 3 years ago • 2 comments

Bug Report

mypyc can compile a parameterized dataclass, but it fails at import.

To Reproduce

  1. Make a file with
from __future__ import annotations

from dataclasses import dataclass
from typing import TypeVar, Generic


T = TypeVar("T")


class Foo(Generic[T]):
    pass


@dataclass
class Bar(Foo[T]):

    x: T

    @property
    def also_x(self) -> T:
        return self.x
  1. Compile with mypyc (it appears that it works)
  2. Try importing Bar from file (to discover it does not work)

Actual Behavior

AttributeError                            Traceback (most recent call last)
...
----> 1 from test import Bar

File ~/.../test.py:15, in <module>
     10 class Foo(Generic[T]):
     11     pass
     14 @dataclass
---> 15 class Bar(Foo[T]):
     17     x: T
     19     @property
     20     def also_x(self) -> T:

AttributeError: attribute '__dict__' of 'type' objects is not writable

Your Environment

  • Mypy version used: '0.950'
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.9.12
  • Operating system and version: macOS Monterey 12.4

nstarman avatar Aug 01 '22 21:08 nstarman

This is effectively a duplicate of https://github.com/mypyc/mypyc/issues/827. Black had to work around this issue too (we just converted the dataclass to a regular class...)

ichard26 avatar Aug 03 '22 20:08 ichard26

Thanks for the further details. Darn, I was really hoping to use dataclasses.replace, which of course requires @dataclass.

nstarman avatar Aug 04 '22 03:08 nstarman