[New-bugs-announce] [issue39078] __function.__defaults__ breaks for __init__ of dataclasses with default factory

Veith Röthlingshöfer report at bugs.python.org
Tue Dec 17 12:51:54 EST 2019


New submission from Veith Röthlingshöfer <veith21 at gmail.com>:

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 "<factory>".

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__)  # <factory>
```

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 <report at bugs.python.org>
<https://bugs.python.org/issue39078>
_______________________________________


More information about the New-bugs-announce mailing list