Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change in behaviour of dataclasses

I am working on release 3.7.1 of the Transcrypt Python to JavaScript compiler. Part of the release procedure is a shipment test, in which Transcrypt is tested back to back with CPython.

It used to run flawlessly with the beta's of CPython 3.7, but with the release there's a problem.

The program:

from dataclasses import dataclass
from typing import ClassVar

@dataclass
class Test:
    x: ClassVar = 10
    y:  int = 10

t = Test ()

t.x = 20

print (repr (t))

used to print (using CPython):

Test(x=20, y=10)

but with the release it prints (again just using CPython):

Test(y=10)

So it leaves out the class variable x from the representation. Can anyone tell my whether or not this change is intentional, and where I can find a discussion that justifies it?

With this change Transcrypt behaves differently from CPython, which I don't want. So I wonder should I adapt Transcrypt or should I consider this a CPython regression and wait for it to be solved.

like image 613
Jacques de Hooge Avatar asked Aug 30 '18 14:08

Jacques de Hooge


1 Answers

From the documentation:

30.6.3. Class variables

One of two places where dataclass() actually inspects the type of a field is to determine if a field is a class variable as defined in PEP 526. It does this by checking if the type of the field is typing.ClassVar. If a field is a ClassVar, it is excluded from consideration as a field and is ignored by the dataclass mechanisms. Such ClassVar pseudo-fields are not returned by the module-level fields() function.

So, this appears to be an intentional change.

like image 168
jasonharper Avatar answered Sep 30 '22 06:09

jasonharper