Skip to content

Commit

Permalink
Clear cached property when validation is off
Browse files Browse the repository at this point in the history
  • Loading branch information
weiliangjin2021 committed Dec 5, 2024
1 parent 3e667d5 commit a7be3ba
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/test_components/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ def test_updated_copy():
assert s3 == s2


def test_cached_property_unvalidated_copy():
"""Make sure cached property is cleared when copied without validation."""
b = td.Box(size=(1, 1, 1))
_ = b.bounds
c = b.updated_copy(size=(2, 2, 2), validate=False)
assert c.bounds[0][0] == -1


def test_updated_copy_path():
"""Make sure updated copying shortcut works as expected with defaults."""
b = td.Box(size=(1, 1, 1))
Expand Down
3 changes: 3 additions & 0 deletions tidy3d/components/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ def copy(self, deep: bool = True, validate: bool = True, **kwargs) -> Tidy3dBase
new_copy = pydantic.BaseModel.copy(self, **kwargs)
if validate:
return self.validate(new_copy.dict())
# cached property is cleared automatically when validation is on, but it
# needs to be manually cleared when validation is off
new_copy._cached_properties = {}
return new_copy

def updated_copy(
Expand Down

0 comments on commit a7be3ba

Please sign in to comment.