Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/msgspec/_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -7994,6 +7994,8 @@ Struct_replace(
}
}

if (Struct_post_init(struct_type, out) < 0) goto error;

if (is_gc && should_untrack && MS_IS_TRACKED(out))
PyObject_GC_UnTrack(out);
return out;
Expand Down
28 changes: 14 additions & 14 deletions tests/unit/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -2379,6 +2379,20 @@ class Test(msgspec.Struct):
assert sys.getrefcount(x2) == x2_count + 1
del t2

def test_replace_calls_post_init(self, replace):
count = 0

class Ex(Struct):
def __post_init__(self):
nonlocal count
count += 1

x1 = Ex()
assert count == 1
x2 = replace(x1)
assert x1 == x2
assert count == 2


class TestAsDictAndAsTuple:
def test_asdict(self):
Expand Down Expand Up @@ -2663,17 +2677,3 @@ def __post_init__(self):
x2 = x1.__copy__()
assert x1 == x2
assert count == 1

def test_post_init_not_called_on_replace(self):
count = 0

class Ex(Struct):
def __post_init__(self):
nonlocal count
count += 1

x1 = Ex()
assert count == 1
x2 = msgspec.structs.replace(x1)
assert x1 == x2
assert count == 1
Loading