Skip to content

Commit 0a72b9c

Browse files
committed
pydantic v2 (wip)
add somefiles replacing v1 more replacements copy methods and ordering of basemodel update get_submodels_by_hash basemodel done (except for docs) basemodel and modespec done slowly but surely.. progress next batch going going getting started on medium more refactoring new structure for medium.py add medium why it no work fix medium upgrade material library most of it first pass add pydantic-settings fixes to validators, mutable assignment, ...: remove skip_if_fields_missing cleaning up types & type serializaton fix printing and serialization of autograd types more type serialization updates Fix traced ndarray serialization fix equality check in basemodel fix some v1 leftovers fix some tests fix equality check sim_data tests passing make serializer more robust and another fix for equality comparison fix warn if none validators Check for pydantic v2 ValidationErrors Canonicalize coordinate handling of unstructured datasets Simulation data tests passing material library tests passing fix bad name fix material libray -> library lotsa fixes, tests_web & test_package passing fix remaining web test warnings everything importable for doctests rebase, wip fix mutation safer ndarray coercion and expressions fix working on post init validation fix multiphysics medium attribute lookup remove test script from vcs doctests passing fix NedeljkovicSorefMashanovich fix non-component tests fix tracer serialization remove unnecessary to_static call the smallest changes really do take the longest all of test_IO passing fix caching and copy update docstring for __init_subclass__ passing: IO, base, beam, boundaries, custom, eme, field_projection, geometry, log passing: grid, grid_spec, heat, heat_charge, layerrefinement, lumped_element, medium passing: meshgenerate, microwave, mode passing: monitor, packaging, parameter_perturbation, perturbation_medium, scene, sidewall passing: expressions passing: smatrix, array_factor, design, dispersion_fitter, microwave, mode_solver wip: adjoint & invdes passing: source, structure, time_modulation passing: types add helpers to filter model fields add tests for new basemodel helpers small fixes wip: autograd fix waveguide passing: autograd rebase fixes no more post-init validators fix dataarray json schema rework array constraints wip: adjoint
1 parent c5188a6 commit 0a72b9c

248 files changed

Lines changed: 8593 additions & 7956 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

poetry.lock

Lines changed: 49 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ include = [{ path = "tidy3d/style.mplstyle", format = ["sdist", "wheel"] }]
2424

2525
[tool.poetry.dependencies]
2626
python = ">=3.9,<3.14"
27+
typing-extensions = { version = "*", python = "<3.11" }
2728
pyroots = ">=0.5.0"
2829
xarray = ">=2023.08"
2930
importlib-metadata = ">=6.0.0"
@@ -34,7 +35,8 @@ numpy = "*"
3435
matplotlib = "*"
3536
shapely = "^2.0"
3637
pandas = "*"
37-
pydantic = "^2.0"
38+
pydantic = ">=2,<3"
39+
pydantic-settings=">=2,<3"
3840
PyYAML = "*"
3941
dask = "*"
4042
toml = "*"
@@ -281,6 +283,7 @@ markers = [
281283
env = ["MPLBACKEND=Agg", "OMP_NUM_THREADS=1"]
282284
doctest_optionflags = "NORMALIZE_WHITESPACE ELLIPSIS"
283285
norecursedirs = [
286+
"tests/test_plugins/test_adjoint.py", # TODO <yaugenst-flex>
284287
"tests/_test_local",
285288
"tests/test_cli",
286289
"tests/_test_data",

tests/test_components/test_IO.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
# Store an example of every minor release simulation to test updater in the future
2323
SIM_DIR = "tests/sims"
24+
SIM_STATIC = SIM.to_static()
2425

2526

2627
@pytest.fixture
@@ -66,14 +67,14 @@ def test_simulation_load_export(split_string, tmp_path):
6667
major, minor, patch = __version__.split(".")
6768
path = os.path.join(tmp_path, f"simulation_{major}_{minor}_{patch}.json")
6869
path_hdf5 = os.path.join(tmp_path, f"simulation_{major}_{minor}_{patch}.h5")
69-
SIM.to_file(path)
70-
SIM.to_hdf5(path_hdf5)
70+
SIM_STATIC.to_file(path)
71+
SIM_STATIC.to_hdf5(path_hdf5)
7172
SIM2 = td.Simulation.from_file(path)
7273
SIM_HDF5 = td.Simulation.from_hdf5(path_hdf5)
7374
assert (
74-
set_datasets_to_none(SIM)._json_string == SIM2._json_string
75+
set_datasets_to_none(SIM_STATIC)._json_string == SIM2._json_string
7576
), "original and loaded simulations are not the same"
76-
assert SIM == SIM_HDF5, "original and loaded from hdf5 simulations are not the same"
77+
assert SIM_STATIC == SIM_HDF5, "original and loaded from hdf5 simulations are not the same"
7778

7879

7980
def test_simulation_load_export_yaml(tmp_path):
@@ -101,30 +102,30 @@ def test_component_load_export_yaml(tmp_path):
101102

102103
def test_simulation_load_export_hdf5(split_string, tmp_path):
103104
path = str(tmp_path / "simulation.hdf5")
104-
SIM.to_file(path)
105+
SIM_STATIC.to_file(path)
105106
SIM2 = td.Simulation.from_file(path)
106-
assert SIM == SIM2, "original and loaded simulations are not the same"
107+
assert SIM_STATIC == SIM2, "original and loaded simulations are not the same"
107108

108109

109110
def test_simulation_load_export_hdf5_gz(split_string, tmp_path):
110111
path = str(tmp_path / "simulation.hdf5.gz")
111-
SIM.to_file(path)
112+
SIM_STATIC.to_file(path)
112113
SIM2 = td.Simulation.from_file(path)
113-
assert SIM == SIM2, "original and loaded simulations are not the same"
114+
assert SIM_STATIC == SIM2, "original and loaded simulations are not the same"
114115

115116

116117
def test_simulation_load_export_hdf5_explicit(split_string, tmp_path):
117118
path = str(tmp_path / "simulation.hdf5")
118-
SIM.to_hdf5(path)
119+
SIM_STATIC.to_hdf5(path)
119120
SIM2 = td.Simulation.from_hdf5(path)
120-
assert SIM == SIM2, "original and loaded simulations are not the same"
121+
assert SIM_STATIC == SIM2, "original and loaded simulations are not the same"
121122

122123

123124
def test_simulation_load_export_hdf5_gz_explicit(split_string, tmp_path):
124125
path = str(tmp_path / "simulation.hdf5.gz")
125-
SIM.to_hdf5_gz(path)
126+
SIM_STATIC.to_hdf5_gz(path)
126127
SIM2 = td.Simulation.from_hdf5_gz(path)
127-
assert SIM == SIM2, "original and loaded simulations are not the same"
128+
assert SIM_STATIC == SIM2, "original and loaded simulations are not the same"
128129

129130

130131
def test_simulation_load_export_pckl(tmp_path):
@@ -189,7 +190,7 @@ def test_validation_speed(tmp_path):
189190
for i in range(n):
190191
new_structure = SIM.structures[0].copy(update={"name": str(i)})
191192
new_structures.append(new_structure)
192-
S = SIM.copy(update=dict(structures=new_structures))
193+
S = SIM.copy(update=dict(structures=tuple(new_structures)))
193194

194195
S.to_file(path)
195196
time_start = time()
@@ -220,7 +221,9 @@ def test_simulation_updater(sim_file):
220221
def test_yaml(tmp_path):
221222
path = str(tmp_path / "simulation.json")
222223
SIM.to_file(path)
224+
SIM.to_file("simulation.json")
223225
sim = td.Simulation.from_file(path)
226+
224227
path1 = str(tmp_path / "simulation.yaml")
225228
sim.to_yaml(path1)
226229
sim1 = td.Simulation.from_yaml(path1)

tests/test_components/test_apodization.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""Tests mode objects."""
22

33
import matplotlib.pyplot as plt
4-
import pydantic.v1 as pydantic
54
import pytest
65
import tidy3d as td
6+
from pydantic import ValidationError
77

88

99
def test_apodization():
@@ -14,27 +14,27 @@ def test_apodization():
1414

1515

1616
def test_end_lt_start():
17-
with pytest.raises(pydantic.ValidationError):
17+
with pytest.raises(ValidationError):
1818
_ = td.ApodizationSpec(start=2, end=1, width=0.2)
1919

2020

2121
def test_no_width():
22-
with pytest.raises(pydantic.ValidationError):
22+
with pytest.raises(ValidationError):
2323
_ = td.ApodizationSpec(start=1, end=2)
24-
with pytest.raises(pydantic.ValidationError):
24+
with pytest.raises(ValidationError):
2525
_ = td.ApodizationSpec(start=1)
26-
with pytest.raises(pydantic.ValidationError):
26+
with pytest.raises(ValidationError):
2727
_ = td.ApodizationSpec(end=2)
2828

2929

3030
def test_negative_times():
31-
with pytest.raises(pydantic.ValidationError):
31+
with pytest.raises(ValidationError):
3232
_ = td.ApodizationSpec(start=-2, end=-1, width=0.2)
3333

34-
with pytest.raises(pydantic.ValidationError):
34+
with pytest.raises(ValidationError):
3535
_ = td.ApodizationSpec(start=1, end=2, width=-0.2)
3636

37-
with pytest.raises(pydantic.ValidationError):
37+
with pytest.raises(ValidationError):
3838
_ = td.ApodizationSpec(start=1, end=2, width=0)
3939

4040

0 commit comments

Comments
 (0)