You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/ericvsmith'closed_at=Nonecreated_at=<Date2019-11-30.23:53:26.972>labels= ['3.7', '3.8', 'type-bug', 'library']
title='dataclass defaults behave inconsistently for init=True/init=False when default is a descriptor'updated_at=<Date2020-10-18.23:48:06.146>user='https://bugs.python.org/KevinShweh'
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.
Output:
Foo().callback: <function Foo.<lambda> at 0x019592F8>Foo().callback(2): 4Bar().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
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: