Skip to content

Commit dd0cd2b

Browse files
committed
fix caching and copy update
1 parent bf75a7b commit dd0cd2b

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

tests/test_components/test_base.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import numpy as np
44
import pytest
55
import tidy3d as td
6+
from pydantic import ValidationError
7+
from pydantic_core import PydanticSerializationError
68
from tidy3d.components.base import Tidy3dBaseModel
79

810
M = td.Medium()
@@ -198,7 +200,7 @@ def test_attrs(tmp_path):
198200
assert obj.attrs == {"foo": "attr"}
199201

200202
# this is still not allowed though
201-
with pytest.raises(TypeError):
203+
with pytest.raises(ValidationError):
202204
obj.attrs = {}
203205

204206
# attrs can be modified
@@ -215,7 +217,7 @@ def test_attrs(tmp_path):
215217

216218
# attrs are in the json strings
217219
obj_json = obj3.json()
218-
assert '{"foo": "bar"}' in obj_json
220+
assert '{"foo":"bar"}' in obj_json
219221

220222
# attrs are in the dict()
221223
obj_dict = obj3.dict()
@@ -230,7 +232,7 @@ def test_attrs(tmp_path):
230232

231233
# test attrs that can't be serialized
232234
obj.attrs["not_serializable"] = type
233-
with pytest.raises(TypeError):
235+
with pytest.raises(PydanticSerializationError):
234236
obj.json()
235237

236238

tidy3d/components/base.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,21 @@ def copy(
209209
Optional mapping of fields to overwrite (passed straight
210210
through to ``model_copy(update=...)``).
211211
"""
212+
if update:
213+
allowed = set(self.model_fields) | {
214+
f.alias for f in self.model_fields.values() if f.alias
215+
}
216+
unknown = set(update) - allowed
217+
if unknown:
218+
raise ValueError(f"Update for '{self.type}' contains unknown fields: {unknown}")
219+
212220
new_model = self.model_copy(deep=deep, update=update)
213221

214222
if validate:
215223
return self.__class__.model_validate(new_model.model_dump())
224+
else:
225+
# make sure cache is always cleared
226+
new_model._cached_properties = {}
216227

217228
return new_model
218229

0 commit comments

Comments
 (0)