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

2020-10-18 Thread Irit Katriel


Irit Katriel  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:  at 0x019592F8>
Foo().callback(2): 4
Bar().callback:  of Bar(callback= of ...>)>
Traceback (most recent call last):
  File "C:\Users\User\src\cpython\x.py", line 17, in 
print('Bar().callback(3):', Bar().callback(3))
TypeError: Bar.() takes 1 positional argument but 2 were given

--

___
Python tracker 

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



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

2020-10-18 Thread Irit Katriel


Irit Katriel  added the comment:

If I change Foo in your code to:

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

Then the same TypeError exception is raised for Foo. 

If I then change it back (remove the init=False so that it picks up the default 
value of True), and then change init=True in field:

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

Then I get the expected output of 
4
4

What do you consider to be an inconsistency here?

--
nosy: +iritkatriel

___
Python tracker 

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



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

2019-12-12 Thread Jeong-Min Lee


Change by Jeong-Min Lee :


--
nosy: +falsetru

___
Python tracker 

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



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

2019-12-10 Thread PCManticore


Change by PCManticore :


--
nosy: +Claudiu.Popa

___
Python tracker 

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



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

2019-12-06 Thread Ivan Levkivskyi


Change by Ivan Levkivskyi :


--
nosy: +levkivskyi

___
Python tracker 

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



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

2019-12-01 Thread Eric V. Smith


Change by Eric V. Smith :


--
assignee:  -> eric.smith

___
Python tracker 

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



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

2019-11-30 Thread Karthikeyan Singaravelan


Change by Karthikeyan Singaravelan :


--
nosy: +eric.smith

___
Python tracker 

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



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

2019-11-30 Thread Kevin Shweh


New submission from Kevin Shweh :

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 

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