Skip to content
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

support custom repr() callable for attributes #568

Merged
merged 15 commits into from Sep 6, 2019
Merged

support custom repr() callable for attributes #568

merged 15 commits into from Sep 6, 2019

Conversation

@wbolster
Copy link
Member

@wbolster wbolster commented Sep 5, 2019

This adds support for using a custom callable to be used instead of the default repr() when formatting individual attributes in the generated __repr()__. The attr.ib(repr=…) argument now takes either a bool (as before), or a callable that takes a value and returns a string.

Fixes #567. Fixes #212.

@wbolster wbolster requested a review from hynek Sep 5, 2019
@wbolster
Copy link
Member Author

@wbolster wbolster commented Sep 5, 2019

not sure why the doctest fails. in an interactive shell it works:

>>> import attr
>>> @attr.s
... class C(object):
...     user = attr.ib()
...     password = attr.ib(repr=lambda value: '***')
... 
>>> C("me", "s3kr3t")
C(user='me', password=***)

update: oops, seem i can't even copy/paste correctly... 🙈

@hynek
Copy link
Member

@hynek hynek commented Sep 6, 2019

The doctests says ... password = attr.ib(repr=False).

@hynek
Copy link
Member

@hynek hynek commented Sep 6, 2019

@Julian is this good enough for your – I'm afraid ancient – #212?

@Julian
Copy link
Member

@Julian Julian commented Sep 6, 2019

Cool. Other than #567 (comment), seems likely... Can review the patch more carefully in a bit.

changelog.d/568.change.rst Outdated Show resolved Hide resolved
docs/examples.rst Outdated Show resolved Hide resolved
docs/examples.rst Outdated Show resolved Hide resolved
src/attr/_make.py Show resolved Hide resolved
src/attr/_make.py Outdated Show resolved Hide resolved
src/attr/_make.py Outdated Show resolved Hide resolved
src/attr/_make.py Show resolved Hide resolved
@Julian
Copy link
Member

@Julian Julian commented Sep 6, 2019

Cool! Left random comments just to help myself understand what this was doing mostly, but yeah looks like it'd indeed hit #212.

@hynek
Copy link
Member

@hynek hynek commented Sep 6, 2019

Additionally to what Julian wrote, please add an example to typing_example.py to verify typing works. Thanks!

Copy link
Member

@hynek hynek left a comment

Almost there, but please also add an versionchanged tag to attrib, thanks!

changelog.d/568.change.rst Outdated Show resolved Hide resolved
docs/examples.rst Outdated Show resolved Hide resolved
src/attr/_make.py Outdated Show resolved Hide resolved
@wbolster wbolster self-assigned this Sep 6, 2019
@wbolster
Copy link
Member Author

@wbolster wbolster commented Sep 6, 2019

Almost there, but please also add an versionchanged tag to attrib, thanks!

sure, but what's the right value?

@hynek
Copy link
Member

@hynek hynek commented Sep 6, 2019

Almost there, but please also add an versionchanged tag to attrib, thanks!

sure, but what's the right value?

Next release is 19.2.0.

src/attr/_make.py Show resolved Hide resolved
@wbolster
Copy link
Member Author

@wbolster wbolster commented Sep 6, 2019

Additionally to what Julian wrote, please add an example to typing_example.py to verify typing works. Thanks!

added in b1bd940

@wbolster wbolster requested review from hynek and Julian Sep 6, 2019
@wbolster
Copy link
Member Author

@wbolster wbolster commented Sep 6, 2019

ok, please review again. i think i fully addressed all comments so far.

@wbolster
Copy link
Member Author

@wbolster wbolster commented Sep 6, 2019

pr description updated as well

@wbolster wbolster changed the title wip: support custom repr() callable for attributes support custom repr() callable for attributes Sep 6, 2019
hynek
hynek approved these changes Sep 6, 2019
Copy link
Member

@hynek hynek left a comment

Great, thanks everyone!

Julian
Julian approved these changes Sep 6, 2019
Copy link
Member

@Julian Julian left a comment

Woohoo!

@hynek hynek merged commit 0b91364 into python-attrs:master Sep 6, 2019
18 checks passed
@wbolster wbolster deleted the custom-attribute-repr branch Sep 6, 2019
@wbolster
Copy link
Member Author

@wbolster wbolster commented Sep 6, 2019

🚀 thanks all the quick turn-around

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

3 participants