[issue33796] dataclasses.replace broken with class variables

2018-06-07 Thread Sigurd Ljødal

New submission from Sigurd Ljødal :

The dataclasses.replace function does not work for classes that have class 
variables. See the console output below for an example.

$ python
Python 3.7.0b5+ (heads/3.7:3c417610ad, Jun  7 2018, 16:21:29) 
[Clang 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import typing
>>> import dataclasses
>>> @dataclasses.dataclass(frozen=True)
... class Test:
...   a: int
...   class_var: typing.ClassVar[str] = 'foo'
... 
>>> obj = Test(a=1)
>>> dataclasses.replace(obj, a=2)
Traceback (most recent call last):
  File "", line 1, in 
  File 
"/Users/sigurdljodal/.pyenv/versions/3.7-dev/lib/python3.7/dataclasses.py", 
line 1179, in replace
return obj.__class__(**changes)
TypeError: __init__() got an unexpected keyword argument 'class_var'

--
messages: 318942
nosy: sigurd
priority: normal
severity: normal
status: open
title: dataclasses.replace broken with class variables
versions: Python 3.7

___
Python tracker 
<https://bugs.python.org/issue33796>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33796] dataclasses.replace broken with class variables

2018-06-07 Thread Sigurd Ljødal

Sigurd Ljødal  added the comment:

I think you misunderstood the issue here. I'm not trying to replace the class 
variable itself, I'm changing another field on a class that has a class 
variable. The problem is that the replace method includes the class variable in 
the change dict when copying fields that are not included in the function call.

My call is: dataclasses.replace(obj, a=2) where I try to replace a, but it 
fails because class_var is included in the attributes to __init__ by the 
replace function.

The problem is on this line: 
https://github.com/python/cpython/blob/master/Lib/dataclasses.py#L1162
The _FIELDS attribute includes the class variable as a field, and it is 
therefore added to the changes dict.

--

___
Python tracker 
<https://bugs.python.org/issue33796>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com