-
-
Notifications
You must be signed in to change notification settings - Fork 386
Add PEP681 explicit & overridable per-attribute __init__ alias
.
#945
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
Comments
This sounds like a great excuse to fix a very old desire! I would like to bike-shed the name on attribute for a moment tho. I find The PEP says:
I regret to inform you that the current algorithm is not |
I agree in that I don't like the color of Unfortunately, there's already a splash of paint here that's been set by pydantic on the dataclass_transform spec and, by extension, PEP681. I believe we (but let's not kid ourselves, you're the one with actual influence here 😉) could jump in before the paint on the PEP fully dries, but I think this may require hasty action! I'd mildly prefer to avoid a name split akin |
Regretfully, we are both incorrect here. There's some semi-spooky interaction here due to name mangling in the declaration that I haven't fully tracked down.
|
Would solving this issue make life easier for projects that alias or wrap |
To be clear, I'm not asking the argument to be renamed, just the attribute on the As for the underscores: I just remember it did something wrong with dunders. 😅 JFTR: they've added
I'm not sure what this means TBH. |
niiiiiiice seems i arrive just in time for some naming bikeshedding 🙃 the weird, you say? nah! there is prior art: the |
while I love a good bikeshed, but the whole point is that |
@asford do you need anything to proceed? |
Currently, you need a mypy-plugin if you alias or wrap The question is if this issue would help mypy figuring out that a decorator is an alias or wrapper for |
This is about aliasing field names in Quoth the PEP:
e.g. @define
class C:
x: int = field(alias="y")
C(y=42) |
alias
.alias
.
If This specific feature expands attrs's, and potentially by extension any As the spec does not cover all of attrs's splendid features, one could imagine a mypy plugin tailored for attrs patterned on dataclass_transform that supports additional attrs-specific features. Ie, an |
Let's do this. My $0.02: the name is a little unfortunate, it's just the As for cattrs, the technical part will be easy, but what about the UX? If you unstructure |
Yes, I agree with you both that the same is unfortunate. I'm happy to implement @hynek's suggestion of having a different name on
My goal for this feature is that it's a silent no-op for any existing users, but that I believe we should leave the semantics of Keeping these two behaviors independent and composable, with |
possible fix for #417 |
PEP681 introduces an optional
alias
parameter to field descriptors, which specifies an alternative__init__
name for the member. This is not implemented in dataclasses, but is implemented in pydantic.It would be useful to implement an explicit & overridable per-attribute
alias
in attrs, mirroring the behavior described in the PEP anddataclass_transform
specification. This would allow users to directly address the single largest incompatibility noted in the specification,attrs
private attributes handling, which strips leading underscores from member names, discussed in #795.Additionally, though we all love
attrs
as a tastemaker with strongly held principles, "private" attribute handling appears to have been (be?) a significant source of confusion and is likely causing at least one active attrs-internal bug. This has also been a source of issues in downstream libraries, noted in references below.In the longer-term, this would enable additional optional configuration flags to bring
attrs
into a strict-superset of the behavior ofdataclasses
, as roughly noted in #565 and #686. Enabling this work is a design goal here, but outside the scope of this issue.A (simple?) solution may be to extend
attr.ib
with an optionalalias
parameter, which sets the__init__
name for the field.This would, as is the custom, be added to
attrs.Attribute
and would be available during for introspection inattr.fields
and customization in thefield_transformer
hook.If
alias
is unspecified it will be set to astr
, indicating the attribute's argument name in the generated__init__
.For example in the current behavior,
alias = name.lstrip("_")
so that:We would retain the default private attribute renaming behavior in this implementation, which would allow users to either adjust the
alias
of the attribute to match their preferences or informdataclass_transform
consumers of the attribute's__init__
name. Ie:Refs:
Related issues:
#391 - Pending feature request, would be closed.
#244 - Class of issue introduced by behavior, would have workaround.
#619 - Feature request with no clear single answer, would have workaround.
#171 - Some original confusion about this behavior.
This is currently implemented in
_make
viaarg_name = a.name.lstrip("_")
.This is modeled in
evolve
viainit_name = attr_name if attr_name[0] != "_" else attr_name[1:]
.The
lstrip
/slice
inconsistency here is almost certainly a bug for non-idiomatic cases like:Exposing
alias
may be useful for libraries which interact with attrs-generated__init__
methods.Typified by
cattrs
, which re-implement's private-attribute naming viaian = an if (is_dc or an[0] != "_") else an[1:]
.Note, this implementation is also affected by
lstrip
/slice
inconsistency.This could be replaced by a lookup of the target Attribute alias.
Originally posted by @asford in #686 (comment)
The text was updated successfully, but these errors were encountered: