attrs
attrs copied to clipboard
Add Attribute.alias
Summary
Spike implementation for #945.
Goals:
- [ ] This is silent no-op for "normal" users of
attrsnot usingalias. Libraries building onattrsmay usealiasto detect both existing private-name init aliases and user-supplied aliases. - [x] Add an
alias : Optional[str]parameter toattr.ibet al and property toAttribute. - [x] Lift current private-attribute-naming logic (
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. - [x] Leave
Attribute.aliasasNoneif not explicitly specified, so thatfield_transformerimplementations can (a) create attributes without needing to be aware ofaliasand optionally providealiasoverrides beforeattrs. - [x] Update
attr.evolveto useAttribute.aliasrather than re-implemented name-mangling. - [x] Update the attribute and private-attribute-handling documentation to note the new
aliasparameter. - [ ] Expand dataclass_transform documentation to describe how
aliascan be used to inform type checkers of private-name renaming behavior. - [ ] Politely file a bug against
attrsnoting the inconsistency between documentation and behavior. - [ ] File a PR against against
cattrsto accessAttribute.aliasduring codegen iff available inattrs.
Non-goals:
- Modification of the existing private-attribute-naming logic, though could be tackled pre-release in a follow-up PR.
Pull Request Check List
- [x] Added tests for changed code. Our CI fails if coverage is not 100%.
- [ ] New features have been added to our Hypothesis testing strategy.
- [x] Changes or additions to public APIs are reflected in our type stubs (files ending in
.pyi).- [x] ...and used in the stub test file
tests/typing_example.py. - [x] If they've been added to
attr/__init__.pyi, they've also been re-imported inattrs/__init__.pyi.
- [x] ...and used in the stub test file
- [ ] Updated documentation for changed code.
- [ ] New functions/classes have to be added to
docs/api.rstby hand. - [x] Changes to the signature of
@attr.s()have to be added by hand too. - [ ] Changed/added classes/methods/functions have appropriate
versionadded,versionchanged, ordeprecateddirectives. Find the appropriate next version in our__init__.pyfile.
- [ ] New functions/classes have to be added to
- [ ] Documentation in
.rstfiles is written using semantic newlines. - [ ] Changes (and possible deprecations) have news fragments in
changelog.d. - [ ] Consider granting push permissions to the PR branch, so maintainers can fix minor issues themselves without pestering you.
Anything you need from my end? 😇
w00t! Thanks for coming back! I'll try to be faster this time! :)
Really appreciate your work on attrs. Thanks for being stoked on the contribution!
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.
aw looks like you're right. pyright works: #1063
mypy doesn't:
Adding
AliasExample(without_alias=42, _with_alias=23)
gives me: tests/typing_example.py:134: error: Unexpected keyword argument "_with_alias" for "AliasExample"; did you mean "with_alias" or "without_alias"?
@Tinche 🐶🥺?
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.