Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: python-attrs/attrs
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 6106e0d3c22548a12202e6ef28e65be344e9f2d6
Choose a base ref
..
head repository: python-attrs/attrs
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 1ac1cdde70b1c03be42be35384f19ac846cf7833
Choose a head ref
Showing with 9 additions and 6 deletions.
  1. +8 −6 src/attr/_make.py
  2. +1 −0 tests/test_make.py
14 changes: 8 additions & 6 deletions src/attr/_make.py
Original file line number Diff line number Diff line change
@@ -446,7 +446,11 @@ def _add_init(cls, frozen):
sha1.hexdigest()
)

script, globs = _attrs_to_script(attrs, frozen)
script, globs = _attrs_to_script(
attrs,
frozen,
post_init=hasattr(cls, '__post_init__')
)
locs = {}
bytecode = compile(script, unique_filename, "exec")
attr_dict = dict((a.name, a) for a in attrs)
@@ -540,7 +544,7 @@ def validate(inst):
a.validator(inst, a, getattr(inst, a.name))


def _attrs_to_script(attrs, frozen):
def _attrs_to_script(attrs, frozen, post_init=False):
"""
Return a script of an initializer for *attrs* and a dict of globals.
@@ -680,10 +684,8 @@ def fmt_setter_with_converter(attr_name, value_var):
a.name))
names_for_globals[val_name] = a.validator
names_for_globals[attr_name] = a
lines.extend([
"func = getattr(self, '__post_init__', None)",
"if callable(func): func()",
])
if post_init:
lines.append("self.__post_init__()")

return """\
def __init__(self, {args}):
1 change: 1 addition & 0 deletions tests/test_make.py
Original file line number Diff line number Diff line change
@@ -318,6 +318,7 @@ def __post_init__(self2):
assert hasattr(c, 'z')
assert c.z == 30


@attributes
class GC(object):
@attributes