Skip to content

Commit 87862f0

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. Signed-off-by: Hans Johnson <hans-johnson@uiowa.edu>
1 parent 410109a commit 87862f0

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

monai/utils/misc.py

Lines changed: 20 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,25 @@
7877
"run_cmd",
7978
]
8079

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

0 commit comments

Comments
 (0)