Skip to content

Commit 63e9afd

Browse files
authored
Merge pull request #1251 from Libensemble/refactor/pre1.2.1_smallfixes
Tiny pydantic version detemination fixes
2 parents 861000d + 82c1e55 commit 63e9afd

5 files changed

Lines changed: 22 additions & 19 deletions

File tree

libensemble/tests/unit_tests/test_ensemble.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44

55
import libensemble.tests.unit_tests.setup as setup
6-
from libensemble.utils.misc import pydanticV1, pydanticV2, specs_dump
6+
from libensemble.utils.misc import pydanticV1, specs_dump
77

88

99
def test_ensemble_init():
@@ -132,7 +132,7 @@ def test_flakey_workflow():
132132
"""Test initializing a workflow via Specs and Ensemble.run()"""
133133
if pydanticV1:
134134
from pydantic.error_wrappers import ValidationError
135-
elif pydanticV2:
135+
else:
136136
from pydantic import ValidationError
137137

138138
from libensemble.ensemble import Ensemble

libensemble/tests/unit_tests/test_models.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import libensemble.tests.unit_tests.setup as setup
44
from libensemble.specs import ExitCriteria, GenSpecs, LibeSpecs, SimSpecs, _EnsembleSpecs
5-
from libensemble.utils.misc import pydanticV1, pydanticV2, specs_dump
5+
from libensemble.utils.misc import pydanticV1, specs_dump
66

77
if pydanticV1:
88
from pydantic.error_wrappers import ValidationError
9-
elif pydanticV2:
9+
else:
1010
from pydantic import ValidationError
1111

1212

@@ -53,7 +53,7 @@ def test_sim_gen_alloc_exit_specs_invalid():
5353
try:
5454
if pydanticV1:
5555
SimSpecs.parse_obj(bad_specs)
56-
elif pydanticV2:
56+
else:
5757
SimSpecs.model_validate(bad_specs)
5858
flag = 0
5959
except ValidationError as e:
@@ -64,7 +64,7 @@ def test_sim_gen_alloc_exit_specs_invalid():
6464
try:
6565
if pydanticV1:
6666
GenSpecs.parse_obj(bad_specs)
67-
elif pydanticV2:
67+
else:
6868
GenSpecs.model_validate(bad_specs)
6969
flag = 0
7070
except ValidationError as e:
@@ -77,7 +77,7 @@ def test_sim_gen_alloc_exit_specs_invalid():
7777
try:
7878
if pydanticV1:
7979
ExitCriteria.parse_obj(bad_ec)
80-
elif pydanticV2:
80+
else:
8181
ExitCriteria.model_validate(bad_ec)
8282
flag = 0
8383
except ValidationError:
@@ -90,28 +90,28 @@ def test_libe_specs():
9090
libE_specs = {"mpi_comm": Fake_MPI(), "comms": "mpi"}
9191
if pydanticV1:
9292
ls = LibeSpecs.parse_obj(libE_specs)
93-
elif pydanticV2:
93+
else:
9494
ls = LibeSpecs.model_validate(libE_specs)
9595

9696
libE_specs["sim_input_dir"] = "./simdir"
9797
libE_specs["sim_dir_copy_files"] = ["./simdir"]
9898
if pydanticV1:
9999
ls = LibeSpecs.parse_obj(libE_specs)
100-
elif pydanticV2:
100+
else:
101101
ls = LibeSpecs.model_validate(libE_specs)
102102

103103
libE_specs = {"comms": "tcp", "nworkers": 4}
104104

105105
if pydanticV1:
106106
ls = LibeSpecs.parse_obj(libE_specs)
107-
elif pydanticV2:
107+
else:
108108
ls = LibeSpecs.model_validate(libE_specs)
109109
assert ls.disable_resource_manager, "resource manager should be disabled when using tcp comms"
110110

111111
libE_specs = {"comms": "tcp", "workers": ["hello.host"]}
112112
if pydanticV1:
113113
ls = LibeSpecs.parse_obj(libE_specs)
114-
elif pydanticV2:
114+
else:
115115
ls = LibeSpecs.model_validate(libE_specs)
116116

117117

@@ -121,7 +121,7 @@ def test_libe_specs_invalid():
121121
try:
122122
if pydanticV1:
123123
LibeSpecs.parse_obj(bad_specs)
124-
elif pydanticV2:
124+
else:
125125
LibeSpecs.model_validate(bad_specs)
126126
flag = 0
127127
except ValidationError:

libensemble/utils/misc.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
pydanticV1 = pydantic_version == "1"
1313
pydanticV2 = pydantic_version == "2"
1414

15+
if not pydanticV1 and not pydanticV2:
16+
raise ModuleNotFoundError("Pydantic not installed or current version not supported. Install v1 or v2.")
17+
1518

1619
def extract_H_ranges(Work: dict) -> str:
1720
"""Convert received H_rows into ranges for labeling"""
@@ -33,14 +36,14 @@ def extract_H_ranges(Work: dict) -> str:
3336
def specs_dump(specs, **kwargs):
3437
if pydanticV1:
3538
return specs.dict(**kwargs)
36-
elif pydanticV2:
39+
else:
3740
return specs.model_dump(**kwargs)
3841

3942

4043
def specs_checker_getattr(obj, key, default=None):
4144
if pydanticV1: # dict
4245
return obj.get(key, default)
43-
elif pydanticV2: # actual obj
46+
else: # actual obj
4447
try:
4548
return getattr(obj, key)
4649
except AttributeError:
@@ -50,5 +53,5 @@ def specs_checker_getattr(obj, key, default=None):
5053
def specs_checker_setattr(obj, key, value):
5154
if pydanticV1: # dict
5255
obj[key] = value
53-
elif pydanticV2: # actual obj
56+
else: # actual obj
5457
obj.__dict__[key] = value

libensemble/utils/pydantic_bindings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from libensemble import specs
66
from libensemble.resources import platforms
7-
from libensemble.utils.misc import pydanticV1, pydanticV2
7+
from libensemble.utils.misc import pydanticV1
88
from libensemble.utils.validators import (
99
_UFUNC_INVALID_ERR,
1010
_UNRECOGNIZED_ERR,
@@ -47,7 +47,7 @@ class Config:
4747
specs.LibeSpecs.Config = Config
4848
specs._EnsembleSpecs.Config = Config
4949

50-
elif pydanticV2:
50+
else:
5151
from pydantic import ConfigDict
5252
from pydantic import validate_call as libE_wrapper # noqa: F401
5353
from pydantic.fields import FieldInfo

libensemble/utils/validators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import numpy as np
55

66
from libensemble.resources.platforms import Platform
7-
from libensemble.utils.misc import pydanticV1, pydanticV2
7+
from libensemble.utils.misc import pydanticV1
88
from libensemble.utils.specs_checkers import (
99
_check_any_workers_and_disable_rm_if_tcp,
1010
_check_exit_criteria,
@@ -170,7 +170,7 @@ def genf_set_in_out_from_attrs(cls, values):
170170
def check_logical_cores(cls, values):
171171
return _check_logical_cores(values)
172172

173-
elif pydanticV2:
173+
else:
174174
from pydantic import field_validator, model_validator
175175

176176
# SPECS VALIDATORS #####

0 commit comments

Comments
 (0)