Skip to content

Commit

Permalink
Remove use of deprecated python 3.12 strtobool (#7900)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
hjmjohnson authored Jul 3, 2024
1 parent 410109a commit 58106a6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
22 changes: 20 additions & 2 deletions monai/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import warnings
from ast import literal_eval
from collections.abc import Callable, Iterable, Sequence
from distutils.util import strtobool
from math import log10
from pathlib import Path
from typing import TYPE_CHECKING, Any, TypeVar, cast, overload
Expand Down Expand Up @@ -78,6 +77,25 @@
"run_cmd",
]


def _strtobool(val: str) -> bool:
"""
Replaces deprecated (pre python 3.12)
distutils strtobool function.
True values are y, yes, t, true, on and 1;
False values are n, no, f, false, off and 0.
Raises ValueError if val is anything else.
"""
val = val.lower()
if val in ("y", "yes", "t", "true", "on", "1"):
return True
elif val in ("n", "no", "f", "false", "off", "0"):
return False
else:
raise ValueError(f"invalid truth value {val}")


_seed = None
_flag_deterministic = torch.backends.cudnn.deterministic
_flag_cudnn_benchmark = torch.backends.cudnn.benchmark
Expand Down Expand Up @@ -400,7 +418,7 @@ def _parse_var(s):
d[key] = literal_eval(value)
except ValueError:
try:
d[key] = bool(strtobool(str(value)))
d[key] = bool(_strtobool(str(value)))
except ValueError:
d[key] = value
return d
Expand Down
3 changes: 2 additions & 1 deletion requirements-min.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Requirements for minimal tests
-r requirements.txt
setuptools>=50.3.0,<66.0.0,!=60.6.0
setuptools>=50.3.0,<66.0.0,!=60.6.0 ; python_version < "3.12"
setuptools>=70.2.0; python_version >= "3.12"
coverage>=5.5
parameterized

0 comments on commit 58106a6

Please sign in to comment.