Skip to content

Commit 58106a6

Browse files
authored
Remove use of deprecated python 3.12 strtobool (#7900)
distutils is not available in python 3.12, so a substitute is needed for the strtobool code. Fixes #7899 ### Description Replace distutils strtobool with local _strtobool version. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [x] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. --------- Signed-off-by: Hans Johnson <[email protected]>
1 parent 410109a commit 58106a6

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
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

requirements-min.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Requirements for minimal tests
22
-r requirements.txt
3-
setuptools>=50.3.0,<66.0.0,!=60.6.0
3+
setuptools>=50.3.0,<66.0.0,!=60.6.0 ; python_version < "3.12"
4+
setuptools>=70.2.0; python_version >= "3.12"
45
coverage>=5.5
56
parameterized

0 commit comments

Comments
 (0)