Skip to content

Add ability for __post_init__ method to be defined.#111

Closed
tbeadle wants to merge 1 commit intopython-attrs:masterfrom
tbeadle:post_init
Closed

Add ability for __post_init__ method to be defined.#111
tbeadle wants to merge 1 commit intopython-attrs:masterfrom
tbeadle:post_init

Conversation

@tbeadle
Copy link
Copy Markdown
Contributor

@tbeadle tbeadle commented Nov 11, 2016

If this method is defined for a class, it will get executed at the end
of __init__. Previously, there was no way to extend what was
done during __init__ when using @attr.s.

Comment thread src/attr/_make.py Outdated
names_for_globals[val_name] = a.validator
names_for_globals[attr_name] = a
lines.extend([
"func = getattr(self, '__post_init__', None)",

This comment was marked as spam.

@codecov-io
Copy link
Copy Markdown

codecov-io commented Nov 11, 2016

Current coverage is 100% (diff: 100%)

Merging #111 into master will not change coverage

@@           master   #111   diff @@
====================================
  Files           8      8          
  Lines         466    468     +2   
  Methods         0      0          
  Messages        0      0          
  Branches      105    106     +1   
====================================
+ Hits          466    468     +2   
  Misses          0      0          
  Partials        0      0          

Powered by Codecov. Last update cbfb229...7724d31

Comment thread src/attr/_make.py Outdated
script, globs = _attrs_to_script(
attrs,
frozen,
post_init=hasattr(cls, '__post_init__')

This comment was marked as spam.

Copy link
Copy Markdown
Member

@hynek hynek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a bunch of comments many of which arguably should have been covered by the contribution guide so I’ve added it to my own todo list to fix that.

If you have problems with Hypothesis, offering candy to @Tinche always helped for me. ;)

Thank you for your contribution! And please remember to ping the ticket once you’re done.

Comment thread src/attr/_make.py Outdated


def _attrs_to_script(attrs, frozen):
def _attrs_to_script(attrs, frozen, post_init=False):

This comment was marked as spam.

Comment thread src/attr/_make.py
names_for_globals[val_name] = a.validator
names_for_globals[attr_name] = a
if post_init:
lines.append("self.__post_init__()")

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

Comment thread tests/test_make.py
assert C.D.__name__ == "D"
assert C.D.__qualname__ == C.__qualname__ + ".D"

def test_post_init(self):

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

Comment thread docs/examples.rst
>>> i.y
[]

Sometimes, you want to have your class's ``__init__`` method do more than just

This comment was marked as spam.

Comment thread CHANGELOG.rst

- Don't overwrite ``__name__`` with ``__qualname__`` for ``attr.s(slots=True)`` classes.
`#99 <https://github.com/hynek/attrs/issues/99>`_
- Allow for a ``__post_init__`` method that, if defined, will get executed at

This comment was marked as spam.

Comment thread tests/test_make.py Outdated
self2.z = self2.x + self2.y

c = C(x=10, y=20)
assert hasattr(c, 'z')

This comment was marked as spam.

Comment thread tests/test_make.py Outdated

c = C(x=10, y=20)
assert hasattr(c, 'z')
assert c.z == 30

This comment was marked as spam.

If this method is defined for a class, it will get executed at the end
of ``__init__``.  Previously, there was no way to extend what was
done during ``__init__`` when using ``@attr.s``.
@wearpants
Copy link
Copy Markdown

Late to the party, but yes absolutely post conversion

@hynek hynek added this to the 16.3.0 milestone Nov 19, 2016
@hynek hynek closed this in 1fbaa20 Nov 20, 2016
@hynek
Copy link
Copy Markdown
Member

hynek commented Nov 20, 2016

I’ve fixed it up so we don’t have ping-pong over minutiae anymore, thank you very much for your contribution!

I’ve decided to rename __post_init__ to __attrs_post_init__ for consistency with __attrs_attrs__ and to be explicit about it’s heritage.

If someone has good arguments against it, feel free to open a new issue before 16.3.0 is out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants