[issue38947] dataclass defaults behave inconsistently for init=True/init=False when default is a descriptor

Irit Katriel report at bugs.python.org
Sun Oct 18 19:48:06 EDT 2020


Irit Katriel <iritkatriel at yahoo.com> added the comment:

I think you may have meant this issue (which is not related to field()):

from dataclasses import dataclass
from typing import Callable

@dataclass(init=True)
class Foo:
    callback: Callable[[int], int] = lambda x: x**2
     
@dataclass(init=False)
class Bar:
    callback: Callable[[int], int] = lambda x: x**2

print('Foo().callback:', Foo().callback)
print('Foo().callback(2):', Foo().callback(2))

print('Bar().callback:', Bar().callback)
print('Bar().callback(3):', Bar().callback(3))

Output:
Foo().callback: <function Foo.<lambda> at 0x019592F8>
Foo().callback(2): 4
Bar().callback: <bound method Bar.<lambda> of Bar(callback=<bound method Bar.<lambda> of ...>)>
Traceback (most recent call last):
  File "C:\Users\User\src\cpython\x.py", line 17, in <module>
    print('Bar().callback(3):', Bar().callback(3))
TypeError: Bar.<lambda>() takes 1 positional argument but 2 were given

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38947>
_______________________________________


More information about the Python-bugs-list mailing list