Skip to content

Commit 44f555a

Browse files
committed
Remove use of deprecated python 3.12 strtobool
distutils is not available in python 3.12, so a substitute is needed for the strtobool code. Use same resolution as implemented by the ITK project. I, Hans Johnson <[email protected]>, hereby add my Signed-off-by to this commit: 89212f5 Signed-off-by: Hans Johnson <[email protected]>
1 parent 410109a commit 44f555a

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

monai/utils/misc.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import warnings
2525
from ast import literal_eval
2626
from collections.abc import Callable, Iterable, Sequence
27-
from distutils.util import strtobool
2827
from math import log10
2928
from pathlib import Path
3029
from typing import TYPE_CHECKING, Any, TypeVar, cast, overload
@@ -78,6 +77,24 @@
7877
"run_cmd",
7978
]
8079

80+
def _strtobool(val: str) -> bool:
81+
"""
82+
Replaces deprecated (pre python 3.12)
83+
distutils strtobool function.
84+
85+
True values are y, yes, t, true, on and 1;
86+
False values are n, no, f, false, off and 0.
87+
Raises ValueError if val is anything else.
88+
"""
89+
val = val.lower()
90+
if val in ("y", "yes", "t", "true", "on", "1"):
91+
return 1
92+
elif val in ("n", "no", "f", "false", "off", "0"):
93+
return 0
94+
else:
95+
raise ValueError(f"invalid truth value {val}")
96+
97+
8198
_seed = None
8299
_flag_deterministic = torch.backends.cudnn.deterministic
83100
_flag_cudnn_benchmark = torch.backends.cudnn.benchmark
@@ -400,7 +417,7 @@ def _parse_var(s):
400417
d[key] = literal_eval(value)
401418
except ValueError:
402419
try:
403-
d[key] = bool(strtobool(str(value)))
420+
d[key] = bool(_strtobool(str(value)))
404421
except ValueError:
405422
d[key] = value
406423
return d

0 commit comments

Comments
 (0)