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

Dataclass + Generic fails during sleight of hand #827

Open
rtpg opened this issue Apr 9, 2021 · 1 comment
Open

Dataclass + Generic fails during sleight of hand #827

rtpg opened this issue Apr 9, 2021 · 1 comment
Labels
bug python compat Mypyc doesn't match CPython or documented semantics.

Comments

@rtpg
Copy link

rtpg commented Apr 9, 2021

With the following snippet:

from dataclasses import dataclass
from typing import Generic, TypeVar

T = TypeVar('T')

@dataclass
class DebugVisitor(Generic[T]):
    tree_depth: int = 0

print("DONE")

Loading the module blows up with the following trace:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/rtpg/proj/pycomp/dc_example.py", line 11, in <module>
    class DebugVisitor(Generic[T]):
AttributeError: attribute '__dict__' of 'type' objects is not writable

When we have the Generic + dataclass combo here, in CPy_DataclassSleightOfHand we end up trying to re-assign __dict__ to a type after applying the dataclass decorator. This issue doesn't show up if we aren't using the Generic[T] subclass.

I haven't fully isolated what's going on here, just yet though. Just wanted to at least document the issue. It kinda feels like there's a __dict__ check possible here (one thing I noticed is that in __mypyc_attrs__ we have tree_depth and __dict__ with the dataclass + Generic, but if we just have the dataclass we only have tree_depth).

@JukkaL JukkaL added the bug label Apr 12, 2021
@JukkaL
Copy link
Collaborator

JukkaL commented Apr 12, 2021

That dataclass code is quite tricky. In the long term we may want to completely compile away the @dataclass decorator, since it's such a basic language feature, but for now some smaller-case fix would be good to have for this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug python compat Mypyc doesn't match CPython or documented semantics.
Projects
None yet
Development

No branches or pull requests

2 participants