python / python/mypy

[mypyc] cannot compile parameterized dataclass

Open
#13,304 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-dataclasses topic-mypyc
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the parameterized dataclass reproducer in test.py and compile it with mypyc using the reported Python and mypy versions. Check the generated module's import behavior and the interaction between the dataclass, Generic[T], and the property; done means importing Bar succeeds without the AttributeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.