[issue38947] dataclass defaults behave inconsistently for init=True/init=False when default is a descriptor
Kevin Shweh
report at bugs.python.org
Sat Nov 30 18:53:27 EST 2019
New submission from Kevin Shweh <kevin.shweh at gmail.com>:
The following code:
from dataclasses import dataclass, field
from typing import Callable
@dataclass
class Foo:
callback: Callable[[int], int] = lambda x: x**2
@dataclass
class Bar:
callback: Callable[[int], int] = field(init=False, default=lambda x: x**2)
print(Foo().callback(2))
print(Bar().callback(2))
prints 4 for the first print, but throws a TypeError for the second. This is because Foo() stores the default callback in the instance dict, while Bar() only has it in the class dict. Bar().callback triggers the descriptor protocol and produces a method object instead of the original callback.
There does not seem to be any indication in the dataclasses documentation that these fields will behave differently. It seems like they should behave the same, and/or the documentation should be clearer about how the default value/non-init field interaction behaves.
----------
components: Library (Lib)
messages: 357669
nosy: Kevin Shweh
priority: normal
severity: normal
status: open
title: dataclass defaults behave inconsistently for init=True/init=False when default is a descriptor
type: behavior
versions: Python 3.7, Python 3.8
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38947>
_______________________________________
More information about the Python-bugs-list
mailing list