New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add alias
as a field()
parameter for dataclasses
#101192
Comments
I’ve got this partially working. I’ll investigate attrs to make sure we’re in sync. Once I get a PR, hopefully some typing folks can take a look. |
Another use case could be to allow doing breaking changes when one wants to rename the spelling of a property. @dataclass
class SubmissionData(object):
allowlist: list[str] If I wanted to rename the spelling of @dataclass
class SubmissionData(object):
allow_list: list[str] = field(alias="allowlist") Then, only in a major version of my package, I would drop support |
Note though that when you define an |
that seems to break the definition of "alias" for me. I wonder if we really have to be 1:1 with attrs. |
Hmm, my main use case would be private attributes where you wouldn't want to use the non-alias name in the constructor: @dataclass
class C:
_name: str = field(alias="name") but I guess there might be other use cases. Though if you want to keep both names in the constructor, it would get a bit complicated. You'd need code for dealing with the case where both names are used at the same time, for example. I agree though that the word "alias" sounds like both names will be available. Unfortunately it's already enshrined in PEP 681... |
Hey I was wondering if there've been any updates on this. While doing some Googling I saw ran into this forum where @ericvsmith had volunteered to work on this in January 2023. Wondering if it's been abandoned. I have a use case where I need custom setters for type checking purposes. As a result I have: @dataclass
class Struct:
_field1: pd.DataFrame
_field2: pd.DataFrame
_field3: pd.DataFrame
@property
def field1(self):
return self._field1
@data.setter
def field1(self, val):
someguard(val)
self._field = val
... but when I initialise I have to do, struct = Struct(_field1=value1, _field2=value2, _field3=value3) which is inconsistent with how I access members: a = struct.field1
b = struct.field2
field3 = pd.DataFrame(a+b) One work around is to have a custom It would look a lot cleaner if I could just use |
There is a workaround with mypy. For details: https://web.archive.org/web/20240109161017/https://florimond.dev/en/posts/2018/10/reconciling-dataclasses-and-properties-in-python tl;dr: from dataclasses import dataclass, field
@dataclass
class Vehicle:
foo: str
wheels: int
_wheels: int = field(init=False, repr=False)
@property # type: ignore[no-redef]
def wheels(self) -> int:
print("getting wheels")
return self._wheels
@wheels.setter
def wheels(self, wheels: int) -> None:
print("setting wheels to", wheels)
self._wheels = wheels It works well with >>> a = Vehicle("bar", 42)
setting wheels to 42
>>> print(a)
getting wheels
Vehicle(foo='bar', wheels=42)
>>> from dataclasses import replace
>>> b = replace(a, wheels=12345)
setting wheels to 12345
>>> print(b)
getting wheels
Vehicle(foo='bar', wheels=12345)
>>> c = replace(a, foo="baz")
getting wheels
setting wheels to 42
>>> print(c)
getting wheels
Vehicle(foo='baz', wheels=42) |
Feature or enhancement
Add an
alias
parameter todataclasses.field()
which behaves likealias
inattrs.field()
(see here and search for "alias"). It has the effect that it changes the corresponding argument name in the synthesized__init__
.Pitch
Since attrs 22.2.0,
attrs.field
has analias
parameter that does the following:The typical use-case for this is private instance variables as in the above example. But there could be other use-cases as well, like
Besides being a useful feature in and of itself, another reason to add this to the standard library is that it would bring dataclasses in line with
dataclass_transform
which already supports thealias
parameter infield()
: https://peps.python.org/pep-0681/#field-specifier-parameters . It is currently a bit strange thatdataclass_transform
has a feature thatdataclasses.dataclass
doesn't actually have.Previous discussion
https://discuss.python.org/t/add-alias-as-a-field-parameter-for-dataclasses/22988/1
The text was updated successfully, but these errors were encountered: