Title: Make dataclasses.field() accept another name for __init__ field's name
Type: enhancement Stage:
Components: Versions: Python 3.8
Status: open Resolution:
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: eric.smith, remi.lapeyre, rhettinger, theophile
Priority: normal Keywords:

Created on 2019-01-10 17:11 by remi.lapeyre, last changed 2019-01-17 13:37 by theophile.

Messages (2)
msg333409 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2019-01-10 17:11
When creating a class, I sometimes wish to get this behavior:

    def MyClass:
        def __init__(self, param):
            self._param = param

        def __repr__(self):
            return f"MyClass(param={self._param})"

Unless I'm making a mistaking, this behavior is not currently possible with dataclasses.

I propose to change:

    field(*, default=MISSING, default_factory=MISSING, repr=True, hash=None, init=True, compare=True, metadata=None)


    field(*, default=MISSING, default_factory=MISSING, repr=True, hash=None, init=True, compare=True, metadata=None, target=None)

with target being used as the init parameter name for this field and in the repr.

If this is accepted, I can post the patch to make this change.
msg333586 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-01-14 06:25
"target" seems too general for the OP's use case.  "private=False" would be more focused.  General renaming occasionally has uses but is mostly an anti-pattern.  

Some thought also needs to be given to downstream effects of renaming (what shows up in help(), impact of renaming on type annotations, introspection challenges, what asdict() should do, whether the overall implementation would be made more complex, is the attribute now out of reach for __post_init__(), is this even within the scope of what dataclasses set out to do, etc).
Date User Action Args
2019-01-17 13:37:57theophilesetnosy: + theophile
2019-01-14 06:25:48rhettingersetnosy: + rhettinger
messages: + msg333586
2019-01-10 20:42:54eric.smithsetassignee: eric.smith

type: enhancement
nosy: + eric.smith
2019-01-10 17:11:11remi.lapeyrecreate