[issue39078] __function.__defaults__ breaks for __init__ of dataclasses with default factory

2021-06-22 Thread Eric V. Smith


Eric V. Smith  added the comment:

I don't think there's any action to be done here, so I'm going to close this.

--
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

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



[issue39078] __function.__defaults__ breaks for __init__ of dataclasses with default factory

2021-06-22 Thread Petr Viktorin


Change by Petr Viktorin :


--
components: +Library (Lib) -C API

___
Python tracker 

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



[issue39078] __function.__defaults__ breaks for __init__ of dataclasses with default factory

2019-12-17 Thread Eric V. Smith


Eric V. Smith  added the comment:

I guess I could make the default value something like _CALLABLE(dict), but I'm 
not sure that would do you any good. But at least you could tell from the repr 
what it is.

--

___
Python tracker 

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



[issue39078] __function.__defaults__ breaks for __init__ of dataclasses with default factory

2019-12-17 Thread Eric V. Smith


Eric V. Smith  added the comment:

The problem is that __init__ has to have a sentinel to know whether or not to 
call the default value. If the default were just "dict", should it call it, or 
is the passed in value really dict, in which case it doesn't get called?

dataclass()
class Test:
a: int
b: Dict[Any, Any] = field(default_factory=dict)

The generated __init__ looks like:

def __init__(self, a, b=_HAS_DEFAULT_FACTORY):
   self.a = a
   self.b = dict() if b is _HAS_DEFAULT_FACTORY else b

If it were:

def __init__(self, a, b=dict):
   self.a = a

Then what would the assignment to self.b look like? What if you instantiated an 
object as Test(0, dict)? You wouldn't want dict to get called. You need to 
differentiate between Test(0, dict) and Test(0). The former does not call b, 
but the latter does call b.

--

___
Python tracker 

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



[issue39078] __function.__defaults__ breaks for __init__ of dataclasses with default factory

2019-12-17 Thread Eric V. Smith


Change by Eric V. Smith :


--
assignee:  -> eric.smith
nosy: +eric.smith

___
Python tracker 

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



[issue39078] __function.__defaults__ breaks for __init__ of dataclasses with default factory

2019-12-17 Thread Veith Röthlingshöfer

New submission from Veith Röthlingshöfer :

When creating a dataclass with a default that is a field with a default 
factory, the factory is not correctly resolved in cls.__init__.__defaults__. It 
evaluates to the __repr__ of dataclasses._HAS_DEFAULT_FACTORY_CLASS, which is 
"".

The expected behavior would be to have a value of whatever the default factory 
produces as a default.

This causes issues for example when using 
inspect.BoundParameters.apply_defaults() on the __init__ of such a dataclass.

Code to reproduce:

```
from dataclasses import dataclass, field
from typing import Any, Dict


@dataclass()
class Test:
a: int
b: Dict[Any, Any] = field(default_factory=dict)

print(Test.__init__.__defaults__)  # 
```

The affected packages are on a high-level dataclasses, on a lower level the 
issue is in the builtin __function.__defaults__.

--
components: C API
messages: 358562
nosy: RunOrVeith
priority: normal
severity: normal
status: open
title: __function.__defaults__ breaks for __init__ of dataclasses with default 
factory
type: behavior

___
Python tracker 

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