Conversation
hynek
left a comment
There was a problem hiding this comment.
This looks all very good and thorough, just a bunch of questions and nits.
it also needs a newsfragment. make it sounds excited. :)
sorry for taking so long…whenever Attribute changes the diffs make me wann hide under the bed.
|
Anything you need from my end? 😇 |
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
asford
left a comment
There was a problem hiding this comment.
My turn to apologize for the slow turnaround.
I've applied the review comments, and believe this is ready for a final pass.
|
w00t! Thanks for coming back! I'll try to be faster this time! :) |
|
Really appreciate your work on attrs. |
hynek
left a comment
There was a problem hiding this comment.
Nailed it a tiny nit aside!
Thank you so much for coming back, I know how difficult it is to pick up something like this after a long time.
|
Cool. Cattrs will be no problem, I can just bump the dependency to the latest version of attrs and implement it. Mypy will be a bigger issue I think. |
|
given alias is a dataclass transform feature, shouldn't it work OOB? |
|
Unsure, Mypy has a custom attrs plugin so I don't know if the dataclass_transform code path is in charge of this. |
|
Yeah, yeah. But we'd need to release attrs first. |
|
yeah, it's super overdue too. i'll try to get #1003 in ASAP (it's just an alias, so it should be just busywork) and push it out RSN. |
Since the attrs version release, pub tests have begun failing with the
error "TypeError: keywords must be strings". Attrs have added an "alias"
parameter to each attribute[1], which is used in the attrs's "evolve"
method to construct a new instance. This causes issues with the attribute
"from", which has some special logic implemented in order to be usable
at all[2]. The attribute "from" is generated from "from_", copying all
its parameters. Since "alias" is a new parameter, it isn't copied and
thus its value is set no "None". attrs's "evolve" method uses **kwargs
to create a new instance, and it sets one of the keys in **kwargs to
"None" (from alias). Keys in **kwargs must be strings, which causes the
TypeError. It can be fixed by also setting "alias" in the newly created
"from" attribute. In case of "from_", it has the same value as "name"
parameter ("from_"), which is why we don't want to copy it from the old
attribute, but explicitly set it to "from" (otherwise it would be
"from_"). The change should be backwards compatible with older versions
of attrs.
[1] python-attrs/attrs#950
[2] release-engineering#108
Since new attrs version release, pub tests have begun failing with the
error "TypeError: keywords must be strings". Attrs have added an "alias"
parameter to each attribute[1], which is used in the attrs's "evolve"
method to construct a new instance. This causes issues with the attribute
"from", which has some special logic implemented in order to be usable
at all[2]. The attribute "from" is generated from "from_", copying all
its parameters. Since "alias" is a new parameter, it isn't copied and
thus its value is set no "None". attrs's "evolve" method uses **kwargs
to create a new instance, and it sets one of the keys in **kwargs to
"None" (from alias). Keys in **kwargs must be strings, which causes the
TypeError. It can be fixed by also setting "alias" in the newly created
"from" attribute. In case of "from_", it has the same value as "name"
parameter ("from_"), which is why we don't want to copy it from the old
attribute, but explicitly set it to "from" (otherwise it would be
"from_"). The change should be backwards compatible with older versions
of attrs.
[1] python-attrs/attrs#950
[2] release-engineering#108
Since new attrs version release, pub tests have begun failing with the
error "TypeError: keywords must be strings". Attrs have added an "alias"
parameter to each attribute[1], which is used in the attrs's "evolve"
method to construct a new instance. This causes issues with the attribute
"from", which has some special logic implemented in order to be usable
at all[2]. The attribute "from" is generated from "from_", copying all
its parameters. Since "alias" is a new parameter, it isn't copied and
thus its value is set no "None". attrs's "evolve" method uses **kwargs
to create a new instance, and it sets one of the keys in **kwargs to
"None" (from alias). Keys in **kwargs must be strings, which causes the
TypeError. It can be fixed by also setting "alias" in the newly created
"from" attribute. In case of "from_", it has the same value as "name"
parameter ("from_"), which is why we don't want to copy it from the old
attribute, but explicitly set it to "from" (otherwise it would be
"from_"). The change should be backwards compatible with older versions
of attrs.
[1] python-attrs/attrs#950
[2] release-engineering#108
Summary
PEP681 introduces an optional
aliasparameter to field descriptors, which specifies an alternative__init__name for the member. This is not implemented in dataclasses, but is implemented in pydantic.Add an explicit & overridable per-attribute
aliasin attrs, mirroring the behavior described in the PEP anddataclass_transformspecification. This allows users to directly address the single largest incompatibility noted in the specification,attrsprivate attributes handling, which strips leading underscores from member names, discussed in #795.This feature unblocks implementation of a shim layer to support
attrs/dataclassinterop, discussed in:closes: #945
closes: #572
Goals:
attrsnot usingalias. Libraries building onattrsmay usealiasto detect both existing private-name init aliases and user-supplied aliases.alias : Optional[str]parameter toattr.ibet al and property toAttribute.name.lstrip("_")) into_ClassBuilderso thataliasis always populated with the field's__init__parameter name, regardless of whether an explicit alias is provided, during__init__rendering andattr.fieldsintrospection.Attribute.aliasasNoneif not explicitly specified, so thatfield_transformerimplementations can (a) create attributes without needing to be aware ofaliasand optionally providealiasoverrides beforeattrs.attr.evolveto useAttribute.aliasrather than re-implemented name-mangling.aliasparameter.aliascan be used to inform type checkers of private-name renaming behavior.attrsnoting the inconsistency between documentation and behavior. Dunder-methods are still affected by attr.ibs that will be __name-mangled should be attrs-init-mangled to <name> not ClassName_<name> #619, butevolvefailures in pathological cases are resolved.cattrsto accessAttribute.aliasduring codegen iff available inattrs: UseAttribute.aliasfor init argument names if attrs>= 2022.2 cattrs#322Non-goals:
Pull Request Check List
Our CI fails if coverage is not 100%.
.pyi).tests/typing_example.py.attr/__init__.pyi, they've also been re-imported inattrs/__init__.pyi.docs/api.rstby hand.@attr.s()have to be added by hand too.versionadded,versionchanged, ordeprecateddirectives.Find the appropriate next version in our
__init__.pyfile..rstfiles is written using semantic newlines.changelog.d.