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] cannot compile parameterized dataclass #13304

Open
nstarman opened this issue Aug 1, 2022 · 2 comments
Open

[mypyc] cannot compile parameterized dataclass #13304

nstarman opened this issue Aug 1, 2022 · 2 comments
Labels
bug mypy got something wrong topic-dataclasses topic-mypyc mypyc bugs

Comments

@nstarman
Copy link

nstarman commented Aug 1, 2022

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
@ichard26
Copy link
Collaborator

ichard26 commented Aug 3, 2022

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

@nstarman
Copy link
Author

nstarman commented Aug 4, 2022

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

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

4 participants