Skip to content

Commit e21421e

Browse files
committed
make proc_val more permissive: not run when type differs from recording
1 parent 13273c3 commit e21421e

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/pymatgen/io/vasp/inputs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,8 @@ def smart_int_or_float_bool(str_: str) -> float | int | bool:
10391039
if "int" in incar_types:
10401040
return int(re.match(r"^-?[0-9]+", val)[0]) # type: ignore[index]
10411041

1042-
except ValueError:
1042+
# If re.match doesn't hit, it would return None and thus TypeError from indexing
1043+
except (ValueError, TypeError):
10431044
pass
10441045

10451046
# Not in known keys. We will try a hierarchy of conversions.

tests/io/vasp/test_inputs.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,43 @@ def test_proc_val(self):
10131013
assert Incar.proc_val("LREAL", "Auto") == "Auto"
10141014
assert Incar.proc_val("LREAL", "on") == "On"
10151015

1016+
def test_proc_val_inconsistent_type(self):
1017+
"""proc_val should not raise even when value conflicts with expected type."""
1018+
1019+
bool_values = ["T", ".FALSE."]
1020+
int_values = ["5", "-3"]
1021+
float_values = ["1.23", "-4.56e-2"]
1022+
list_values = ["3*1.0 2*0.5", "1 2 3"]
1023+
str_values = ["Auto", "Run", "Hello"]
1024+
1025+
# Expect bool
1026+
for v in int_values + float_values + list_values + str_values:
1027+
assert Incar.proc_val("LASPH", v) is not None
1028+
1029+
# Expect int
1030+
for v in bool_values + float_values + list_values + str_values:
1031+
assert Incar.proc_val("LORBIT", v) is not None
1032+
1033+
# Expect float
1034+
for v in bool_values + int_values + list_values + str_values:
1035+
assert Incar.proc_val("ENCUT", v) is not None
1036+
1037+
# Expect str
1038+
for v in bool_values + int_values + float_values + list_values:
1039+
assert Incar.proc_val("ALGO", v) is not None
1040+
1041+
# Expect list
1042+
for v in bool_values + int_values + float_values + str_values:
1043+
assert Incar.proc_val("PHON_TLIST", v) is not None
1044+
1045+
# Union type (bool | str)
1046+
for v in int_values + float_values + list_values:
1047+
assert Incar.proc_val("LREAL", v) is not None
1048+
1049+
# Non-existent
1050+
for v in int_values + float_values + list_values + str_values + bool_values:
1051+
assert Incar.proc_val("HELLOWORLD", v) is not None
1052+
10161053
def test_check_params(self):
10171054
# Triggers warnings when running into invalid parameters
10181055
incar = Incar(

0 commit comments

Comments
 (0)