Skip to content

Commit 6843287

Browse files
Copilotbhashemian
andcommitted
Fix circular import by moving strtobool to standalone module
Co-authored-by: bhashemian <3968947+bhashemian@users.noreply.github.com>
1 parent ec0d5c8 commit 6843287

4 files changed

Lines changed: 41 additions & 25 deletions

File tree

monailabel/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from pydantic import AnyHttpUrl
1616
from pydantic_settings import BaseSettings, SettingsConfigDict
1717

18-
from monailabel.utils.others.generic import strtobool
18+
from monailabel.utils.others.strtobool import strtobool
1919

2020

2121
def is_package_installed(name):

monailabel/utils/others/class_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from typing import List
1919

2020
from monailabel.interfaces.exception import MONAILabelError, MONAILabelException
21-
from monailabel.utils.others.generic import strtobool
21+
from monailabel.utils.others.strtobool import strtobool
2222

2323
logger = logging.getLogger(__name__)
2424

monailabel/utils/others/generic.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
from monailabel.config import settings
3232
from monailabel.utils.others.modelzoo_list import MAINTAINED_BUNDLES
33+
from monailabel.utils.others.strtobool import strtobool # noqa: F401
3334

3435
logger = logging.getLogger(__name__)
3536

@@ -240,29 +241,6 @@ def _list_files(d, ext):
240241
]
241242

242243

243-
def strtobool(s):
244-
"""Convert a string representation of truth to true or false.
245-
246-
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
247-
are 'n', 'no', 'f', 'false', 'off', and '0'. Returns the input if
248-
already a bool. Returns False if None.
249-
"""
250-
if s is None:
251-
return False
252-
if isinstance(s, bool):
253-
return s
254-
if not isinstance(s, str):
255-
raise TypeError(f"strtobool expects a string or bool, got {type(s).__name__}: {s!r}")
256-
257-
val = s.lower()
258-
if val in ('y', 'yes', 't', 'true', 'on', '1'):
259-
return True
260-
elif val in ('n', 'no', 'f', 'false', 'off', '0'):
261-
return False
262-
else:
263-
raise ValueError(f"invalid truth value {s!r}")
264-
265-
266244
def is_openslide_supported(name):
267245
ext = file_ext(name)
268246
supported_ext = (".bif", ".mrxs", ".ndpi", ".scn", ".svs", ".svslide", ".tif", ".tiff", ".vms", ".vmu")
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright (c) MONAI Consortium
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.
11+
12+
"""Standalone utility function for string to boolean conversion.
13+
14+
This module exists separately to avoid circular import dependencies.
15+
"""
16+
17+
18+
def strtobool(s):
19+
"""Convert a string representation of truth to true or false.
20+
21+
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
22+
are 'n', 'no', 'f', 'false', 'off', and '0'. Returns the input if
23+
already a bool. Returns False if None.
24+
"""
25+
if s is None:
26+
return False
27+
if isinstance(s, bool):
28+
return s
29+
if not isinstance(s, str):
30+
raise TypeError(f"strtobool expects a string or bool, got {type(s).__name__}: {s!r}")
31+
32+
val = s.lower()
33+
if val in ('y', 'yes', 't', 'true', 'on', '1'):
34+
return True
35+
elif val in ('n', 'no', 'f', 'false', 'off', '0'):
36+
return False
37+
else:
38+
raise ValueError(f"invalid truth value {s!r}")

0 commit comments

Comments
 (0)